It is not an issue, it is the cost benefit of higher performance x less memory. But the optimization of creating it with the capacity is valid considering that the performance increases is on average 30%, both for small collections and for large ones.
uses
System.SysUtils, System.Generics.Collections, System.Diagnostics, FMX.Dialogs;
procedure TestWithInitialCapacity;
var
I, J, LNewId: Integer;
LDictionary: TDictionary<Integer, Boolean>;
LStopwatch: TStopwatch;
begin
LStopwatch := TStopwatch.StartNew;
for I := 0 to 10000 do begin
LDictionary := TDictionary<Integer, Boolean>.Create(200);
for J := 0 to 100 do begin
repeat
LNewId := Random(High(Integer));
until not LDictionary.ContainsKey(LNewId);
LDictionary.Add(LNewId, True);
end;
FreeAndNil(LDictionary);
end;
showmessage(Format('Test with initial capacity: %g', [LStopwatch.Elapsed.TotalMilliseconds]));
end;
procedure TestWithoutInitialCapacity;
var
I, J, LNewId: Integer;
LDictionary: TDictionary<Integer, Boolean>;
LStopwatch: TStopwatch;
begin
LStopwatch := TStopwatch.StartNew;
for I := 0 to 10000 do begin
LDictionary := TDictionary<Integer, Boolean>.Create;
for J := 0 to 100 do begin
repeat
LNewId := Random(High(Integer));
until not LDictionary.ContainsKey(LNewId);
LDictionary.Add(LNewId, True);
end;
FreeAndNil(LDictionary);
end;
showmessage(Format('Test without initial capacity: %g', [LStopwatch.Elapsed.TotalMilliseconds]));
end;