2

I just read

"Any class that implements the IEqualityComparer interface is expected to provide the implementation for the Equals method." - (Delphi DocWiki)

and

"Any class that implements the IEqualityComparer interface is expected to provide the implementation for the GetHashCode method." - (Delphi DocWiki)

How will performance of a TDictionary be if I create a TDictionary<TObject, TObject> and do not implement a IEqualityComparer?

I have not found a default implementation (in Delphi 2009). So how will the hash code for they keys be calculated?

If it is simply the memory address of the object in the Dictionary entries key, will a search for a given entry be performed in sequential order?

mjn
  • 36,362
  • 28
  • 176
  • 378

1 Answers1

5

The default implementation will perform extremely well with a TObject key. Equality is defined to be object identity, the same as testing if A=B. The hash is simply the address of the reference – it could not be more efficient.

The code looks like this:

function TObject.Equals(Obj: TObject): Boolean;
begin
  Result := Obj = Self;
end;

function TObject.GetHashCode: Integer;
begin
  Result := Integer(Self);
end;

Looking up in a hashed dictionary does not involve searching. It is a highly efficient O(1) operation. I think you should take a read of the Wikipedia article.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Delphi could have added Equals and GetHashCode as new methods in the TObject class instead of introducing the IEqualityComparer interface, maybe – mjn Mar 15 '11 at 17:27
  • @mjn Not everything that can be put in a TDictionary descends from TObject, so to force you to use TObject would have crippled it in my view. Could not use strings, GUIDs etc. – David Heffernan Mar 15 '11 at 17:28
  • Oh, I forgot - right, there is no String, or GUID class in Delphi. – mjn Mar 15 '11 at 17:32
  • What does "Looking up in a hashed dictionary does not involve searching." mean? The article in Wikipedia mentions content addressable memory ... – mjn Mar 15 '11 at 17:37
  • @mjn It's all there in that article. You asked if a lookup involved sequential search. It doesn't. It's an O(1) operation. For linear search the expected time to retrieve a value increases as the container size increases. That is not the case for a hash. – David Heffernan Mar 15 '11 at 17:40
  • Doesn't it depend on the quality of the hash function? (reading http://en.wikipedia.org/wiki/Hash_table) – mjn Mar 15 '11 at 17:49
  • @mjn Hash function for TObject cannot really be improved upon. – David Heffernan Mar 15 '11 at 17:51
  • Now I understand :) TObject was an example - actually I meant any TObject subclass (no basic data types like string) – mjn Mar 15 '11 at 17:57
  • This trick is not going to work with 64bit Delphi. Maybe we'll have NativeInt sized hashes there? – Wouter van Nifterick May 13 '11 at 13:10
  • @WoutervanNifterick For 64 bit the `GetHashCode` is implemented as `Result := Integer(IntPtr(Self)) xor Integer(IntPtr(Self) shr 32);` not quite as efficient as the 32 bit code - but still pretty fast. – Alister May 27 '15 at 01:42