Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/compile: jump table optimisation is not utilised when a case has multiple clauses #56253

Closed
renthraysk opened this issue Oct 16, 2022 · 2 comments
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge

Comments

@renthraysk
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.19.2 linux/amd64

Does this issue reproduce with the latest release?

Yes.

What did you do?

https://go.dev/play/p/LSrP_ch-8f1

What did you expect to see?

Both f1() & f2() to utilize jump table optimization, as both functions are functionally identical.

What did you see instead?

f1() uses jump table optimization as it has 8 distinct case clauses.
f2() reverts to a chain of comparisons.

@renthraysk renthraysk changed the title cmd/complie: jump table optimisation is not utilised when a case has multiple clauses cmd/compile: jump table optimisation is not utilised when a case has multiple clauses Oct 16, 2022
@gopherbot gopherbot added the compiler/runtime Issues related to the Go compiler and/or runtime. label Oct 16, 2022
@randall77
Copy link
Contributor

This is kind of intentional.
The intent is to measure the number of comparisons needed to implement the switch as a binary search. In f2, there are really only 7 different ranges that need to be distinguished. Consider:

	switch x {
	case 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10:
		println("<11")
	}

That also doesn't use a jump table, as it doesn't take many comparisons to implement it.

We probably shoudn't generate the jump table for f1, but we don't recognize that we can join cases when the first case is a naked fallthrough. (And also we don't recognize duplicate, in your case empty, code blocks either, which would also allow joining cases.)

And of course the number 8 is kind of arbitrary. We had to pick some threshold.

@dr2chase
Copy link
Contributor

Close as working as intended / not a bug?

@golang golang locked and limited conversation to collaborators Oct 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
compiler/runtime Issues related to the Go compiler and/or runtime. FrozenDueToAge
Projects
None yet
Development

No branches or pull requests

4 participants