authorgravatar for contact@fengb.meBenjamin Feng <contact@fengb.me> 2019-12-04 21:21:54-06:00
committergravatar for contact@fengb.meBenjamin Feng <contact@fengb.me> 2019-12-04 21:22:36-06:00
log5784985bb8a4c6aa686ad0b9fb98532c44c74b85
tree526e2de350856f970f62c9b2b1462950c3fca0c2
parenta910a6c871d1840c900287d68f0d5c9f8888247d

Use raw PackedIo to shave ~150b


1 files changed, 18 insertions(+), 13 deletions(-)

lib/std/heap.zig+18-13
......@@ -257,24 +257,28 @@ const WasmPageAllocator = struct {
257257 var heap_base_wannabe: [256]u8 align(16) = undefined;
258258
259259 const FreeBlock = struct {
260 packed_data: std.PackedIntSlice(u1) = std.PackedIntSlice(u1).init(&[_]u8{}, 0),
261 block_data: []u128 = &[_]u128{},
260 const Io = std.packed_int_array.PackedIntIo(u1, .Little);
262261
263 fn initData(self: *FreeBlock, data: []align(16) u8) void {
262 bytes: []align(16) u8,
263
264 fn initData(self: *FreeBlock, bytes: []align(16) u8) void {
264265 // 0 == used, 1 == free
265 std.mem.set(u8, data, 0);
266 self.packed_data = std.PackedIntSlice(u1).init(data, data.len * 8);
267 self.block_data = @bytesToSlice(u128, data);
266 std.mem.set(u8, bytes, 0);
267 self.bytes = bytes;
268268 }
269269
270270 fn totalPages(self: FreeBlock) usize {
271 return self.packed_data.int_count;
271 return self.bytes.len * 8;
272 }
273
274 fn getBit(self: *FreeBlock, idx: usize) u1 {
275 return Io.get(self.bytes, idx, 0);
272276 }
273277
274278 fn setBits(self: *FreeBlock, start_idx: usize, len: usize, val: u1) void {
275279 var i: usize = 0;
276280 while (i < len) : (i += 1) {
277 self.packed_data.set(i + start_idx, val);
281 Io.set(self.bytes, start_idx + i, 0, val);
278282 }
279283 }
280284
......@@ -282,14 +286,15 @@ const WasmPageAllocator = struct {
282286 // This saves ~50 bytes compared to returning a nullable
283287
284288 // We can guarantee that conventional memory never gets this big,
285 // and wasm32 would not be able to address this block (32 GB > usize).
289 // and wasm32 would not be able to address this memory (32 GB > usize).
286290
287291 // Revisit if this is settled: https://github.com/ziglang/zig/issues/3806
288292 const not_found = std.math.maxInt(usize);
289293
290294 fn useRecycled(self: *FreeBlock, num_pages: usize) usize {
291295 @setCold(true);
292 for (self.block_data) |segment, i| {
296 const segments = @bytesToSlice(u128, self.bytes);
297 for (segments) |segment, i| {
293298 const spills_into_next = @bitCast(i128, segment) < 0;
294299 const has_enough_bits = @popCount(u128, segment) >= num_pages;
295300
......@@ -298,7 +303,7 @@ const WasmPageAllocator = struct {
298303 var j: usize = i * 128;
299304 while (j < (i + 1) * 128) : (j += 1) {
300305 var count: usize = 0;
301 while (j + count < self.packed_data.int_count and self.packed_data.get(j + count) == 1) {
306 while (j + count < self.totalPages() and self.getBit(j + count) == 1) {
302307 count += 1;
303308 if (count >= num_pages) {
304309 self.setBits(j, num_pages, 0);
......@@ -316,8 +321,8 @@ const WasmPageAllocator = struct {
316321 }
317322 };
318323
319 var conventional = FreeBlock{};
320 var extended = FreeBlock{};
324 var conventional = FreeBlock{ .bytes = &[_]u8{} };
325 var extended = FreeBlock{ .bytes = &[_]u8{} };
321326
322327 fn extendedOffset() usize {
323328 return conventional.totalPages();