Skip to content

Commit cc264ad

Browse files
committed
optimization for storing ownsObjects in most significant bit of fCount field
1 parent abe02a8 commit cc264ad

File tree

3 files changed

+9
-8
lines changed

3 files changed

+9
-8
lines changed

Source/Base/Collections/Spring.Collections.Base.pas

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -640,8 +640,9 @@ TArrayHelper = record
640640
end;
641641

642642
const
643-
CountMask = Integer($7FFFFFFF);
644-
BitMask: array[Boolean] of Integer = (0, not CountMask);
643+
OwnsObjectsBitIndex = 31;
644+
OwnsObjectsMask = 1 shl OwnsObjectsBitIndex;
645+
CountMask = not OwnsObjectsMask;
645646

646647
// use the MSB of the HashCode to note removed items
647648
RemovedFlag = Integer($80000000);
@@ -2462,7 +2463,7 @@ procedure TCircularArrayBuffer<T>.SetCapacity(value: Integer);
24622463

24632464
procedure TCircularArrayBuffer<T>.SetOwnsObjects(value: Boolean);
24642465
begin
2465-
fCount := (fCount and CountMask) or BitMask[value];
2466+
fCount := (fCount and CountMask) or (Ord(value) shl OwnsObjectsBitIndex);
24662467
end;
24672468

24682469
{$ENDREGION}

Source/Base/Collections/Spring.Collections.Lists.pas

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ TEnumerator = class(TRefCountedObject, IEnumerator<T>)
9494
procedure SetCapacity(value: Integer);
9595
procedure SetCount(value: Integer);
9696
procedure SetItem(index: Integer; const value: T);
97-
procedure SetOwnsObjects(value: Boolean);
97+
procedure SetOwnsObjects(value: Boolean); inline;
9898
{$ENDREGION}
9999

100100
function TryGetElementAt(var value: T; index: Integer): Boolean;
@@ -496,7 +496,7 @@ function TAbstractArrayList<T>.GetRange(index, count: Integer): IList<T>;
496496

497497
Result := CreateList;
498498
list := TList<T>(Result.AsObject);
499-
list.fCount := (list.fCount and not CountMask) or count;
499+
list.fCount := (list.fCount and OwnsObjectsMask) or count;
500500
{$IFDEF DELPHIXE2_UP}
501501
list.fItems := Copy(fItems, index, count);
502502
{$ELSE}
@@ -592,7 +592,7 @@ procedure TAbstractArrayList<T>.SetItemInternal(index: Integer; const value: T);
592592
procedure TAbstractArrayList<T>.SetOwnsObjects(value: Boolean);
593593
begin
594594
if TType.Kind<T> = tkClass then
595-
fCount := (fCount and CountMask) or BitMask[value];
595+
fCount := (fCount and CountMask) or (Ord(value) shl OwnsObjectsBitIndex);
596596
end;
597597

598598
function TAbstractArrayList<T>.Single: T;
@@ -1102,7 +1102,7 @@ procedure TAbstractArrayList<T>.SetCount(value: Integer);
11021102
SetCapacity(value);
11031103
if value < Count then
11041104
DeleteRange(value, Count - value);
1105-
fCount := (fCount and not CountMask) or value;
1105+
fCount := (fCount and OwnsObjectsMask) or value;
11061106
end;
11071107

11081108
procedure TAbstractArrayList<T>.Delete(index: Integer);

Source/Base/Collections/Spring.Collections.Stacks.pas

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ procedure TAbstractStack<T>.SetCapacity(value: Integer);
318318
procedure TAbstractStack<T>.SetOwnsObjects(const value: Boolean);
319319
begin
320320
if GetElementType.Kind = tkClass then
321-
fCount := (fCount and CountMask) or BitMask[value];
321+
fCount := (fCount and CountMask) or (Ord(value) shl OwnsObjectsBitIndex);
322322
end;
323323

324324
procedure TAbstractStack<T>.TrimExcess;

0 commit comments

Comments
 (0)