Skip to content

Commit 183e254

Browse files
committed
avoid unnecessary reference counting for default comparers
1 parent a974af9 commit 183e254

File tree

6 files changed

+98
-116
lines changed

6 files changed

+98
-116
lines changed

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,10 +1087,10 @@ function TEnumerableBase<T>.Concat(const second: IEnumerable<T>): IEnumerable<T>
10871087

10881088
function TEnumerableBase<T>.Contains(const value: T): Boolean;
10891089
var
1090-
comparer: IEqualityComparer<T>;
1090+
comparer: Pointer;
10911091
begin
1092-
comparer := IEqualityComparer<T>(_LookupVtableInfo(giEqualityComparer, GetElementType, SizeOf(T)));
1093-
Result := IEnumerable<T>(this).Contains(value, comparer);
1092+
comparer := _LookupVtableInfo(giEqualityComparer, GetElementType, SizeOf(T));
1093+
Result := IEnumerable<T>(this).Contains(value, IEqualityComparer<T>(comparer));
10941094
end;
10951095

10961096
function TEnumerableBase<T>.Contains(const value: T;
@@ -1165,15 +1165,15 @@ function TEnumerableBase<T>.EqualsTo(const values: array of T): Boolean;
11651165
ExitFalse;
11661166
var
11671167
enumerator: IEnumerator<T>;
1168-
comparer: IEqualityComparer<T>;
1168+
comparer: Pointer;
11691169
i: Integer;
11701170
{$IFDEF RSP31615}
11711171
item: T;
11721172
{$ENDIF}
11731173
begin
11741174
i := 0;
11751175
enumerator := IEnumerable<T>(this).GetEnumerator;
1176-
comparer := IEqualityComparer<T>(_LookupVtableInfo(giEqualityComparer, GetElementType, SizeOf(T)));
1176+
comparer := _LookupVtableInfo(giEqualityComparer, GetElementType, SizeOf(T));
11771177
while enumerator.MoveNext do
11781178
begin
11791179
{$IFDEF RSP31615}
@@ -1187,7 +1187,7 @@ function TEnumerableBase<T>.EqualsTo(const values: array of T): Boolean;
11871187
end
11881188
else
11891189
{$ENDIF}
1190-
if (i > High(values)) or not comparer.Equals(enumerator.Current, values[i]) then
1190+
if (i > High(values)) or not IEqualityComparer<T>(comparer).Equals(enumerator.Current, values[i]) then
11911191
goto ExitFalse;
11921192
Inc(i);
11931193
end;
@@ -1198,13 +1198,13 @@ function TEnumerableBase<T>.EqualsTo(const values: array of T): Boolean;
11981198

11991199
function TEnumerableBase<T>.EqualsTo(const values: IEnumerable<T>): Boolean;
12001200
var
1201-
comparer: IEqualityComparer<T>;
1201+
comparer: Pointer;
12021202
begin
12031203
Result := IInterface(this) = values;
12041204
if not Result then
12051205
begin
1206-
comparer := IEqualityComparer<T>(_LookupVtableInfo(giEqualityComparer, GetElementType, SizeOf(T)));
1207-
Result := IEnumerable<T>(this).EqualsTo(values, comparer);
1206+
comparer := _LookupVtableInfo(giEqualityComparer, GetElementType, SizeOf(T));
1207+
Result := IEnumerable<T>(this).EqualsTo(values, IEqualityComparer<T>(comparer));
12081208
end;
12091209
end;
12101210

@@ -3844,14 +3844,14 @@ function TArrayIterator<T>.IndexOf(const item: T; index: Integer): Integer;
38443844

38453845
function TArrayIterator<T>.IndexOf(const item: T; index, count: Integer): Integer;
38463846
var
3847-
comparer: IEqualityComparer<T>;
3847+
comparer: Pointer;
38483848
i: Integer;
38493849
begin
38503850
CheckRange(index, count, fCount);
38513851

3852-
comparer := IEqualityComparer<T>(_LookupVtableInfo(giEqualityComparer, GetElementType, SizeOf(T)));
3852+
comparer := _LookupVtableInfo(giEqualityComparer, GetElementType, SizeOf(T));
38533853
for i := index to index + count - 1 do
3854-
if comparer.Equals(fItems[i], item) then
3854+
if IEqualityComparer<T>(comparer).Equals(fItems[i], item) then
38553855
Exit(i);
38563856
Result := -1;
38573857
end;

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -271,14 +271,14 @@ function TLinkedList<T>.Extract(const item: T): T;
271271
function TLinkedList<T>.Find(const value: T): TLinkedListNode<T>;
272272
var
273273
node: TLinkedListNode<T>;
274-
comparer: IEqualityComparer<T>;
274+
comparer: Pointer;
275275
begin
276276
Result := nil;
277277
node := fHead;
278278
if Assigned(node) then
279279
begin
280-
comparer := IEqualityComparer<T>(_LookupVtableInfo(giEqualityComparer, TypeInfo(T), SizeOf(T)));
281-
while not comparer.Equals(node.fItem, value) do
280+
comparer := _LookupVtableInfo(giEqualityComparer, TypeInfo(T), SizeOf(T));
281+
while not IEqualityComparer<T>(comparer).Equals(node.fItem, value) do
282282
begin
283283
node := node.fNext;
284284
if node = fHead then
@@ -291,16 +291,16 @@ function TLinkedList<T>.Find(const value: T): TLinkedListNode<T>;
291291
function TLinkedList<T>.FindLast(const value: T): TLinkedListNode<T>;
292292
var
293293
node1, node2: TLinkedListNode<T>;
294-
comparer: IEqualityComparer<T>;
294+
comparer: Pointer;
295295
begin
296296
if not Assigned(fHead) then
297297
Exit(nil);
298298
node1 := fHead.fPrev;
299299
node2 := node1;
300300
if Assigned(node2) then
301301
begin
302-
comparer := IEqualityComparer<T>(_LookupVtableInfo(giEqualityComparer, TypeInfo(T), SizeOf(T)));
303-
while not comparer.Equals(node2.fItem, value) do
302+
comparer := _LookupVtableInfo(giEqualityComparer, TypeInfo(T), SizeOf(T));
303+
while not IEqualityComparer<T>(comparer).Equals(node2.fItem, value) do
304304
begin
305305
node2 := node2.fPrev;
306306
if node2 = node1 then

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2270,12 +2270,12 @@ function TAnonymousReadOnlyList<T>.IndexOf(const item: T;
22702270
function TAnonymousReadOnlyList<T>.IndexOf(const item: T; index,
22712271
count: Integer): Integer;
22722272
var
2273-
comparer: IEqualityComparer<T>;
2273+
comparer: Pointer;
22742274
i: Integer;
22752275
begin
2276-
comparer := IEqualityComparer<T>(_LookupVtableInfo(giEqualityComparer, TypeInfo(T), SizeOf(T)));
2276+
comparer := _LookupVtableInfo(giEqualityComparer, TypeInfo(T), SizeOf(T));
22772277
for i := index to index + count - 1 do
2278-
if Comparer.Equals(fItems(i), item) then
2278+
if IEqualityComparer<T>(comparer).Equals(fItems(i), item) then
22792279
Exit(i);
22802280
Result := -1;
22812281
end;

0 commit comments

Comments
 (0)