Skip to content

Commit d7d9056

Browse files
authored
Merge pull request #7622 from tetsuo-cpp/array-hash-map-improvements
std: Support equivalent ArrayList operations in ArrayHashMap
2 parents 76870a2 + d92ea56 commit d7d9056

File tree

12 files changed

+363
-64
lines changed

12 files changed

+363
-64
lines changed

lib/std/array_hash_map.zig

Lines changed: 333 additions & 34 deletions
Large diffs are not rendered by default.

lib/std/array_list.zig

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ pub fn ArrayListAligned(comptime T: type, comptime alignment: ?u29) type {
279279

280280
/// Reduce allocated capacity to `new_len`.
281281
/// May invalidate element pointers.
282-
pub fn shrink(self: *Self, new_len: usize) void {
282+
pub fn shrinkAndFree(self: *Self, new_len: usize) void {
283283
assert(new_len <= self.items.len);
284284

285285
self.items = self.allocator.realloc(self.allocatedSlice(), new_len) catch |e| switch (e) {
@@ -587,7 +587,7 @@ pub fn ArrayListAlignedUnmanaged(comptime T: type, comptime alignment: ?u29) typ
587587
}
588588

589589
/// Reduce allocated capacity to `new_len`.
590-
pub fn shrink(self: *Self, allocator: *Allocator, new_len: usize) void {
590+
pub fn shrinkAndFree(self: *Self, allocator: *Allocator, new_len: usize) void {
591591
assert(new_len <= self.items.len);
592592

593593
self.items = allocator.realloc(self.allocatedSlice(), new_len) catch |e| switch (e) {
@@ -1155,7 +1155,7 @@ test "std.ArrayList/ArrayListUnmanaged.shrink still sets length on error.OutOfMe
11551155
try list.append(2);
11561156
try list.append(3);
11571157

1158-
list.shrink(1);
1158+
list.shrinkAndFree(1);
11591159
testing.expect(list.items.len == 1);
11601160
}
11611161
{
@@ -1165,7 +1165,7 @@ test "std.ArrayList/ArrayListUnmanaged.shrink still sets length on error.OutOfMe
11651165
try list.append(a, 2);
11661166
try list.append(a, 3);
11671167

1168-
list.shrink(a, 1);
1168+
list.shrinkAndFree(a, 1);
11691169
testing.expect(list.items.len == 1);
11701170
}
11711171
}

lib/std/fs.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2186,7 +2186,7 @@ pub const Walker = struct {
21862186
var top = &self.stack.items[self.stack.items.len - 1];
21872187
const dirname_len = top.dirname_len;
21882188
if (try top.dir_it.next()) |base| {
2189-
self.name_buffer.shrink(dirname_len);
2189+
self.name_buffer.shrinkAndFree(dirname_len);
21902190
try self.name_buffer.append(path.sep);
21912191
try self.name_buffer.appendSlice(base.name);
21922192
if (base.kind == .Directory) {

lib/std/io/reader.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ pub fn Reader(
7676
start_index += bytes_read;
7777

7878
if (start_index - original_len > max_append_size) {
79-
array_list.shrink(original_len + max_append_size);
79+
array_list.shrinkAndFree(original_len + max_append_size);
8080
return error.StreamTooLong;
8181
}
8282

8383
if (bytes_read != dest_slice.len) {
84-
array_list.shrink(start_index);
84+
array_list.shrinkAndFree(start_index);
8585
return;
8686
}
8787

@@ -111,7 +111,7 @@ pub fn Reader(
111111
delimiter: u8,
112112
max_size: usize,
113113
) !void {
114-
array_list.shrink(0);
114+
array_list.shrinkAndFree(0);
115115
while (true) {
116116
var byte: u8 = try self.readByte();
117117

lib/std/json.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1897,7 +1897,7 @@ pub const Parser = struct {
18971897

18981898
pub fn reset(p: *Parser) void {
18991899
p.state = .Simple;
1900-
p.stack.shrink(0);
1900+
p.stack.shrinkAndFree(0);
19011901
}
19021902

19031903
pub fn parse(p: *Parser, input: []const u8) !ValueTree {

lib/std/math/big/int.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,7 @@ pub const Mutable = struct {
607607
/// it will have the same length as it had when the function was called.
608608
pub fn gcd(rma: *Mutable, x: Const, y: Const, limbs_buffer: *std.ArrayList(Limb)) !void {
609609
const prev_len = limbs_buffer.items.len;
610-
defer limbs_buffer.shrink(prev_len);
610+
defer limbs_buffer.shrinkAndFree(prev_len);
611611
const x_copy = if (rma.limbs.ptr == x.limbs.ptr) blk: {
612612
const start = limbs_buffer.items.len;
613613
try limbs_buffer.appendSlice(x.limbs);

lib/std/net.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1200,13 +1200,13 @@ fn linuxLookupNameFromDnsSearch(
12001200

12011201
var tok_it = mem.tokenize(search, " \t");
12021202
while (tok_it.next()) |tok| {
1203-
canon.shrink(canon_name.len + 1);
1203+
canon.shrinkAndFree(canon_name.len + 1);
12041204
try canon.appendSlice(tok);
12051205
try linuxLookupNameFromDns(addrs, canon, canon.items, family, rc, port);
12061206
if (addrs.items.len != 0) return;
12071207
}
12081208

1209-
canon.shrink(canon_name.len);
1209+
canon.shrinkAndFree(canon_name.len);
12101210
return linuxLookupNameFromDns(addrs, canon, name, family, rc, port);
12111211
}
12121212

src/Compilation.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
738738
}
739739
assert(mem.endsWith(u8, buf.items, ","));
740740
buf.items[buf.items.len - 1] = 0;
741-
buf.shrink(buf.items.len);
741+
buf.shrinkAndFree(buf.items.len);
742742
break :blk buf.items[0 .. buf.items.len - 1 :0].ptr;
743743
} else null;
744744

src/Module.zig

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -594,7 +594,7 @@ pub const Scope = struct {
594594
}
595595

596596
pub fn removeDecl(self: *Container, child: *Decl) void {
597-
_ = self.decls.remove(child);
597+
_ = self.decls.swapRemove(child);
598598
}
599599

600600
pub fn fullyQualifiedNameHash(self: *Container, name: []const u8) NameHash {
@@ -1710,7 +1710,7 @@ pub fn analyzeContainer(self: *Module, container_scope: *Scope.Container) !void
17101710
// Update the AST Node index of the decl, even if its contents are unchanged, it may
17111711
// have been re-ordered.
17121712
decl.src_index = decl_i;
1713-
if (deleted_decls.remove(decl) == null) {
1713+
if (deleted_decls.swapRemove(decl) == null) {
17141714
decl.analysis = .sema_failure;
17151715
const err_msg = try Compilation.ErrorMsg.create(self.gpa, tree.token_locs[name_tok].start, "redefinition of '{s}'", .{decl.name});
17161716
errdefer err_msg.destroy(self.gpa);
@@ -1752,7 +1752,7 @@ pub fn analyzeContainer(self: *Module, container_scope: *Scope.Container) !void
17521752
// Update the AST Node index of the decl, even if its contents are unchanged, it may
17531753
// have been re-ordered.
17541754
decl.src_index = decl_i;
1755-
if (deleted_decls.remove(decl) == null) {
1755+
if (deleted_decls.swapRemove(decl) == null) {
17561756
decl.analysis = .sema_failure;
17571757
const err_msg = try Compilation.ErrorMsg.create(self.gpa, name_loc.start, "redefinition of '{s}'", .{decl.name});
17581758
errdefer err_msg.destroy(self.gpa);
@@ -1882,7 +1882,7 @@ pub fn deleteDecl(self: *Module, decl: *Decl) !void {
18821882
try self.markOutdatedDecl(dep);
18831883
}
18841884
}
1885-
if (self.failed_decls.remove(decl)) |entry| {
1885+
if (self.failed_decls.swapRemove(decl)) |entry| {
18861886
entry.value.destroy(self.gpa);
18871887
}
18881888
if (self.emit_h_failed_decls.remove(decl)) |entry| {
@@ -1900,7 +1900,7 @@ pub fn deleteDecl(self: *Module, decl: *Decl) !void {
19001900
/// Delete all the Export objects that are caused by this Decl. Re-analysis of
19011901
/// this Decl will cause them to be re-created (or not).
19021902
fn deleteDeclExports(self: *Module, decl: *Decl) void {
1903-
const kv = self.export_owners.remove(decl) orelse return;
1903+
const kv = self.export_owners.swapRemove(decl) orelse return;
19041904

19051905
for (kv.value) |exp| {
19061906
if (self.decl_exports.getEntry(exp.exported_decl)) |decl_exports_kv| {
@@ -1927,10 +1927,10 @@ fn deleteDeclExports(self: *Module, decl: *Decl) void {
19271927
if (self.comp.bin_file.cast(link.File.MachO)) |macho| {
19281928
macho.deleteExport(exp.link.macho);
19291929
}
1930-
if (self.failed_exports.remove(exp)) |entry| {
1930+
if (self.failed_exports.swapRemove(exp)) |entry| {
19311931
entry.value.destroy(self.gpa);
19321932
}
1933-
_ = self.symbol_exports.remove(exp.options.name);
1933+
_ = self.symbol_exports.swapRemove(exp.options.name);
19341934
self.gpa.free(exp.options.name);
19351935
self.gpa.destroy(exp);
19361936
}
@@ -1975,7 +1975,7 @@ pub fn analyzeFnBody(self: *Module, decl: *Decl, func: *Fn) !void {
19751975
fn markOutdatedDecl(self: *Module, decl: *Decl) !void {
19761976
log.debug("mark {s} outdated\n", .{decl.name});
19771977
try self.comp.work_queue.writeItem(.{ .analyze_decl = decl });
1978-
if (self.failed_decls.remove(decl)) |entry| {
1978+
if (self.failed_decls.swapRemove(decl)) |entry| {
19791979
entry.value.destroy(self.gpa);
19801980
}
19811981
if (self.emit_h_failed_decls.remove(decl)) |entry| {

src/codegen.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2123,7 +2123,7 @@ fn Function(comptime arch: std.Target.Cpu.Arch) type {
21232123
try parent_branch.inst_table.ensureCapacity(self.gpa, parent_branch.inst_table.items().len +
21242124
else_branch.inst_table.items().len);
21252125
for (else_branch.inst_table.items()) |else_entry| {
2126-
const canon_mcv = if (saved_then_branch.inst_table.remove(else_entry.key)) |then_entry| blk: {
2126+
const canon_mcv = if (saved_then_branch.inst_table.swapRemove(else_entry.key)) |then_entry| blk: {
21272127
// The instruction's MCValue is overridden in both branches.
21282128
parent_branch.inst_table.putAssumeCapacity(else_entry.key, then_entry.value);
21292129
if (else_entry.value == .dead) {

src/libc_installation.zig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ pub const LibCInstallation = struct {
337337
defer result_buf.deinit();
338338

339339
for (searches) |search| {
340-
result_buf.shrink(0);
340+
result_buf.shrinkAndFree(0);
341341
try result_buf.outStream().print("{s}\\Include\\{s}\\ucrt", .{ search.path, search.version });
342342

343343
var dir = fs.cwd().openDir(result_buf.items, .{}) catch |err| switch (err) {
@@ -383,7 +383,7 @@ pub const LibCInstallation = struct {
383383
};
384384

385385
for (searches) |search| {
386-
result_buf.shrink(0);
386+
result_buf.shrinkAndFree(0);
387387
try result_buf.outStream().print("{s}\\Lib\\{s}\\ucrt\\{s}", .{ search.path, search.version, arch_sub_dir });
388388

389389
var dir = fs.cwd().openDir(result_buf.items, .{}) catch |err| switch (err) {
@@ -437,7 +437,7 @@ pub const LibCInstallation = struct {
437437
};
438438

439439
for (searches) |search| {
440-
result_buf.shrink(0);
440+
result_buf.shrinkAndFree(0);
441441
const stream = result_buf.outStream();
442442
try stream.print("{s}\\Lib\\{s}\\um\\{s}", .{ search.path, search.version, arch_sub_dir });
443443

src/translate_c.zig

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2846,7 +2846,7 @@ fn transCase(
28462846

28472847
// take all pending statements
28482848
try switch_scope.pending_block.statements.appendSlice(block_scope.statements.items);
2849-
block_scope.statements.shrink(0);
2849+
block_scope.statements.shrinkAndFree(0);
28502850

28512851
const pending_node = try switch_scope.pending_block.complete(rp.c);
28522852
switch_scope.pending_block.deinit();
@@ -2884,7 +2884,7 @@ fn transDefault(
28842884

28852885
// take all pending statements
28862886
try switch_scope.pending_block.statements.appendSlice(block_scope.statements.items);
2887-
block_scope.statements.shrink(0);
2887+
block_scope.statements.shrinkAndFree(0);
28882888

28892889
const pending_node = try switch_scope.pending_block.complete(rp.c);
28902890
switch_scope.pending_block.deinit();
@@ -4773,9 +4773,9 @@ const RestorePoint = struct {
47734773
src_buf_index: usize,
47744774

47754775
fn activate(self: RestorePoint) void {
4776-
self.c.token_ids.shrink(self.c.gpa, self.token_index);
4777-
self.c.token_locs.shrink(self.c.gpa, self.token_index);
4778-
self.c.source_buffer.shrink(self.src_buf_index);
4776+
self.c.token_ids.shrinkAndFree(self.c.gpa, self.token_index);
4777+
self.c.token_locs.shrinkAndFree(self.c.gpa, self.token_index);
4778+
self.c.source_buffer.shrinkAndFree(self.src_buf_index);
47794779
}
47804780
};
47814781

0 commit comments

Comments
 (0)