Re: Fastcode CharPosRev B&V 0.1.0



Hi

Our two first functions are array based because the treat the string as an
array and indexes into it

if S[CharNo] = SearchChar then

This compiles into

cmp al,[edx+ecx-$01]

and we would like to get rid of the -$01. It comes from the fact that
element 1 of an array is located at the baseadress = edx. ecx is the CharNo.

Let us try to give the compiler an opportunity to optimize away the -$01 by
writing

if S[CharNo+1] = SearchChar then

which compiles into

cmp al,[edx+ecx]

The rest of the function will need some corrections

function CharPosRev_DKC_Pas_3_a(SearchChar : Char; const S: string) :
Integer;
var
CharNo : Integer;

begin
Result := 0;
if Length(S) > 0 then
begin
CharNo := Length(S)-1;
repeat
if S[CharNo+1] = SearchChar then
begin
Result := CharNo+1;
Break;
end;
Dec(CharNo);
until (CharNo < 0);
end;
end;

This adds extra code for subtracting and adding 1 in CharNo := Length(S)-1;
and Result := CharNo+1; The change to the first line moves our branch target
by one byte and adds a 1 cycle latency to dec ecx and this occurs for all
strings. Result := CharNo+1; adds extra code to the path of a hit

CharPosRevDKCUnit.pas.171: Result := CharNo+1;
0046C1B6 8D7101 lea esi,[ecx+$01]

versus

CharPosRevDKCUnit.pas.151: Result := CharNo;
0046C182 8BF1 mov esi,ecx

Look up the latency numbers in the reference manuals for the respective
processors.

Benchmarking on Opteron

CharPosRev_DKC_Pas_3_a 0 3014 2618 5632

CharPosRev_DKC_Pas_2_a 0 3037 2628 5665

CharPosRev_DKC_Pas_3_b 8 3058 2650 5708

CharPosRev_DKC_Pas_2_c 8 3071 2681 5752

CharPosRev_DKC_Pas_3_c 4 3482 3180 6662

CharPosRev_DKC_Pas_2_b 4 3452 3242 6694

CharPosRev_DKC_Pas_3_d C 3513 3203 6716

CharPosRev_DKC_Pas_2_d C 4655 3675 8330

We see a very small improvement
Best regards
Dennis Kjaer Christensen

----------------------------------------------------------------------------
----
Jeg beskyttes af den gratis SPAMfighter til privatbrugere.
Den har indtil videre sparet mig for at få 5673 spam-mails
Betalende brugere får ikke denne besked i deres e-mails.
Hent en gratis SPAMfighter her.


.



Relevant Pages

  • Re: Newbie questions
    ... It compiles ... float getLowest(float array, int length); ... float getLowest(float array[], int length) { ... before printing a second prompt. ...
    (comp.lang.c)
  • Re: light weight types
    ... the array instance knows that it contains ... String instances and nothing else. ... The third line also compiles finely ... to Java. ...
    (comp.lang.java.programmer)
  • Re: foreach doesnt work with array of bools?
    ... > intialize them to false. ... > That compiles just fine. ... the array of bools seems to have intialized to false anyway. ...
    (microsoft.public.dotnet.languages.csharp)
  • How does Lisp compile constants?
    ... I'm wondering how Lisp compiles various constants that consist of more ... ;; trivial example where load-bitmap returns an array of bytes ... ;; blit the constant image to the screen ... return the array of bytes and then `img` be set to some constant array? ...
    (comp.lang.lisp)