Skip to content

Commit 95a11c7

Browse files
iandbradfitz
authored andcommitted
net/url: remove an allocation for short strings in escape
Use a 64 byte array to avoid an allocation on the assumption that most url escaping is performed on short strings. Also adds a fast path for escaping strings whose only replacements are spaces which is common in query components. Adds benchmarks for QueryEscape, PathEscape, QueryUnescape and PathUnescape but no optimizations are include for the unescape functions so I don't include those benchmark results here. Reduces allocations by 10% in the existing String benchmark with a modest performance increase. name old time/op new time/op delta QueryEscape/#00-8 64.6ns ± 1% 43.8ns ± 0% -32.14% (p=0.000 n=9+9) QueryEscape/#1-8 276ns ± 3% 249ns ± 0% -9.62% (p=0.000 n=10+7) QueryEscape/#2-8 176ns ± 2% 155ns ± 3% -12.21% (p=0.000 n=10+10) QueryEscape/#3-8 388ns ± 1% 362ns ± 0% -6.55% (p=0.000 n=10+8) QueryEscape/#4-8 2.32µs ± 2% 2.27µs ± 2% -2.26% (p=0.001 n=10+10) PathEscape/#00-8 78.0ns ± 3% 63.4ns ± 1% -18.69% (p=0.000 n=10+10) PathEscape/#1-8 276ns ± 2% 260ns ± 0% -6.01% (p=0.000 n=10+10) PathEscape/#2-8 175ns ± 0% 153ns ± 0% -12.53% (p=0.000 n=8+10) PathEscape/#3-8 389ns ± 2% 361ns ± 0% -7.21% (p=0.000 n=10+9) PathEscape/#4-8 2.30µs ± 2% 2.27µs ± 1% -1.33% (p=0.001 n=9+10) String-8 3.56µs ± 4% 3.42µs ± 7% -4.00% (p=0.003 n=10+10) name old alloc/op new alloc/op delta QueryEscape/#00-8 16.0B ± 0% 8.0B ± 0% -50.00% (p=0.000 n=10+10) QueryEscape/#1-8 128B ± 0% 64B ± 0% -50.00% (p=0.000 n=10+10) QueryEscape/#2-8 64.0B ± 0% 32.0B ± 0% -50.00% (p=0.000 n=10+10) QueryEscape/#3-8 128B ± 0% 64B ± 0% -50.00% (p=0.000 n=10+10) QueryEscape/#4-8 832B ± 0% 832B ± 0% ~ (all equal) PathEscape/#00-8 32.0B ± 0% 16.0B ± 0% -50.00% (p=0.000 n=10+10) PathEscape/#1-8 128B ± 0% 64B ± 0% -50.00% (p=0.000 n=10+10) PathEscape/#2-8 64.0B ± 0% 32.0B ± 0% -50.00% (p=0.000 n=10+10) PathEscape/#3-8 128B ± 0% 64B ± 0% -50.00% (p=0.000 n=10+10) PathEscape/#4-8 704B ± 0% 704B ± 0% ~ (all equal) String-8 1.84kB ± 0% 1.66kB ± 0% -9.57% (p=0.000 n=10+10) name old allocs/op new allocs/op delta QueryEscape/#00-8 2.00 ± 0% 1.00 ± 0% -50.00% (p=0.000 n=10+10) QueryEscape/#1-8 2.00 ± 0% 1.00 ± 0% -50.00% (p=0.000 n=10+10) QueryEscape/#2-8 2.00 ± 0% 1.00 ± 0% -50.00% (p=0.000 n=10+10) QueryEscape/#3-8 2.00 ± 0% 1.00 ± 0% -50.00% (p=0.000 n=10+10) QueryEscape/#4-8 2.00 ± 0% 2.00 ± 0% ~ (all equal) PathEscape/#00-8 2.00 ± 0% 1.00 ± 0% -50.00% (p=0.000 n=10+10) PathEscape/#1-8 2.00 ± 0% 1.00 ± 0% -50.00% (p=0.000 n=10+10) PathEscape/#2-8 2.00 ± 0% 1.00 ± 0% -50.00% (p=0.000 n=10+10) PathEscape/#3-8 2.00 ± 0% 1.00 ± 0% -50.00% (p=0.000 n=10+10) PathEscape/#4-8 2.00 ± 0% 2.00 ± 0% ~ (all equal) String-8 69.0 ± 0% 61.0 ± 0% -11.59% (p=0.000 n=10+10) Updates #17860 Change-Id: I45c5e9d40b242f874c61f6ccc73bf94c494bb868 Reviewed-on: https://go-review.googlesource.com/134296 Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 0f72e79 commit 95a11c7

File tree

2 files changed

+123
-1
lines changed

2 files changed

+123
-1
lines changed

src/net/url/url.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,26 @@ func escape(s string, mode encoding) string {
304304
return s
305305
}
306306

