Reserving/Preallocating associative array?

Benjamin Thaut code at benjamin-thaut.de
Fri Dec 27 15:42:04 PST 2013


Am 27.12.2013 20:55, schrieb Gordon:
> On Friday, 27 December 2013 at 17:26:42 UTC, Benjamin Thaut wrote:
>> Also, gordon whats is keeping you from converting the textual
>> representation to a binary one? I mean, if you need to read that file
>> in often, you can easly preprocess it into a binary blob. You could
>> even modify my hashmap implementation to support binary serialization,
>> by just writing the entire array to a binary file and loading it again.
>> That would be fast...
>
> This is intended to be a general-purpose CLI program to handle certain
> text files (from other sources), not a in-house application where I can
> restructure my data.
>
> Text files are still the most ubiquitous way to share data (especially
> without limiting the languages/tools other will use to process them).
>
> But out of curiosity - how would you serialize it?

Given my hashmap implementation I would do something like

// method of hashmap
void serializeTo(FILE* f)
{
   fwrite(&m_FullCount, typeof(m_FullCount).sizeof, 1, f);
   ulong len = m_Data.length;
   fwrite(&len, typeof(len).sizeof, 1, f);
   fwrite(m_Data.ptr, Pair.sizeof, len, f);
}

// construct from file
void this(FILE* f)
{
   fread(&m_FullCount, typeof(m_FullCount).sizeof, 1, f);
   ulong len;
   fread(&len, typeof(len).sizeof, 1, f);
   m_Data = (cast(Pair*)malloc(Pair.sizeof * len))[0..len];
   fread(m_Data.ptr, Pair.sizeof, len, f);
}

The deserialization would be just one allocation and a big binary read 
from a file, which should be quite fast.

Kind Regards
Benjamin Thaut


More information about the Digitalmars-d mailing list