Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Allowing NULL Keys in Collections

Moderators: kris

Posted: 09/04/07 22:54:09

Quick background:

Writing up a relatively complex configuration system which, in previous iterations, made heavy use of associative arrays to hold 'profiled' settings. It's really a lot more convoluted than this but to break it down, each config setting was stored as an associative array like this:


I have since swapped this all to use the Tango HashMap?, the reason being that the config system use-case is quite heavy on config gets, and the HaspMap? seems to display much better 'get' performance:

Averages over 10 iterations, using StopWatch? for timings, ~60,000 entries and ~6million gets of those entries: Associative Arrays: Set: 7.99s, Get: 99.3s. HashMap?: Set: 17.2s, Get: 37.3s.

The kicker here is that <profile> can be (and often is) Null (No profile, considered 'default' setting). Associative arrays allow Null keys and this worked fine, however when I switched to HashMap? I would get:

"Attempt to include invalid key _in Collection" (Verbatim, I think the underscore is a typo in the source).

So, I wrote a Q&D fix in my code, that if the key was null then the key I passed into the HaspMap? was "". (Ugly, I know).

Today, I changed the isValidKey function in MapCollection? to just return true (all it did was return false if the key was inherited from Object and was Null), removed my Q&D hack, and ran all my tests. The code still seems to work fine, and I re-timed the same test again, with somewhat surprising results:

HaspMap? (Null Keys): Set: 15.74s, Get: 31.76s.

So.. after all that long winded background, my question is:

Can Collections support Null keys? Please? :)

Author Message

Posted: 09/04/07 23:45:13

I had a feeling that there's some comparison going on, which would fail with null-objects? Can't recall where exactly, but thought I'd mention it