Skip to content

Commit bc8bb5b

Browse files
committed
unicode: speed-up is16/is32
Avoid division in common case. There are 5438 ranges in unicode/tables.go 4110 of them have stride 1. Stride 1 case got significantly faster. Other stride is a bit slower. Measured by import ( "testing" "unicode" ) func BenchmarkDiv1(b *testing.B) { rtb := &unicode.RangeTable{ R16: []unicode.Range16{ {0xa800, 0xdfff, 1}, // or 3 }, } for i := 0; i < b.N; i++ { unicode.Is(rtb, rune(0xc700)) } } Div1-6 15.6ns ± 1% 9.9ns ± 1% -36.54% (p=0.000 n=10+10) Div3-6 15.5ns ± 1% 16.1ns ± 1% +3.67% (p=0.000 n=10+10) Helps a bit with xml parsing from issue golang#21823 XMLsax-6 30.9s ± 0% 29.6s ± 0% -4.15% (p=0.000 n=10+9) Change-Id: Ibac1a91d7b9474d0c134b0add83e56caa62daa20 Reviewed-on: https://go-review.googlesource.com/63390 Run-TryBot: Ilya Tocar <ilya.tocar@intel.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Ian Lance Taylor <iant@golang.org>
1 parent 6237ab2 commit bc8bb5b

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/unicode/letter.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func is16(ranges []Range16, r uint16) bool {
9797
return false
9898
}
9999
if r <= range_.Hi {
100-
return (r-range_.Lo)%range_.Stride == 0
100+
return range_.Stride == 1 || (r-range_.Lo)%range_.Stride == 0
101101
}
102102
}
103103
return false
@@ -110,7 +110,7 @@ func is16(ranges []Range16, r uint16) bool {
110110
m := lo + (hi-lo)/2
111111
range_ := &ranges[m]
112112
if range_.Lo <= r && r <= range_.Hi {
113-
return (r-range_.Lo)%range_.Stride == 0
113+
return range_.Stride == 1 || (r-range_.Lo)%range_.Stride == 0
114114
}
115115
if r < range_.Lo {
116116
hi = m
@@ -130,7 +130,7 @@ func is32(ranges []Range32, r uint32) bool {
130130
return false
131131
}
132132
if r <= range_.Hi {
133-
return (r-range_.Lo)%range_.Stride == 0
133+
return range_.Stride == 1 || (r-range_.Lo)%range_.Stride == 0
134134
}
135135
}
136136
return false
@@ -143,7 +143,7 @@ func is32(ranges []Range32, r uint32) bool {
143143
m := lo + (hi-lo)/2
144144
range_ := ranges[m]
145145
if range_.Lo <= r && r <= range_.Hi {
146-
return (r-range_.Lo)%range_.Stride == 0
146+
return range_.Stride == 1 || (r-range_.Lo)%range_.Stride == 0
147147
}
148148
if r < range_.Lo {
149149
hi = m

0 commit comments

Comments
 (0)