Thread overview
lookuptable
Apr 22, 2020
Sebastiaan Koppe
Apr 22, 2020
Sebastiaan Koppe
Apr 23, 2020
Sebastiaan Koppe
April 22, 2020
Just wanted to throw this out there on a slow day. I wrote this little utility to generate static lookup tables, because using an AA is too expensive for something like e.g. looking up database row data by column name.

It's very crude, but very short and sweet. Hopefully people find a use for it.

Let me know if you have any requests or find any bugs on github.

https://code.dlang.org/packages/lookuptable

-Steve
April 22, 2020
On Wednesday, 22 April 2020 at 20:27:12 UTC, Steven Schveighoffer wrote:
> Just wanted to throw this out there on a slow day. I wrote this little utility to generate static lookup tables, because using an AA is too expensive for something like e.g. looking up database row data by column name.
>
> It's very crude, but very short and sweet. Hopefully people find a use for it.
>
> Let me know if you have any requests or find any bugs on github.
>
> https://code.dlang.org/packages/lookuptable
>
> -Steve

Nice.

For the indexLookup you can also use https://github.com/skoppe/perfect-hash
April 22, 2020
On Wednesday, 22 April 2020 at 21:21:31 UTC, Sebastiaan Koppe wrote:
> For the indexLookup you can also use https://github.com/skoppe/perfect-hash

That is not quite true, only for strings.

April 23, 2020
On 4/22/20 5:21 PM, Sebastiaan Koppe wrote:
> On Wednesday, 22 April 2020 at 20:27:12 UTC, Steven Schveighoffer wrote:
>> Just wanted to throw this out there on a slow day. I wrote this little utility to generate static lookup tables, because using an AA is too expensive for something like e.g. looking up database row data by column name.
>>
>> It's very crude, but very short and sweet. Hopefully people find a use for it.
>>
>> Let me know if you have any requests or find any bugs on github.
>>
>> https://code.dlang.org/packages/lookuptable
>>
> 
> Nice.
> 
> For the indexLookup you can also use https://github.com/skoppe/perfect-hash

Hm... thanks for the suggestion. I'm not sure if it fits here, as the point is to avoid runtime cost and GC allocation, not make lookups uber-fast.

These are meant to be short-lived things. My main target was e.g. mysql-native has an "asAA" function which generates an AA with keys being the column names. This is kind of crappy, because if you do that for each row, then you are generating and throwing away a LOT of data.

But if you generate the lookup index once per sequence, and you reuse that, you have much more efficiency, and are only generating one array. Possibly if I can figure out a good API, you could do it with malloc/free instead of using GC, but I'm happy with it right now anyway.

-Steve
April 23, 2020
On Thursday, 23 April 2020 at 04:29:12 UTC, Steven Schveighoffer wrote:
> Hm... thanks for the suggestion. I'm not sure if it fits here, as the point is to avoid runtime cost and GC allocation, not make lookups uber-fast.

Granted, it was far-fetched.

> These are meant to be short-lived things. My main target was e.g. mysql-native has an "asAA" function which generates an AA with keys being the column names. This is kind of crappy, because if you do that for each row, then you are generating and throwing away a LOT of data.

Yeah, that is quite crappy. I remember mapping each row to a struct and use member access, but then you have to sync the struct and the query yourself.

It doesn't have to be short-lived though. Most apps process the same set of queries, so pre-creating all your prepared statements and mappings might make sense. I am not sure about mysql, but postgres gives you back the columns and types after preparing a statement, so you could do some runtime mapping once at startup (and at each reconnect).