Skip to content

Commit 03bb3e9

Browse files
committed
cmd/internal/obj/wasm,cmd/link/internal/wasm: add fast path for writeUleb128
While building a simple hello world binary, there are total 858277 calls to writeUleb during the assembler phase out of which 836625 (97%) are less than 7 bits. Using a simple micro-benchmark like this: func BenchmarkUleb(b *testing.B) { var buf bytes.Buffer for i := 0; i < b.N; i++ { writeUleb128(&buf, 42) buf.Reset() } } We get the following results with the fast path enabled. name old time/op new time/op delta Uleb-4 8.45ns ± 2% 7.51ns ± 2% -11.16% (p=0.000 n=10+10) Applying the time taken to the number of calls, we get roughly 6% improvement in total time taken for writeUleb128. We also apply the change to the function in linker to make it consistent. Change-Id: I9fe8c41df1209f5f3aa7d8bd0181f1b0e536ceb5 Reviewed-on: https://go-review.googlesource.com/c/go/+/201177 Run-TryBot: Agniva De Sarker <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent 86f40a2 commit 03bb3e9

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

src/cmd/internal/obj/wasm/wasmobj.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,10 @@ func align(as obj.As) uint64 {
11541154
}
11551155

11561156
func writeUleb128(w io.ByteWriter, v uint64) {
1157+
if v < 128 {
1158+
w.WriteByte(uint8(v))
1159+
return
1160+
}
11571161
more := true
11581162
for more {
11591163
c := uint8(v & 0x7f)

src/cmd/link/internal/wasm/asm.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,10 @@ func writeName(w nameWriter, name string) {
542542
}
543543

544544
func writeUleb128(w io.ByteWriter, v uint64) {
545+
if v < 128 {
546+
w.WriteByte(uint8(v))
547+
return
548+
}
545549
more := true
546550
for more {
547551
c := uint8(v & 0x7f)

0 commit comments

Comments
 (0)