 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
GEBor Guest
|
Posted: Wed Nov 15, 2006 2:37 am Post subject: Simulated string variables on the stack |
|
|
Strings on stack would be much faster than on heap.
Sometimes you would like to use stack strings, like
in the following case:
if ts.IndexOf('vv$'+s) <> -1 then...
Here it weould be much faster if s - an ordinary string -
could be added to a string allocated on the stack, then
this stack variable could be passed to indexof.
A proof of concept implementation is the following:
const
MaxLen=248;
type
SimString=record
refcnt: Integer;
actlen: Integer;
body: array[1..Maxlen] of char;
end;
TMyStringList=class(TStringList)
public
function IndexOf(var s: SimString): integer; overload;
end;
implementation
function TMyStringList.IndexOf(var s: SimString): integer;
asm
add edx,8 // points to record.body
mov ecx,[eax] // self
jmp dword ptr [ecx+$54] // self.indexof
end;
var
t: TMyStringList;
procedure test4;
var s: SimString;
begin
s.refcnt:=-1;
s.actlen:=5;
s.body[1]:='a';
s.body[2]:='l';
s.body[3]:='m';
s.body[4]:='a';
s.body[5]:=#0;
if t.IndexOf(s) = -1 then
//ShowMessage('Not found')
else
//ShowMessage('Found');
end;
initialization
t:=TMyStringList.Create;
t.Text:='alma'#10'barack'#10'dió';
finalization
t.free;
It is approx. 3 times faster than to create an ordinary
string, and using it to search within the string list:
procedure test3;
var s1: string;
begin
s1:='alma';
if t.IndexOf(s1) = -1 then
//ShowMessage('Not found')
else
//ShowMessage('Found');
end;
Maybe I rediscovered the wheel, but it would be
nice if Borland would support this kind of functionality,
with a "StackString" type.
What do you think? |
|
| Back to top |
|
 |
Aleksandr Guest
|
Posted: Fri Nov 17, 2006 9:44 pm Post subject: Re: Simulated string variables on the stack |
|
|
Hi.
Source below is not for use. Just a joke.
//NOT WELL TESTED
unit ShaLimitedStrings;
interface
function GetMaxLength(var s: string): integer;
procedure SetMaxLength(var s: string; maxlen: integer);
procedure SetLimitedString(var s: string; buffer: pchar; len: integer);
overload;
procedure SetLimitedString(var s: string; const value: string); overload;
implementation
//GetMaxLength returns MaxInt for ANSI strings, 0 for constants and
//maximal length for our Limited strings
function GetMaxLength(const s: string): integer;
begin;
Result:=integer(s);
if Result<>0 then Result:=pInteger(Result- ^;
if Result>=0 then Result:=MaxInt else Result:= -1-Result;
end;
//Use SetMaxLength to convert ANSI string to Limited one
//and to set it's maximal length or to deallocate Limited string
procedure SetMaxLength(var s: string; maxlen: integer);
var
len: integer;
begin;
if pointer(s)<>nil
then if pInteger(integer(s)- ^<-1
then pInteger(integer(s)- ^:=1; //Set refct to 1
if maxlen=0 then s:='' //Deallocate string
else begin; //Limited string
len:=Length(s);
SetLength(s, maxlen);
pInteger(integer(s)- ^:= -1-maxlen;
if len<maxlen then begin;
pInteger(integer(s)-4)^:=len;
pchar(pointer(s))[len]:=#0;
end;
end;
end;
//SetLimitedString sets value of Limited string
procedure SetLimitedString(var s: string; buffer: pchar; len: integer);
overload;
var
maxlen: integer;
begin;
maxlen:=integer(s);
if maxlen<>0 then maxlen:=pInteger(maxlen- ^; //Get refct
if maxlen>=0 then SetString(s,buffer,len) //ANSI string
else begin; //Limited string
maxlen:= -1-maxlen;
if len>maxlen then len:=maxlen;
System.Move(pointer(buffer)^,pointer(s)^,len);
pInteger(integer(s)-4)^:=len;
pchar(pointer(s))[len]:=#0;
end;
end;
//SetLimitedString sets value of Limited string
procedure SetLimitedString(var s: string; const value: string); overload;
begin;
SetLimitedString(s,pointer(value),length(value));
end;
end.
////////
//This works on my D6
procedure TForm1.Button1Click(Sender: TObject);
var
sl: TStringList;
s, t: string;
begin;
sl:=TStringList.Create;
sl.Text:='alma'#10'barack';
SetMaxLength(s,248); //allocate s
SetLimitedString(s,'alma'); //set value
ShowMessage(IntToStr(sl.IndexOf(s)));
SetLimitedString(s,'barack');
ShowMessage(IntToStr(sl.IndexOf(s)));
SetLimitedString(s,'abc');
ShowMessage(IntToStr(sl.IndexOf(s)));
SetMaxLength(s,0); //deallocate s
sl.Free;
end;
--
regards,
Aleksandr |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|