golang/go

all: instruction alignment optimizations for assembly routines, good starter projects

Open

#63,678 opened on Oct 23, 2023

View on GitHub
 (12 comments) (2 reactions) (0 assignees)Go (19,008 forks)batch import
NeedsFixPerformancecompiler/runtimehelp wanted

Repository metrics

Stars
 (133,883 stars)
PR merge metrics
 (No merged PRs in 30d)

Description

Issue https://github.com/golang/go/issues/56474 added support for instruction alignment on the amd64 architecture. This is achieved with the PCALIGN assembly pseudo instruction, which inserts NOP's to align the next instruction to a given boundary.

Since this feature is pretty new on amd64, we didn't had much time to check which assembly routines in the runtime/libraries would benefit com instruction alignment. In most cases, the effect of instruction alignment is minimal, but on critical subroutines and critical innermost loops it can deliver a significant boost in performance.

Some examples:

There are multiple places were we might get interesting results using PCALIGN in amd64 assembly:

  • runtime functions (memmove, memclr, etc)
  • internal/bytealg
  • crypto/*
  • *amd64*.s hot loops/critical sections in assembly code

Generally a 16-byte alignment works fine, while 32-byte is better when aligning AVX2 instructions. Be careful when overusing it, routines may end up slower than before.

In order to verify if there are any speedups you can run the benchmarks for the affected package with a higher count, at least -count=10. Then use benchstat to compare the before/after results. I'd say anything higher than 3-5% consistently is worth submitting.

Code alignment is already supported on ppc64, arm64, loong64 and riscv64. Feel free to look into improvements for these architectures as well! You can rely on qemu-static to run the benchmarks after compiling the tests with go test -c.

Happy hacking!

Contributor guide