aboutsummaryrefslogtreecommitdiff
path: root/src/pkg/strings/replace.go
diff options
context:
space:
mode:
authorRui Ueyama <ruiu@google.com>2014-06-22 15:26:30 -0700
committerRui Ueyama <ruiu@google.com>2014-06-22 15:26:30 -0700
commita200e0b8fda46dadb1474248f21d44a16bd92dd2 (patch)
treed41cfe44b87f00f358f94b8f8fb05bb6b60dcd9b /src/pkg/strings/replace.go
parent5b342f78043389e35a6c8c6ee1030ae733570da1 (diff)
downloadgo-a200e0b8fda46dadb1474248f21d44a16bd92dd2.tar.xz
undo CL 101330053 / c19c9a063fe8
sync.Pool is not supposed to be used everywhere, but is a last resort. ««« original CL description strings: use sync.Pool to cache buffer benchmark old ns/op new ns/op delta BenchmarkByteReplacerWriteString 3596 3094 -13.96% benchmark old allocs new allocs delta BenchmarkByteReplacerWriteString 1 0 -100.00% LGTM=dvyukov R=bradfitz, dave, dvyukov CC=golang-codereviews https://golang.org/cl/101330053 »»» LGTM=dave R=r, dave CC=golang-codereviews https://golang.org/cl/102610043
Diffstat (limited to 'src/pkg/strings/replace.go')
-rw-r--r--src/pkg/strings/replace.go33
1 files changed, 13 insertions, 20 deletions
diff --git a/src/pkg/strings/replace.go b/src/pkg/strings/replace.go
index 89aca95bae..cb9d7b1fa4 100644
--- a/src/pkg/strings/replace.go
+++ b/src/pkg/strings/replace.go
@@ -4,10 +4,7 @@
package strings
-import (
- "io"
- "sync"
-)
+import "io"
// A Replacer replaces a list of strings with replacements.
type Replacer struct {
@@ -454,31 +451,27 @@ func (r *byteReplacer) Replace(s string) string {
return string(buf)
}
-var bufferPool = sync.Pool{
- New: func() interface{} {
- b := make([]byte, 4096)
- return &b
- },
-}
-
func (r *byteReplacer) WriteString(w io.Writer, s string) (n int, err error) {
- bp := bufferPool.Get().(*[]byte)
- buf := *bp
+ // TODO(bradfitz): use io.WriteString with slices of s, avoiding allocation.
+ bufsize := 32 << 10
+ if len(s) < bufsize {
+ bufsize = len(s)
+ }
+ buf := make([]byte, bufsize)
+
for len(s) > 0 {
- ncopy := copy(buf, s)
+ ncopy := copy(buf, s[:])
+ s = s[ncopy:]
for i, b := range buf[:ncopy] {
buf[i] = r.new[b]
}
- s = s[ncopy:]
- var wn int
- wn, err = w.Write(buf[:ncopy])
+ wn, err := w.Write(buf[:ncopy])
n += wn
if err != nil {
- break
+ return n, err
}
}
- bufferPool.Put(bp)
- return
+ return n, nil
}
// byteStringReplacer is the implementation that's used when all the