[fpc-devel] Re: dominant short strings in compiler source

L505 fpc505 at z505.com
Sun May 21 05:33:49 CEST 2006


> IMHO this whole discussion is nonsense. For basic path related stuff,
> ansistring is good enough, and if findfirst/findnext are still limiting with
> the current memmgr (afaik we changed to Micha's one since those original
> tests).
>
> Moreover, if the weak point of ansistrings is the heap copying and implicit
> exceptions, the weak point of shortstrings iirc is unnecessary copying.
> Something that might really start to hurt more if strings get longer.

I agree that playing around with some benchmarks might be wise as all the assumptions
about stack based versus heap based could be proven false from doing some tests..

Well there is a way to emulate a wordstring and play around to see what it's performance
is like, I haven't done any testing yet verus some heap based situation. Just FYI, what
I've come up with in order to emulate my so called "wordstring".. as I don't think a
longstring is as needed as just a wordstring, because how are you going to allocate 2gb of
stack for that longstring which can go to 2 gigs..??

But then again, in the future, a wordstring might seem puny .. and we might end up calling
longstrings shortstrings, if memory gets humungous.

program Proj1;
{$MODE OBJFPC} {$H+}

const MaxWord = High(word);

type
  TWordStr = array [-1..MaxWord-1] of char;

{ turn two bytes into a word }
function MakeWord(a, b: byte): word;
begin
  Result := (b shl 8) or a;
end;

{ set length of word string }
procedure SetWordStrLen(var ws: twordstr; size: word);
begin
  ws[-1]:= char(lo(size)); // low byte of storage word
  ws[0]:= char(hi(size)); // high byte of storage word
end;

{ get length of word string }
function WordStrLen(const ws: twordstr): word;
begin
  result:= makeword(byte(ws[-1]), byte(ws[0]))
end;

{ concatenate (combine) 2 wordstrings }
procedure WordStrConcat(const ws1: TWordStr; var dest: TWordStr);
begin
  move(pchar(@ws1[1])^, pchar(@dest[WordStrLen(dest)+1])^, WordStrLen(ws1));
end;

var
  wordstr1: twordstr;
  wordstr2: twordstr;
  i: integer; // loop
  sstring: shortstring;
begin
  writeln('The program will prompt you for several tests (hit enter)');
  readln;
  writeln;

  SetWordStrLen(wordstr1, 5); // no heap memory allocation occurs
  wordstr1[1]:= 'h';
  wordstr1[2]:= 'e';
  wordstr1[3]:= 'l';
  wordstr1[4]:= 'l';
  wordstr1[5]:= 'o';
  writeln('Length of WordString1: ', WordStrLen(wordstr1));
  writeln('Actual Memory Size of Word String: ', sizeof(wordstr1));
  writeln('WordString 1 Contents: ', pchar(@WordStr1[1]));
  writeln;

  SetWordStrLen(wordstr2, 7); // no heap memory allocation occurs
  wordstr2[1]:= ' ';
  wordstr2[2]:= 'w';
  wordstr2[3]:= 'o';
  wordstr2[4]:= 'r';
  wordstr2[5]:= 'l';
  wordstr2[6]:= 'd';
  wordstr2[7]:= '!';
  writeln('Length of Word String: ', WordStrLen(wordstr2));
  writeln('Actual Memory Size of Word String: ', sizeof(wordstr2));
  writeln('WordString 2 Contents: ', pchar(@WordStr2[1]));
  WordStrConcat(wordstr2, wordstr1); // no memory allocation occurs
  write('After concatenation of two wordstrings: ');
  writeln(pchar(@wordstr1[1]));

  writeln;
  writeln('Want to see over 65,000 ''a'' characters? (press enter)');
  readln;
  SetWordStrLen(wordstr2, MaxWord - 1); // no memory allocation occurs
  for i:= 1 to MaxWord - 1 do
  begin
    wordstr2[i]:= 'a';
  end;
  // write over 65,000 characters, the maximum capacity of a WordString
  writeln(pchar(@wordstr2[1]));

  { now let's combine a short string with a wordstring }
  writeln;
  writeln('Let''s combine a short string with a wordstring.. (press enter)');
  readln;
  sstring:= ', interesting world.';
  WordStrConcat(sstring, wordstr1); // no memory allocated
  // now write the contents of the new wordstring
  writeln(pchar(@WordStr1[1]));

  writeln;
  writeln('Done (enter to exit)');
  readln;
end.





More information about the fpc-devel mailing list