#is `std.AutoArrayHashMapUnmanaged(void, void)` basically an id generator?

1 messages · Page 1 of 1 (latest)

fast galleon
#

Was try to understand how the /aro/backend/Interner.zig file used it

    const adapter: KeyAdapter = .{ .interner = i };
    const gop = try i.map.getOrPutAdapted(gpa, key, adapter);
    if (gop.found_existing) return @enumFromInt(gop.index);
    try i.items.ensureUnusedCapacity(gpa, 1);

Looking at the language ref for void in hash maps is says all the code that deals with storing and loading voids is deleted.

https://ziglang.org/documentation/master/#void

By using void as the type of the value, the hash map entry type has no value field, and thus the hash map takes up less space. Further, all the code that deals with storing and loading the value is deleted, as seen above.

src/codegen/c/Type.zig also uses one:


        const source_info = source_ctype.info(source_pool);
        const gop = pool.map.getOrPutAssumeCapacityAdapted(source_ctype, CTypeAdapter{
            .pool = pool,
            .source_pool = source_pool,
            .source_info = source_info,
            .pool_adapter = pool_adapter,
        });
        errdefer _ = pool.map.pop();
        const ctype = CType.fromPoolIndex(gop.index);

Looking at the implementation of getOrPutAssumeCapacityAdapted
https://github.com/ziglang/zig/blob/master/lib/std/hash_map.zig#L1360

It creates a hash of the key:

const hash = ctx.hash(key);

Which is verified to match the type specified at comptime, but that doesn't seem to trigger the error that's in the code because the type is void but void is not passed in? Is that because of the " all the code that deals with storing and loading the value is deleted" nature of void? It looks like if does get past the error it is stored as metadata and the rest of it seems to make sense.

It looks like it's being used as an id generator of sort to me, but maybe I misunderstand what's happening.

I'm just curious, trying to understand what this part of the code base is doing.

stiff perch
#

Using void as the value type means there are no values stored, which for a HashMap is a set, but for an ArrayHashMap is a mapping from key to the index in the stored ordering, which allows you to store the values externally in a different data structure.

#

Using void as the key type generally makes the non-adapted methods not useful, but you can use the adapted variants to lookup keys with external storage.

#

Adapted means that const hash = ctx.hash(key); is using the adapted context to hash the adapted key, which is not a void value.

scarlet breach
#

To summarize, a void key lets you store the value elsewhere, or even compute it on the fly, using the ...Adapted API functions