307-
t := make([]byte, len(s)+2*hexCount)
307+
var buf [64]byte
308+
var t []byte
309+
310+
required := len(s) + 2*hexCount
311+
if required <= len(buf) {
312+
t = buf[:required]
313+
} else {
314+
t = make([]byte, required)
315+
}
316+
317+
if hexCount == 0 {
318+
copy(t, s)
319+
for i := 0; i < len(s); i++ {
320+
if s[i] == ' ' {
321+
t[i] = '+'
322+
}
323+
}
324+
return string(t)
325+
}
326+
308327
j := 0
309328
for i := 0; i < len(s); i++ {
310329
switch c := s[i]; {

src/net/url/url_test.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,3 +1754,106 @@ func TestInvalidUserPassword(t *testing.T) {
17541754
t.Errorf("error = %q; want substring %q", got, wantsub)
17551755
}
17561756
}
1757+
1758+
var escapeBenchmarks = []struct {
1759+
unescaped string
1760+
query string
1761+
path string
1762+
}{
1763+
{
1764+
unescaped: "one two",
1765+
query: "one+two",
1766+
path: "one%20two",
1767+
},
1768+
{
1769+
unescaped: "Фотки собак",
1770+
query: "%D0%A4%D0%BE%D1%82%D0%BA%D0%B8+%D1%81%D0%BE%D0%B1%D0%B0%D0%BA",
1771+
path: "%D0%A4%D0%BE%D1%82%D0%BA%D0%B8%20%D1%81%D0%BE%D0%B1%D0%B0%D0%BA",
1772+
},
1773+
1774+
{
1775+
unescaped: "shortrun(break)shortrun",
1776+
query: "shortrun%28break%29shortrun",
1777+
path: "shortrun%28break%29shortrun",
1778+
},
1779+
1780+
{
1781+
unescaped: "longerrunofcharacters(break)anotherlongerrunofcharacters",
1782+
query: "longerrunofcharacters%28break%29anotherlongerrunofcharacters",
1783+
path: "longerrunofcharacters%28break%29anotherlongerrunofcharacters",
1784+
},
1785+
1786+
{
1787+
unescaped: strings.Repeat("padded/with+various%characters?that=need$some@escaping+paddedsowebreak/256bytes", 4),
1788+
query: strings.Repeat("padded%2Fwith%2Bvarious%25characters%3Fthat%3Dneed%24some%40escaping%2Bpaddedsowebreak%2F256bytes", 4),
1789+
path: strings.Repeat("padded%2Fwith+various%25characters%3Fthat=need$some@escaping+paddedsowebreak%2F256bytes", 4),
1790+
},
1791+
}
1792+
1793+
func BenchmarkQueryEscape(b *testing.B) {
1794+
for _, tc := range escapeBenchmarks {
1795+
b.Run("", func(b *testing.B) {
1796+
b.ReportAllocs()
1797+
var g string
1798+
for i := 0; i < b.N; i++ {
1799+
g = QueryEscape(tc.unescaped)
1800+
}
1801+
b.StopTimer()
1802+
if g != tc.query {
1803+
b.Errorf("QueryEscape(%q) == %q, want %q", tc.unescaped, g, tc.query)
1804+
}
1805+
1806+
})
1807+
}
1808+
}
1809+
1810+
func BenchmarkPathEscape(b *testing.B) {
1811+
for _, tc := range escapeBenchmarks {
1812+
b.Run("", func(b *testing.B) {
1813+
b.ReportAllocs()
1814+
var g string
1815+
for i := 0; i < b.N; i++ {
1816+
g = PathEscape(tc.unescaped)
1817+
}
1818+
b.StopTimer()
1819+
if g != tc.path {
1820+
b.Errorf("PathEscape(%q) == %q, want %q", tc.unescaped, g, tc.path)
1821+
}
1822+
1823+
})
1824+
}
1825+
}
1826+
1827+
func BenchmarkQueryUnescape(b *testing.B) {
1828+
for _, tc := range escapeBenchmarks {
1829+
b.Run("", func(b *testing.B) {
1830+
b.ReportAllocs()
1831+
var g string
1832+
for i := 0; i < b.N; i++ {
1833+
g, _ = QueryUnescape(tc.query)
1834+
}
1835+
b.StopTimer()
1836+
if g != tc.unescaped {
1837+
b.Errorf("QueryUnescape(%q) == %q, want %q", tc.query, g, tc.unescaped)
1838+
}
1839+
1840+
})
1841+
}
1842+
}
1843+
1844+
func BenchmarkPathUnescape(b *testing.B) {
1845+
for _, tc := range escapeBenchmarks {
1846+
b.Run("", func(b *testing.B) {
1847+
b.ReportAllocs()
1848+
var g string
1849+
for i := 0; i < b.N; i++ {
1850+
g, _ = PathUnescape(tc.path)
1851+
}
1852+
b.StopTimer()
1853+
if g != tc.unescaped {
1854+
b.Errorf("PathUnescape(%q) == %q, want %q", tc.path, g, tc.unescaped)
1855+
}
1856+
1857+
})
1858+
}
1859+
}

0 commit comments

Comments
 (0)