Skip to content

Commit 90e9669

Browse files
committed
undo CL 44150043 / 198bdc0984dd
See https://golang.org/cl/44150043/ ««« original CL description regexp: use sync.Pool For machines, not threads. Update #4720 R=golang-dev, rsc CC=golang-dev https://golang.org/cl/44150043 »»» TBR=golang-dev CC=golang-codereviews https://golang.org/cl/48190043
1 parent 02a15e7 commit 90e9669

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/pkg/regexp/regexp.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,9 @@ type Regexp struct {
8585
subexpNames []string
8686
longest bool
8787

88-
// pool of machines for running regexp
89-
machinePool sync.Pool // of *machine
88+
// cache of machines for running regexp
89+
mu sync.Mutex
90+
machine []*machine
9091
}
9192

9293
// String returns the source text used to compile the regular expression.
@@ -174,9 +175,14 @@ func compile(expr string, mode syntax.Flags, longest bool) (*Regexp, error) {
174175
// It uses the re's machine cache if possible, to avoid
175176
// unnecessary allocation.
176177
func (re *Regexp) get() *machine {
177-
if v := re.machinePool.Get(); v != nil {
178-
return v.(*machine)
179-
}
178+
re.mu.Lock()
179+
if n := len(re.machine); n > 0 {
180+
z := re.machine[n-1]
181+
re.machine = re.machine[:n-1]
182+
re.mu.Unlock()
183+
return z
184+
}
185+
re.mu.Unlock()
180186
z := progMachine(re.prog)
181187
z.re = re
182188
return z
@@ -187,7 +193,9 @@ func (re *Regexp) get() *machine {
187193
// grow to the maximum number of simultaneous matches
188194
// run using re. (The cache empties when re gets garbage collected.)
189195
func (re *Regexp) put(z *machine) {
190-
re.machinePool.Put(z)
196+
re.mu.Lock()
197+
re.machine = append(re.machine, z)
198+
re.mu.Unlock()
191199
}
192200

193201
// MustCompile is like Compile but panics if the expression cannot be parsed.

0 commit comments

Comments
 (0)