Reduce binary size by 400 KB (#18280)

Co-authored-by: Jarred-Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Co-authored-by: Dylan Conway <35280289+dylan-conway@users.noreply.github.com>
This commit is contained in:
Jarred Sumner
2025-03-18 21:02:01 -07:00
committed by GitHub
parent c8634668e7
commit 40e222c43b
6 changed files with 97 additions and 56 deletions

View File

@@ -12,21 +12,29 @@ pub fn HiveArray(comptime T: type, comptime capacity: u16) type {
const Self = @This();
buffer: [capacity]T,
available: bun.bit_set.IntegerBitSet(capacity),
used: bun.bit_set.IntegerBitSet(capacity),
pub const size = capacity;
pub const empty: Self = .{
/// This is deliberately a `var` instead of a `const`.
///
/// https://github.com/ziglang/zig/issues/22462
/// https://github.com/ziglang/zig/issues/21988
pub var empty: Self = .{
.buffer = undefined,
.available = .initFull(),
.used = .initEmpty(),
};
pub fn init() Self {
return .{};
return .{
.buffer = undefined,
.used = .initEmpty(),
};
}
pub fn get(self: *Self) ?*T {
const index = self.available.findFirstSet() orelse return null;
self.available.unset(index);
const index = self.used.findFirstUnset() orelse return null;
self.used.set(index);
return &self.buffer[index];
}
@@ -37,8 +45,8 @@ pub fn HiveArray(comptime T: type, comptime capacity: u16) type {
pub fn claim(self: *Self, index: u16) void {
assert(index < capacity);
assert(self.available.isSet(index));
self.available.unset(index);
assert(!self.used.isSet(index));
self.used.set(index);
}
pub fn indexOf(self: *const Self, value: *const T) ?u32 {
@@ -63,17 +71,17 @@ pub fn HiveArray(comptime T: type, comptime capacity: u16) type {
pub fn put(self: *Self, value: *T) bool {
const index = self.indexOf(value) orelse return false;
assert(!self.available.isSet(index));
assert(self.used.isSet(index));
assert(&self.buffer[index] == value);
value.* = undefined;
self.available.set(index);
self.used.unset(index);
return true;
}
pub const Fallback = struct {
hive: if (capacity > 0) HiveArray(T, capacity) else void,
hive: if (capacity > 0) Self else void,
allocator: std.mem.Allocator,
pub const This = @This();
@@ -81,7 +89,7 @@ pub fn HiveArray(comptime T: type, comptime capacity: u16) type {
pub fn init(allocator: std.mem.Allocator) This {
return .{
.allocator = allocator,
.hive = if (capacity > 0) .empty,
.hive = if (comptime capacity > 0) Self.empty,
};
}
@@ -156,7 +164,7 @@ test "HiveArray" {
try testing.expect(a.in(&d) == false);
}
a.available = @TypeOf(a.available).initFull();
a.used = @TypeOf(a.used).initEmpty();
{
for (0..size) |i| {
const b = a.get().?;