Skip to content

Commit d332250

Browse files
committed
Optimize codepath when USE_MONITOR_FOR_REFCOUNT is not set, and turn it off for WIN64 (memory / performance tradeof)
1 parent abfd96d commit d332250

1 file changed

Lines changed: 25 additions & 15 deletions

File tree

Source/dwsUtils.pas

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ TInt64DynArrayHelper = record helper for TInt64DynArray
7777
// Uses Monitor hidden field to store refcount, so not compatible with monitor use
7878
// (but Monitor is buggy, so no great loss)
7979
{$ifndef FPC}
80-
{$define USE_MONITOR_FOR_REFCOUNT}
80+
{$ifdef WIN32}
81+
{$define USE_MONITOR_FOR_REFCOUNT}
82+
{$endif}
8183
{$endif}
8284
TRefCountedObject = class
8385
private
@@ -3814,9 +3816,9 @@ function UnicodeCompareText(const s1, s2 : UnicodeString) : Integer;
38143816
ps2 := PWideChar(Pointer(s2));
38153817
if ps1 = ps2 then Exit(0);
38163818
if ps1 <> nil then begin
3819+
n1 := PInteger(NativeUInt(ps1)-4)^;
38173820
if ps2 <> nil then begin
38183821
{$if Defined(WIN64_ASM) or Defined(WIN32_ASM)}
3819-
n1 := PInteger(NativeUInt(ps1)-4)^;
38203822
n2 := PInteger(NativeUInt(ps2)-4)^;
38213823
{$else}
38223824
n1:=Length(s1);
@@ -7079,33 +7081,41 @@ procedure TRefCountedObject.Free;
70797081
// IncRefCount
70807082
//
70817083
function TRefCountedObject.IncRefCount : Integer;
7082-
var
7083-
p : PInteger;
70847084
begin
7085-
{$ifdef FPC}
7086-
p:=@FRefCount;
7085+
{$ifdef USE_MONITOR_FOR_REFCOUNT}
7086+
var p := PInteger(NativeInt(Self)+InstanceSize-hfFieldSize+hfMonitorOffset);
7087+
Result := AtomicIncrement(p^);
70877088
{$else}
7088-
p:=PInteger(NativeInt(Self)+InstanceSize-hfFieldSize+hfMonitorOffset);
7089+
Result := AtomicIncrement(FRefCount);
70897090
{$endif}
7090-
Result := AtomicIncrement(p^);
70917091
end;
70927092

70937093
// DecRefCount
70947094
//
7095+
procedure CallDestroy(obj : TObject);
7096+
begin
7097+
obj.Destroy;
7098+
end;
70957099
function TRefCountedObject.DecRefCount : Integer;
7096-
var
7097-
p : PInteger;
7100+
7101+
{$ifdef USE_MONITOR_FOR_REFCOUNT}
70987102
begin
7099-
{$ifdef FPC}
7100-
p:=@FRefCount;
7101-
{$else}
7102-
p:=PInteger(NativeInt(Self)+InstanceSize-hfFieldSize+hfMonitorOffset);
7103-
{$endif}
7103+
var p := PInteger(NativeInt(Self)+InstanceSize-hfFieldSize+hfMonitorOffset);
71047104
if p^=0 then begin
71057105
Destroy;
71067106
Result:=0;
71077107
end else Result := AtomicDecrement(p^);
71087108
end;
7109+
{$else}
7110+
begin
7111+
if FRefCount = 0 then begin
7112+
Destroy;
7113+
Result := 0;
7114+
end else begin
7115+
Result := AtomicDecrement(FRefCount);
7116+
end;
7117+
end;
7118+
{$endif}
71097119

71107120
// GetRefCount
71117121
//

0 commit comments

Comments
 (0)