multi_array_list: use slice() when possible

This commit is contained in:
Motiejus Jakštys 2023-05-19 16:12:33 +03:00
parent 82ab622c66
commit 6d957967ed
2 changed files with 10 additions and 10 deletions

View File

@ -580,9 +580,10 @@ fn usersSection(
// 8 bytes. 24 is an optimistic lower bound for an average record size. // 8 bytes. 24 is an optimistic lower bound for an average record size.
var blob = try ArrayListAligned(u8, 8).initCapacity(allocator, 24 * corpus.users.len); var blob = try ArrayListAligned(u8, 8).initCapacity(allocator, 24 * corpus.users.len);
errdefer blob.deinit(); errdefer blob.deinit();
for (0..corpus.users.len) |i| { const users = corpus.users.slice();
for (0..users.len) |i| {
// TODO: this may be inefficient by calling `.slice()` on every iteration? // TODO: this may be inefficient by calling `.slice()` on every iteration?
const user = corpus.users.get(i); const user = users.get(i);
const user_offset = math.cast(u35, blob.items.len) orelse return error.TooMany; const user_offset = math.cast(u35, blob.items.len) orelse return error.TooMany;
assert(user_offset & 7 == 0); assert(user_offset & 7 == 0);
idx2offset[i] = @truncate(u32, user_offset >> 3); idx2offset[i] = @truncate(u32, user_offset >> 3);
@ -595,7 +596,7 @@ fn usersSection(
try blob.appendNTimes(0, mem.alignForward(blob.items.len, 8) - blob.items.len); try blob.appendNTimes(0, mem.alignForward(blob.items.len, 8) - blob.items.len);
} }
return UsersSection{ return UsersSection{
.len = @intCast(u32, corpus.users.len), .len = @intCast(u32, users.len),
.idx2offset = idx2offset, .idx2offset = idx2offset,
.blob = try blob.toOwnedSlice(), .blob = try blob.toOwnedSlice(),
}; };

View File

@ -215,17 +215,16 @@ test "turbonss-unix2db smoke test" {
const group_fd = try os.open(groupPath, os.O.CREAT | os.O.WRONLY, 0o644); const group_fd = try os.open(groupPath, os.O.CREAT | os.O.WRONLY, 0o644);
var i: usize = 0; var i: usize = 0;
while (i < corpus.users.len) : (i += 1) { const users = corpus.users.slice();
const user = corpus.users.get(i); while (i < users.len) : (i += 1)
const line = user.toLine().constSlice(); _ = try os.write(passwd_fd, users.get(i).toLine().constSlice());
_ = try os.write(passwd_fd, line);
}
os.close(passwd_fd); os.close(passwd_fd);
var group_writer = (fs.File{ .handle = group_fd }).writer(); var group_writer = (fs.File{ .handle = group_fd }).writer();
i = 0; i = 0;
while (i < corpus.groups.len) : (i += 1) const groups = corpus.groups.slice();
try corpus.groups.get(i).writeTo(group_writer); while (i < groups.len) : (i += 1)
try groups.get(i).writeTo(group_writer);
os.close(group_fd); os.close(group_fd);
const args = &[_][*:0]const u8{ const args = &[_][*:0]const u8{