Associative array of strings perf

bearophile bearophileHUGS at lycos.com
Sat Nov 19 01:04:51 PST 2011


Daniel Murphy:

> Maybe different hash functions are used?

I presume that something somewhere uses some different function of a different part of it, but then one of those functions has a significant amount of space left for improvement.

The __Dmain produced by the two versions of the program are exactly the same, minus some names, they call the same runtime functions like __aaInX and the other functions too visible with obj2asm are the same.

---------------

Alex R. Petersen:

> With an enum, I assume the hashes can be precomputed.

This program doesn't use enum/chars but dynamic array of enums/chars, so you can't precompute (it uses only 500 different strings, so again it can precompute the hash of them all, but this just forces me to create a more complex benchmark).


Regarding hashing of single enums, often enough (from ObjectPascal usage) I'd like to use arrays with a small group of named indexes. D allows you to write this, but it defines an associative array, this is a waste of memory and performance:


enum Foo : size_t { A, B, C, D }
void main() {
    int[Foo] array;
    array[Foo.D] = 5;
    // array[2] = 1; // statically rejected
}


This alternative code works, but you lose all strong static typing:

enum : size_t { A, B, C, D }
void main() {
    int[4] array;
    array[D] = 5;
    array[2] = 1; // accepted
}

Bye,
bearophile


More information about the Digitalmars-d mailing list