Skip to content

cmd/compile: optimize string concatenation to avoid runtime calls #77325

Description

@y1yang0

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:

Image

after applying opts:

Image

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    FeatureRequestIssues asking for a new feature that does not need a proposal.NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.Performancecompiler/runtimeIssues related to the Go compiler and/or runtime.help wanted

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    Status
    Todo

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions