authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-06-02 14:02:48-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-06-10 20:47:59-07:00
logad54f47b95a2295e0c199decb5ff10c572317a22
tree9a5fcf078a87233ab27b713a3037dd7c7891fdd8
parent0fd52cdc5eb4b17e8066a06d8af761f934cf8808

InternPool: optimize previous fix

Just because we can't dedup, doesn't mean we can't use `string_bytes`.

1 files changed, 8 insertions(+), 22 deletions(-)

src/InternPool.zig+8-22
......@@ -3951,24 +3951,20 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {
39513951 else => unreachable,
39523952 },
39533953 }
3954 // We can't dedup '0' bytes in the pool or it could add garbage to string_bytes. So
3955 // if there are any 0 bytes, we have to skip the bytes case. Note that it's okay for
3956 // our sentinel to be 0 since getOrPutTrailingString would add a 0 sentinel anyway.
3957 for (ip.string_bytes.items[string_bytes_index..]) |x| {
3958 if (x == 0) {
3959 ip.string_bytes.shrinkRetainingCapacity(string_bytes_index);
3960 break :bytes;
3961 }
3962 }
3954 const has_internal_null =
3955 std.mem.indexOfScalar(u8, ip.string_bytes.items[string_bytes_index..], 0) != null;
39633956 if (sentinel != .none) ip.string_bytes.appendAssumeCapacity(
39643957 @intCast(u8, ip.indexToKey(sentinel).int.storage.u64),
39653958 );
3966 const bytes = try ip.getOrPutTrailingString(gpa, len_including_sentinel);
3959 const string = if (has_internal_null)
3960 @intToEnum(String, string_bytes_index)
3961 else
3962 (try ip.getOrPutTrailingString(gpa, len_including_sentinel)).toString();
39673963 ip.items.appendAssumeCapacity(.{
39683964 .tag = .bytes,
39693965 .data = ip.addExtraAssumeCapacity(Bytes{
39703966 .ty = aggregate.ty,
3971 .bytes = bytes.toString(),
3967 .bytes = string,
39723968 }),
39733969 });
39743970 return @intToEnum(Index, ip.items.len - 1);
......@@ -3984,17 +3980,7 @@ pub fn get(ip: *InternPool, gpa: Allocator, key: Key) Allocator.Error!Index {
39843980 .ty = aggregate.ty,
39853981 }),
39863982 });
3987 switch (aggregate.storage) {
3988 .bytes => |bytes| for (bytes) |b| {
3989 const elem = try ip.get(gpa, .{ .int = .{
3990 .ty = .u8_type,
3991 .storage = .{ .u64 = b },
3992 } });
3993 ip.extra.appendAssumeCapacity(@enumToInt(elem));
3994 },
3995 .elems => |elems| ip.extra.appendSliceAssumeCapacity(@ptrCast([]const u32, elems)),
3996 .repeated_elem => |elem| ip.extra.appendNTimesAssumeCapacity(@enumToInt(elem), len),
3997 }
3983 ip.extra.appendSliceAssumeCapacity(@ptrCast([]const u32, aggregate.storage.elems));
39983984 if (sentinel != .none) ip.extra.appendAssumeCapacity(@enumToInt(sentinel));
39993985 },
40003986