summaryrefslogtreecommitdiff
path: root/src/zisp/gc/IstrSet.zig
blob: 900caa7b0a91aa1ea655309fc2dc3ccb254f9a2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const std = @import("std");

const Alloc = std.mem.Allocator;

const value = @import("../value.zig");

const IstrPtr = value.istr.IstrPtr;
const IstrHead = value.istr.IstrHead;

const Set = @This();

const max_fill_percent = 80;

const Bucket = packed struct(u64) {
    ptr: u48 = 0,
    fp: u16 = 0,

    pub fn empty(self: Bucket) bool {
        return self.ptr == 0;
    }

    pub fn istrPtr(self: *const Bucket) IstrPtr {
        return @ptrFromInt(self.ptr);
    }

    pub fn putIstrPtr(self: *Bucket, istr: IstrPtr) void {
        self.ptr = @intCast(@intFromPtr(istr));
    }
};

alloc: Alloc,
buckets: []Bucket = undefined,

used_buckets: usize = 0,
used_threshold: usize = undefined,

const test_stdlib_impl = false;

const default_bcount = 512;

pub fn init(alloc: Alloc) !Set {
    return initCustom(alloc, default_bcount);
}

pub fn initCustom(alloc: Alloc, bcount: usize) !Set {
    if (test_stdlib_impl) {
        try map.ensureTotalCapacity(alloc, 512);
        return Set{ .alloc = alloc };
    }
    std.debug.assert(@popCount(bcount) == 1); // Must be power of 2.
    var self = Set{ .alloc = alloc };
    try self.allocBuckets(bcount);
    return self;
}

fn allocBuckets(self: *Set, bcount: usize) !void {
    self.buckets = try self.alloc.alloc(Bucket, bcount);
    @memset(self.buckets, Bucket{});
    self.used_threshold = bcount * max_fill_percent / 100;
}

pub fn deinit(self: *Set) void {
    self.alloc.free(self.buckets);
}

/// Get the istr with the given string contents, or alloc and store a new one.
pub fn getOrNew(self: *Set, s: []const u8) !IstrPtr {
    std.debug.assert(s.len <= value.istr.max_len);
    if (test_stdlib_impl) {
        return self.addStdlib(s);
    }
    return self.getOrPutOrNew(s, null);
}

/// Get the canonical istr with the same contents, or store this one as it.
pub fn getOrPut(self: *Set, istr: IstrPtr) !IstrPtr {
    return self.getOrPutOrNew(istr.bytes(), istr);
}

fn getOrPutOrNew(self: *Set, s: []const u8, ptr: ?IstrPtr) !IstrPtr {
    if (self.used_buckets > self.used_threshold) {
        try self.resize();
    }

    const hash = strHash(s);
    const fp = hashFp(hash);

    const idx_mask = self.buckets.len - 1;
    const idx_start = hash & idx_mask;

    var idx = idx_start;
    while (true) : (idx = (idx + 1) & idx_mask) {
        const bucket = &self.buckets[idx];
        if (bucket.empty()) {
            self.used_buckets += 1;
            const istr = ptr orelse try value.istr.new(self.alloc, s);
            bucket.putIstrPtr(istr);
            bucket.fp = fp;
            return istr;
        }
        if (bucket.fp == fp) {
            const istr = bucket.istrPtr();
            if (strEq(s, istr.bytes())) return istr;
        }
    }
}

fn resize(self: *Set) !void {
    const old_buckets = self.buckets;
    try self.allocBuckets(old_buckets.len << 1);
    defer self.alloc.free(old_buckets);
    const new_buckets = self.buckets;

    const idx_mask = new_buckets.len - 1;
    for (old_buckets) |old| {
        if (old.empty()) continue;
        const str = old.istrPtr().bytes();
        var idx = strHash(str) & idx_mask;
        while (!new_buckets[idx].empty()) idx = (idx + 1) & idx_mask;
        new_buckets[idx] = old;
    }
}

fn strHash(s: []const u8) u64 {
    return std.hash_map.hashString(s);
}

fn strEq(s: []const u8, s2: []const u8) bool {
    return std.hash_map.eqlString(s, s2);
}

fn hashFp(hash: u64) u16 {
    return @intCast(hash >> 48);
}

// Using stdlib, to compare

const str_ctx = std.hash_map.StringContext{};

const Map = std.hash_map.StringHashMapUnmanaged(IstrPtr);
var map: Map = .empty;

pub fn addStdlib(self: *Set, s: []const u8) !IstrPtr {
    const gop = map.getOrPutAdapted(self.alloc, s, str_ctx) catch @panic("OOM");
    if (gop.found_existing) {
        return gop.value_ptr.*;
    }

    const istr = try self.newIstr(s);
    gop.key_ptr.* = istr.bytes();
    gop.value_ptr.* = istr;
    return istr;
}