golang/go

cmd/compile: optimize string concatenation to avoid runtime calls

Aperta

#77.325 aperta il 27 gen 2026

 (6 commenti) (5 reazioni) (0 assegnatari)Go (19.008 fork)batch import

Metriche repository

Star
 (133.883 stelle)
Metriche merge PR
 (Metriche PR in attesa)

Descrizione

Hi team, I've identified a potential optimization opportunity to eliminate concat calls for constant strings and replace them directly with a LEAQ straddr instruction, similar to what the Java JIT does.

This is a cheap optimization that can enable further optimizations down the line, such as compile-time evaluation of len(const_string).

A local run of ./make.bash shows 200+ potential sites for this optimization.

Here is an example:

func ff(a, b int) string {
	y := "abc"
	t := y + "def"
	if t == "abcdef" {
		return t + "ghi"
	}
	return t
}

before:

after applying opts:

For implementation, we could either

  • add a dedicated stringopt pass before expand_call (which centralizes the code for future optimizations like strcmp) or
  • integrate it into the sccp pass after expand_call (which uses its framework but disperses the code) or
  • add some generic rules

Do you have a preference on the approach? Would the community be interested in this?

Guida contributor