golang/go

cmd/compile: optimize string concatenation to avoid runtime calls

Open

#77325 opened on Jan 27, 2026

View on GitHub
 (6 comments) (5 reactions) (0 assignees)Go (133,883 stars) (19,008 forks)batch import
FeatureRequestNeedsInvestigationPerformancecompiler/runtimehelp wanted

Description

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?

Contributor guide