From 90e9669c50fc471e14b358382a60b6354182fb2d Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Mon, 6 Jan 2014 12:38:04 -0800 Subject: [PATCH] undo CL 44150043 / 198bdc0984dd MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 --- src/pkg/regexp/regexp.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/pkg/regexp/regexp.go b/src/pkg/regexp/regexp.go index 18fd049fc8..0046026eae 100644 --- a/src/pkg/regexp/regexp.go +++ b/src/pkg/regexp/regexp.go @@ -85,8 +85,9 @@ type Regexp struct { subexpNames []string longest bool - // pool of machines for running regexp - machinePool sync.Pool // of *machine + // cache of machines for running regexp + mu sync.Mutex + machine []*machine } // 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) { // It uses the re's machine cache if possible, to avoid // unnecessary allocation. func (re *Regexp) get() *machine { - if v := re.machinePool.Get(); v != nil { - return v.(*machine) - } + re.mu.Lock() + if n := len(re.machine); n > 0 { + z := re.machine[n-1] + re.machine = re.machine[:n-1] + re.mu.Unlock() + return z + } + re.mu.Unlock() z := progMachine(re.prog) z.re = re return z @@ -187,7 +193,9 @@ func (re *Regexp) get() *machine { // grow to the maximum number of simultaneous matches // run using re. (The cache empties when re gets garbage collected.) func (re *Regexp) put(z *machine) { - re.machinePool.Put(z) + re.mu.Lock() + re.machine = append(re.machine, z) + re.mu.Unlock() } // MustCompile is like Compile but panics if the expression cannot be parsed. -- 2.48.1