Skip to content

cmd/compile: optimize type switch #18492

@bradfitz

Description

@bradfitz

In code review https://go-review.googlesource.com/c/34773/, John Doe points out that a type switch in an inner loop can be optimized by pulling it out of the loop, setting an integer, and switching on the integer value instead.

John Doe provided this benchmark: https://play.golang.org/p/6LXF82e6U4

With tip, I get:

$ go test -v -bench=. -benchtime=2s
BenchmarkTypeSwitchInside-4         2000           1298710 ns/op
BenchmarkTypeSwitchOutside-4       10000            475899 ns/op

These seem like they could be identical. In both cases, it's just a switch on an integer. In the "Inside" case, that integer just happens to be in the first word of an interface value.

/cc @randall77 @mdempsky @josharian

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions