Skip to content

Commit 409cf53

Browse files
committed
optimizations
1 parent 9387f49 commit 409cf53

File tree

3 files changed

+34
-17
lines changed

3 files changed

+34
-17
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ TValueCollection = class(TEnumerableBase<TValue>,
523523
{$ENDREGION}
524524

525525
{$REGION 'Implements IMap<TKey, TValue>'}
526+
procedure Add(const key: TKey; const value: TValue); overload;
526527
function TryAdd(const key: TKey; const value: TValue): Boolean;
527528
function Remove(const key: TKey): Boolean; overload;
528529
function Remove(const key: TKey; const value: TValue): Boolean; overload;
@@ -2340,6 +2341,21 @@ destructor TSortedDictionary<TKey, TValue>.Destroy;
23402341
inherited Destroy;
23412342
end;
23422343

2344+
procedure TSortedDictionary<TKey, TValue>.Add(const key: TKey;
2345+
const value: TValue);
2346+
begin
2347+
if fTree.Add(key, value) then
2348+
begin
2349+
IncUnchecked(fVersion);
2350+
if Assigned(Notify) then
2351+
DoNotify(key, value, caAdded);
2352+
KeyChanged(key, caAdded);
2353+
ValueChanged(value, caAdded);
2354+
end
2355+
else
2356+
raise Error.DuplicateKey;
2357+
end;
2358+
23432359
procedure TSortedDictionary<TKey, TValue>.AddOrSetValue(const key: TKey;
23442360
const value: TValue);
23452361
begin

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

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -118,27 +118,26 @@ function THashTable.Add(const key; hashCode: Integer): Pointer;
118118
entry: THashTableEntry;
119119
begin
120120
entry.HashCode := hashCode;
121-
if not Find(key, entry) then
121+
122+
if Find(key, entry) then
123+
Exit(nil);
124+
125+
if fItemCount = DynArrayLength(fItems) then
122126
begin
123-
if fItemCount = DynArrayLength(fItems) then
124-
begin
125-
Grow;
126-
Find(key, entry);
127-
end;
127+
Grow;
128+
Find(key, entry);
129+
end;
128130

129-
{$Q-}
130-
Inc(fVersion);
131-
{$IFDEF OVERFLOWCHECKS_ON}{$Q+}{$ENDIF}
132-
fBuckets[entry.BucketIndex] := entry.ItemIndex or (entry.HashCode and fBucketHashCodeMask);
131+
{$Q-}
132+
Inc(fVersion);
133+
{$IFDEF OVERFLOWCHECKS_ON}{$Q+}{$ENDIF}
134+
fBuckets[entry.BucketIndex] := entry.ItemIndex or (entry.HashCode and fBucketHashCodeMask);
133135

134-
Result := fItems + entry.ItemIndex * fItemSize;
135-
PInteger(Result)^ := entry.HashCode;
136+
Inc(fCount);
137+
Inc(fItemCount);
136138

137-
Inc(fCount);
138-
Inc(fItemCount);
139-
end
140-
else
141-
Result := nil;
139+
Result := fItems + entry.ItemIndex * fItemSize;
140+
PInteger(Result)^ := entry.HashCode;
142141
end;
143142

144143
function THashTable.AddOrSet(const key; hashCode: Integer;

Source/Base/Spring.pas

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3621,7 +3621,9 @@ function VarArrayLength(const value: Variant; dim: Integer): Integer;
36213621

36223622
function GetVirtualMethod(const classType: TClass; const index: Integer): Pointer;
36233623
begin
3624+
{$Q-}
36243625
Result := PPointer(IntPtr(classType) + IntPtr(index * SizeOf(Pointer)))^;
3626+
{$IFDEF OVERFLOWCHECKS_ON}{$Q+}{$ENDIF}
36253627
end;
36263628

36273629
type

0 commit comments

Comments
 (0)