Reserving/Preallocating associative array?

bearophile bearophileHUGS at lycos.com
Tue Dec 24 14:39:20 PST 2013


Gordon:

> void main()
> {
>         size_t[size_t] unions;
>         auto f = File("input.txt");
>         foreach ( line ; f.byLine() ) {
>                 auto fields = line.split();
>                 size_t i = to!size_t(fields[0]);
>                 size_t j = to!size_t(fields[1]);
>                 unions[i] = j; // <-- here be question
>         }
> }
> ...
> Is there a way to speed the loading? perhaps reserving memory 
> in the hash before populating it? Or another trick?

Built-in associative arrays don't yet have a "reserve".

Some ides to speed up your code:
- Try to use parse instead of split + to!size_t
- Try to use byLineFast, in the attach here (the code is not 
mine): https://d.puremagic.com/issues/attachment.cgi?id=1305
- Try to disable the GC before the associative array creation and 
re-enable it when it's built. (This could double your max memory 
usage or more). To disable it import GC from core.memory and call 
GC.disable; and GC.enable;

Bye,
bearophile


More information about the Digitalmars-d mailing list