Compiler enhancement proposal: threadlocalvar
By A.Bouchez on 2010, Friday July 30, 11:25 - Pascal Programing - Permalink
As I wrote in a previous post, Delphi string, dynamic array and memory manager don't like multi-core CPU.
My proposal is to add a threadlocalvar keyword, to be used instead of var in your code, to mark some variables to be used in only the current thread. Then the compiler and RTL won't have to use the LOCK instruction, and the application will be MUCH faster in multi-thread environment.
I made this proposal in both the Embarcadero forum and in the FreePascal forum.
And what about using a threadlocalvar new reserved word, in a threadvar way, which could define the variable (string, dynamic array) to be accessed by the current thread only, and won't have any LOCK call?
This could be something like that:
function TServer.ComputePage: string; threadlocalvar tmp: string; // differs from var tmp: string i: integer; // i is defined as threadlocalvar, but is the same as normal var begin for i := 0 to high(ListStr) do // ListStr[] is a dynamic array tmp := tmp+ListStr[i]+#13#10; // very fast computation, without LOCK result := tmp; // copy from local heap to global heap end;
function _LStrAddRef(var str): Pointer;
var P: PStrRec;
begin
if Integer(str)<>0 then begin
P := Pointer(Integer(str) - sizeof(StrRec));
if P.refcnt >= 0 then
InterlockedIncrement(P.refcnt) else // slow multi-thread safe reference count
if P.refcnt < -1 then // -1 for const
dec(P.refcnt); // -2,-3... for threadlocalvar
end;
Result := Pointer(str);
end;
And a local threadheap should be implemented for such threadlocalvar, with threadlocalgetmem() and such functions.
Another possibility should be do add a "threadlocal" attribute for types, to be used for local variables and properties:
TServer = class public ListStr: threadlocal array of string; ...In this case, the method above should begin with:
function TServer.ComputePage: string; var tmp: threadlocal string; // differs from var tmp: string ....But this syntax sound a bit not "pascalish", whereas the threadlocalvar does (it sounds like the treadvar feature) ... another idea?