summaryrefslogtreecommitdiff
path: root/src/zisp/gc/IstrSet.zig
blob: c90f5dddd8bd0cd48aaac5ad2c0d6f6075155fe8 (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
//!
//! Efficient hash set for short(ish) strings.
//!
//! Layout: []Bucket
//!
//! * Bucket: [8]Sector
//!
//! * Sector: [8]u8 or u64
//!
//! Strategy: Hybrid between open addressing with linear probing, and chaining,
//! with special markers for indirection; see details below.
//!
//! The Zisp parser interns strings up to 255 bytes in length, using the `Istr`
//! heap representation with a len prefix of one byte and hash cache suffix of
//! eight bytes.  This means that strings of up to 55 bytes of content can fit
//! within 64 bytes.  We store these inline in our buckets.  A pointer to that
//! bucket is then itself the Istr.
//!
//! A bucket can contain multiple strings if they fit.  The strings are aligned
//! at 8 byte "sectors" so a pointer to a sector can also be a valid Istr.
//!
//! A sector may appear empty (all 0 bytes) but actually be part of a string;
//! this is not a problem since strings are length-prefixed.  Linear probing
//! starts at the beginning of a bucket, and jumps over occupied sectors as
//! indicated by a string's length prefix.  This can continue across buckets.
//! When an empty sector is found after jumping over a string, we check if
//! there's enough space within the bucket to hold the string, otherwise we
//! advance into the next bucket and keep probing.
//!
//! Splitting across buckets is not allowed, since this introduces ambiguity:
//! Strings can contain ASCII NUL bytes, so if a string ending in a series of
//! NUL bytes were allowed to spill into the next bucket, it would appear to
//! begin with an empty sector.  A later probe sequence beginning with that
//! bucket would then see it as empty and overwrite part of another string.
//!
//! Since strings up to 6 bytes are packed into NaN, they can never appear here,
//! which means the empty string cannot appear, so a length prefix cannot be 0.
//! Thus there's no ambiguity between empty sector and empty string.
//!
//! A sector can begin with a 0 byte, but have a non-0 value as a u64; this
//! indicates a packed pointer to an Istr elsewhere on the heap.  The low 48
//! bits are the pointer, while the high bits encode the length of the string
//! being pointed to, so we don't need to follow the pointer for the equality
//! check in case the lengths are different.  Also, in this case, the cached
//! hash follows directly in the next sector, so we also needn't follow the
//! pointer when resizing the hash set.
//!

/// Bucket count
bcount: usize = 512, // 512

used_bytes: usize,
total_bytes: usize,