authorgravatar for r00ster91@proton.meWooster <r00ster91@proton.me> 2023-07-22 00:16:08+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-07-21 21:32:18-07:00
logd962ad5ea0290ea4b713f5546eb12032351cb7b9
treecfec9eed2540ce60061257ad53dfe64126b27674
parent619140c0d2abd22da668747820f0ee80b497eb24

codegen: writer().writeByteNTimes -> appendNTimes

Both ways do the same thing but I think the compiler might have an easier time optimizing `appendNTimes` because it does less things/the path is shorter. I have not done any benchmarking at runtime but have compared the instruction count of both ways a little here: https://zig.godbolt.org/z/vr193W9oj `b` (`appendNTimes`) is ~103 instructions while `a` (`writer().writeByteNTimes`) is ~117 instructions. And looking at the implementation of `writeByteNTimes`, it only seems to buffer up 256 bytes before doing another `writeAll` which for `std.ArrayList` probably means another allocation, whereas when directly using `appendNTimes`, the entire exact additional capacity required is known from the start. Either way, this would be more consistent anyway.

1 files changed, 11 insertions(+), 11 deletions(-)

src/codegen.zig+11-11
......@@ -294,7 +294,7 @@ pub fn generateSymbol(
294294 const padding = math.cast(usize, padded_end - unpadded_end) orelse return error.Overflow;
295295
296296 if (padding > 0) {
297 try code.writer().writeByteNTimes(0, padding);
297 try code.appendNTimes(0, padding);
298298 }
299299 }
300300
......@@ -307,7 +307,7 @@ pub fn generateSymbol(
307307 const padding = math.cast(usize, padded_end - unpadded_end) orelse return error.Overflow;
308308
309309 if (padding > 0) {
310 try code.writer().writeByteNTimes(0, padding);
310 try code.appendNTimes(0, padding);
311311 }
312312 }
313313 },
......@@ -367,7 +367,7 @@ pub fn generateSymbol(
367367 .fail => |em| return Result{ .fail = em },
368368 }
369369 } else {
370 try code.writer().writeByteNTimes(0, abi_size);
370 try code.appendNTimes(0, abi_size);
371371 }
372372 } else {
373373 const padding = abi_size - (math.cast(usize, payload_type.abiSize(mod)) orelse return error.Overflow) - 1;
......@@ -382,7 +382,7 @@ pub fn generateSymbol(
382382 }
383383 }
384384 try code.writer().writeByte(@intFromBool(payload_val != null));
385 try code.writer().writeByteNTimes(0, padding);
385 try code.appendNTimes(0, padding);
386386 }
387387 },
388388 .aggregate => |aggregate| switch (mod.intern_pool.indexToKey(typed_value.ty.toIntern())) {
......@@ -433,7 +433,7 @@ pub fn generateSymbol(
433433 error.DivisionByZero => unreachable,
434434 else => |e| return e,
435435 })) orelse return error.Overflow;
436 if (padding > 0) try code.writer().writeByteNTimes(0, padding);
436 if (padding > 0) try code.appendNTimes(0, padding);
437437 },
438438 .anon_struct_type => |tuple| {
439439 const struct_begin = code.items.len;
......@@ -465,7 +465,7 @@ pub fn generateSymbol(
465465 return error.Overflow;
466466
467467 if (padding > 0) {
468 try code.writer().writeByteNTimes(0, padding);
468 try code.appendNTimes(0, padding);
469469 }
470470 }
471471 },
......@@ -540,7 +540,7 @@ pub fn generateSymbol(
540540 const padding = math.cast(usize, padded_field_end - unpadded_field_end) orelse return error.Overflow;
541541
542542 if (padding > 0) {
543 try code.writer().writeByteNTimes(0, padding);
543 try code.appendNTimes(0, padding);
544544 }
545545 }
546546 }
......@@ -573,7 +573,7 @@ pub fn generateSymbol(
573573 assert(union_ty.haveFieldTypes());
574574 const field_ty = union_ty.fields.values()[field_index].ty;
575575 if (!field_ty.hasRuntimeBits(mod)) {
576 try code.writer().writeByteNTimes(0xaa, math.cast(usize, layout.payload_size) orelse return error.Overflow);
576 try code.appendNTimes(0xaa, math.cast(usize, layout.payload_size) orelse return error.Overflow);
577577 } else {
578578 switch (try generateSymbol(bin_file, src_loc, .{
579579 .ty = field_ty,
......@@ -585,7 +585,7 @@ pub fn generateSymbol(
585585
586586 const padding = math.cast(usize, layout.payload_size - field_ty.abiSize(mod)) orelse return error.Overflow;
587587 if (padding > 0) {
588 try code.writer().writeByteNTimes(0, padding);
588 try code.appendNTimes(0, padding);
589589 }
590590 }
591591
......@@ -599,7 +599,7 @@ pub fn generateSymbol(
599599 }
600600
601601 if (layout.padding > 0) {
602 try code.writer().writeByteNTimes(0, layout.padding);
602 try code.appendNTimes(0, layout.padding);
603603 }
604604 }
605605 },
......@@ -734,7 +734,7 @@ fn lowerDeclRef(
734734 const decl = mod.declPtr(decl_index);
735735 const is_fn_body = decl.ty.zigTypeTag(mod) == .Fn;
736736 if (!is_fn_body and !decl.ty.hasRuntimeBits(mod)) {
737 try code.writer().writeByteNTimes(0xaa, @divExact(ptr_width, 8));
737 try code.appendNTimes(0xaa, @divExact(ptr_width, 8));
738738 return Result.ok;
739739 }
740740