#Solved: Unreachable StringHashMap

1 messages · Page 1 of 1 (latest)

winter laurel
#

send code?

drowsy mortar
#

One sec

drowsy mortar
#

Like how does it go from put to putAssumeCapacityNoClobberContext. I assume this is an error within stdlib and some things were mixed up.

#

Potential bug in StringHashMap

#

I'll try to reproduce this error.

drowsy mortar
#

YAAAS!!

#

Sorry for all my confusion and especially deleting the original message. I am just lost and got all weird from all the debugging.

#

At least now I have been able to reproduce this issue with a small LoC count.

#
const std = @import("std");
const Sha256 = std.crypto.hash.sha3.Sha3_256;

pub fn main() !void {
    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer std.debug.assert(gpa.deinit() == .ok);
    const allocator = gpa.allocator();

    var random_data: [1000000]u8 = undefined;
    std.crypto.random.bytes(&random_data);

    var hm = std.StringHashMap([]const u8).init(allocator);
    defer hm.deinit();
    defer hm.clearAndFree();

    for ([_][2]usize{
        .{ 0, 10000 },
        .{ 10000, 20000 },
        .{ 20000, 30000 },
        .{ 30000, 40000 },
        .{ 40000, 50000 },
        .{ 50000, 60000 },
        .{ 60000, 70000 },
    }) |i| {
        std.debug.print("{d} - {d}\n", .{ hm.capacity(), hm.unmanaged.available });

        var sha256_hash: [Sha256.digest_length]u8 = undefined;
        Sha256.hash(random_data[i[0]..i[1]], &sha256_hash, .{});

        try hm.put(&sha256_hash, random_data[i[0]..i[1]]);
    }
}
#

Again, and because I deleted the first message the traceback is calling a NoClobber method, although I initially called just put. Maybe I just don't understand the terminology or something else is at fault, but if this happens to be an stdlib bug then I'd be more than happy to open an issue or PR.

#

Here is the traceback.

#

Tried both master and 0.11.0 same issue

drowsy mortar
drowsy mortar
#

Nevermind, I think I solved the issue:
Builtin hashmap for strings as keys. Key memory is managed by the caller.
Placing an inline up front or duping the keys solves the issue.

#

Solved: Unreachable StringHashMap