0
1
mirror of https://github.com/golang/go synced 2025-06-15 17:41:45 +00:00

cmd/compile: use better splitting condition for string binary search

Currently we use a full cmpstring to do the comparison for each
split in the binary search for a string switch.

Instead, split by comparing a single byte of the input string with a
constant. That will give us a much faster split (although it might be
not quite as good a split).

Fixes 

R=go1.20

Change-Id: I28c7209342314f367071e4aa1f2beb6ec9ff7123
Reviewed-on: https://go-review.googlesource.com/c/go/+/414894
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Keith Randall <khr@golang.org>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Heschi Kreinick <heschi@google.com>
This commit is contained in:
Keith Randall
2022-06-25 11:11:07 -07:00
parent af7f067e0d
commit 69aed4712d
2 changed files with 114 additions and 5 deletions
src/cmd/compile/internal/walk
test/codegen

@ -72,3 +72,30 @@ func length(x string) int {
return len(x)
}
}
// Use single-byte ordered comparisons for binary searching strings.
// See issue 53333.
func mimetype(ext string) string {
// amd64: `CMPB\s1\(.*\), \$104$`,-`cmpstring`
// arm64: `MOVB\s1\(R.*\), R.*$`, `CMPW\s\$104, R.*$`, -`cmpstring`
switch ext {
// amd64: `CMPL\s\(.*\), \$1836345390$`
// arm64: `CMPW\s\$1836345390, R.*$`
case ".htm":
return "A"
// amd64: `CMPL\s\(.*\), \$1953457454$`
// arm64: `CMPW\s\$1953457454, R.*$`
case ".eot":
return "B"
// amd64: `CMPL\s\(.*\), \$1735815982$`
// arm64: `CMPW\s\$1735815982, R.*$`
case ".svg":
return "C"
// amd64: `CMPL\s\(.*\), \$1718907950$`
// arm64: `CMPW\s\$1718907950, R.*$`
case ".ttf":
return "D"
default:
return ""
}
}