RefCountedSet: use usize for cap to allow up to max Id+1

This commit is contained in:
Qwerasd
2024-06-24 20:30:37 -04:00
committed by Mitchell Hashimoto
parent a7e6f1a070
commit 6f732cca55
3 changed files with 16 additions and 15 deletions

View File

@ -1845,7 +1845,7 @@ pub const AdjustCapacity = struct {
/// Adjust the number of styles in the page. This may be /// Adjust the number of styles in the page. This may be
/// rounded up if necessary to fit alignment requirements, /// rounded up if necessary to fit alignment requirements,
/// but it will never be rounded down. /// but it will never be rounded down.
styles: ?u16 = null, styles: ?usize = null,
/// Adjust the number of available grapheme bytes in the page. /// Adjust the number of available grapheme bytes in the page.
grapheme_bytes: ?usize = null, grapheme_bytes: ?usize = null,
@ -1877,7 +1877,7 @@ pub fn adjustCapacity(
var cap = page.data.capacity; var cap = page.data.capacity;
if (adjustment.styles) |v| { if (adjustment.styles) |v| {
const aligned = try std.math.ceilPowerOfTwo(u16, v); const aligned = try std.math.ceilPowerOfTwo(usize, v);
cap.styles = @max(cap.styles, aligned); cap.styles = @max(cap.styles, aligned);
} }
if (adjustment.grapheme_bytes) |v| { if (adjustment.grapheme_bytes) |v| {

View File

@ -1027,7 +1027,7 @@ pub const Capacity = struct {
rows: size.CellCountInt, rows: size.CellCountInt,
/// Number of unique styles that can be used on this page. /// Number of unique styles that can be used on this page.
styles: u16 = 16, styles: usize = 16,
/// Number of bytes to allocate for grapheme data. /// Number of bytes to allocate for grapheme data.
grapheme_bytes: usize = grapheme_bytes_default, grapheme_bytes: usize = grapheme_bytes_default,

View File

@ -132,21 +132,22 @@ pub fn RefCountedSet(
/// ///
/// The returned layout `cap` property will be 1 more than the number /// The returned layout `cap` property will be 1 more than the number
/// of items that the set can actually store, since ID 0 is reserved. /// of items that the set can actually store, since ID 0 is reserved.
pub fn layout(cap: Id) Layout { pub fn layout(cap: usize) Layout {
// Experimentally, this load factor works quite well. // Experimentally, this load factor works quite well.
const load_factor = 0.8125; const load_factor = 0.8125;
const table_cap: Id = std.math.ceilPowerOfTwoAssert(Id, cap); assert(cap <= @as(usize, @intCast(std.math.maxInt(Id))) + 1);
const table_mask: Id = (@as(Id, 1) << std.math.log2_int(Id, table_cap)) - 1;
const items_cap: Id = @intFromFloat(load_factor * @as(f64, @floatFromInt(table_cap))); const table_cap: usize = std.math.ceilPowerOfTwoAssert(usize, cap);
const items_cap: usize = @intFromFloat(load_factor * @as(f64, @floatFromInt(table_cap)));
const table_mask: Id = @intCast((@as(usize, 1) << std.math.log2_int(usize, table_cap)) - 1);
const table_start = 0; const table_start = 0;
const table_cap_usize: usize = @intCast(table_cap); const table_end = table_start + table_cap * @sizeOf(Id);
const table_end = table_start + table_cap_usize * @sizeOf(Id);
const items_start = std.mem.alignForward(usize, table_end, @alignOf(Item)); const items_start = std.mem.alignForward(usize, table_end, @alignOf(Item));
const items_cap_usize: usize = @intCast(items_cap); const items_end = items_start + items_cap * @sizeOf(Item);
const items_end = items_start + items_cap_usize * @sizeOf(Item);
const total_size = items_end; const total_size = items_end;
@ -161,8 +162,8 @@ pub fn RefCountedSet(
} }
pub const Layout = struct { pub const Layout = struct {
cap: Id, cap: usize,
table_cap: Id, table_cap: usize,
table_mask: Id, table_mask: Id,
table_start: usize, table_start: usize,
items_start: usize, items_start: usize,
@ -390,7 +391,7 @@ pub fn RefCountedSet(
items[id] = .{}; items[id] = .{};
var p: Id = item.meta.bucket; var p: Id = item.meta.bucket;
var n: Id = (p + 1) & self.layout.table_mask; var n: Id = @addWithOverflow(p, 1)[0] & self.layout.table_mask;
while (table[n] != 0 and items[table[n]].meta.psl > 0) { while (table[n] != 0 and items[table[n]].meta.psl > 0) {
items[table[n]].meta.bucket = p; items[table[n]].meta.bucket = p;
@ -399,7 +400,7 @@ pub fn RefCountedSet(
self.psl_stats[items[table[n]].meta.psl] += 1; self.psl_stats[items[table[n]].meta.psl] += 1;
table[p] = table[n]; table[p] = table[n];
p = n; p = n;
n = (n + 1) & self.layout.table_mask; n = @addWithOverflow(n, 1)[0] & self.layout.table_mask;
} }
while (self.max_psl > 0 and self.psl_stats[self.max_psl] == 0) { while (self.max_psl > 0 and self.psl_stats[self.max_psl] == 0) {