diff options
| author | Jacob Young <jacobly0@users.noreply.github.com> | 2023-06-02 14:02:48 -0400 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2023-06-10 20:47:59 -0700 |
| commit | ad54f47b95a2295e0c199decb5ff10c572317a22 (patch) | |
| tree | 9a5fcf078a87233ab27b713a3037dd7c7891fdd8 /src/InternPool.zig | |
| parent | 0fd52cdc5eb4b17e8066a06d8af761f934cf8808 (diff) | |
| download | zig-ad54f47b95a2295e0c199decb5ff10c572317a22.tar.gz zig-ad54f47b95a2295e0c199decb5ff10c572317a22.zip | |
InternPool: optimize previous fix
Just because we can't dedup, doesn't mean we can't use `string_bytes`.
Diffstat (limited to 'src/InternPool.zig')
| -rw-r--r-- | src/InternPool.zig | 30 |
1 files changed, 8 insertions, 22 deletions
diff --git a/src/InternPool.zig b/src/InternPool.zig index 2e592c4dd7..bd7028b879 100644 --- a/src/InternPool.zig +++ b/src/InternPool.zig @@ -3951,24 +3951,20 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index { else => unreachable, }, } - // We can't dedup '0' bytes in the pool or it could add garbage to string_bytes. So - // if there are any 0 bytes, we have to skip the bytes case. Note that it's okay for - // our sentinel to be 0 since getOrPutTrailingString would add a 0 sentinel anyway. - for (ip.string_bytes.items[string_bytes_index..]) |x| { - if (x == 0) { - ip.string_bytes.shrinkRetainingCapacity(string_bytes_index); - break :bytes; - } - } + const has_internal_null = + std.mem.indexOfScalar(u8, ip.string_bytes.items[string_bytes_index..], 0) != null; if (sentinel != .none) ip.string_bytes.appendAssumeCapacity( @intCast(u8, ip.indexToKey(sentinel).int.storage.u64), ); - const bytes = try ip.getOrPutTrailingString(gpa, len_including_sentinel); + const string = if (has_internal_null) + @intToEnum(String, string_bytes_index) + else + (try ip.getOrPutTrailingString(gpa, len_including_sentinel)).toString(); ip.items.appendAssumeCapacity(.{ .tag = .bytes, .data = ip.addExtraAssumeCapacity(Bytes{ .ty = aggregate.ty, - .bytes = bytes.toString(), + .bytes = string, }), }); return @intToEnum(Index, ip.items.len - 1); @@ -3984,17 +3980,7 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index { .ty = aggregate.ty, }), }); - switch (aggregate.storage) { - .bytes => |bytes| for (bytes) |b| { - const elem = try ip.get(gpa, .{ .int = .{ - .ty = .u8_type, - .storage = .{ .u64 = b }, - } }); - ip.extra.appendAssumeCapacity(@enumToInt(elem)); - }, - .elems => |elems| ip.extra.appendSliceAssumeCapacity(@ptrCast([]const u32, elems)), - .repeated_elem => |elem| ip.extra.appendNTimesAssumeCapacity(@enumToInt(elem), len), - } + ip.extra.appendSliceAssumeCapacity(@ptrCast([]const u32, aggregate.storage.elems)); if (sentinel != .none) ip.extra.appendAssumeCapacity(@enumToInt(sentinel)); }, |
