golang/go

cmd/compile: optimize string concatenation to avoid runtime calls

開放

#77,325 建立於 2026年1月27日

 (6 則留言) (5 個反應) (0 位負責人)Go (19,008 個分叉)batch import
FeatureRequestNeedsInvestigationPerformancecompiler/runtimehelp wanted

倉庫指標

星標
 (133,883 顆星)
PR 合併指標
 (30 天內沒有已合併 PR)

描述

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?

貢獻者指南