teh-cmc / go-internals

A book about the internals of the Go programming language.
Other
7.83k stars 352 forks source link

chapter2 : interface Type-switches have sorted #37

Open dreamerjackson opened 3 years ago

dreamerjackson commented 3 years ago
That this reordering is a net win for us in this particular case is nothing but mere luck, AFAICT. In fact, if you take the time to experiment a bit with type-switches, especially ones with more than two cases, you'll find that the compiler always shuffles the cases using some kind of deterministic heuristics.
What those heuristics are, I don't know (but as always, I'd love to if you do).

Type-switches have sorted and runtime use a Binary search to find the result , so the time complexity is log2(n). if necessary, i am glad to pull request to this repo about how the golang compile do