]> Cypherpunks repositories - gostls13.git/commitdiff
regexp: use sync.Pool
authorBrad Fitzpatrick <bradfitz@golang.org>
Thu, 19 Dec 2013 00:43:19 +0000 (16:43 -0800)
committerBrad Fitzpatrick <bradfitz@golang.org>
Thu, 19 Dec 2013 00:43:19 +0000 (16:43 -0800)
For machines, not threads.

Update #4720

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/44150043

src/pkg/regexp/regexp.go

index 0046026eaebdcfe1c8ed14525c3c451ad07aa7aa..18fd049fc8146886923ea30becfa8b3ddd4d1d75 100644 (file)
@@ -85,9 +85,8 @@ type Regexp struct {
        subexpNames    []string
        longest        bool
 
-       // cache of machines for running regexp
-       mu      sync.Mutex
-       machine []*machine
+       // pool of machines for running regexp
+       machinePool sync.Pool // of *machine
 }
 
 // String returns the source text used to compile the regular expression.
@@ -175,14 +174,9 @@ 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 {
-       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()
+       if v := re.machinePool.Get(); v != nil {
+               return v.(*machine)
+       }
        z := progMachine(re.prog)
        z.re = re
        return z
@@ -193,9 +187,7 @@ 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.mu.Lock()
-       re.machine = append(re.machine, z)
-       re.mu.Unlock()
+       re.machinePool.Put(z)
 }
 
 // MustCompile is like Compile but panics if the expression cannot be parsed.