Commit b682f6de authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

regexp: use sync.Pool

For machines, not threads.

Update #4720

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/44150043
parent 4890502a
...@@ -85,9 +85,8 @@ type Regexp struct { ...@@ -85,9 +85,8 @@ type Regexp struct {
subexpNames []string subexpNames []string
longest bool longest bool
// cache of machines for running regexp // pool of machines for running regexp
mu sync.Mutex machinePool sync.Pool // of *machine
machine []*machine
} }
// String returns the source text used to compile the regular expression. // 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) { ...@@ -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 // It uses the re's machine cache if possible, to avoid
// unnecessary allocation. // unnecessary allocation.
func (re *Regexp) get() *machine { func (re *Regexp) get() *machine {
re.mu.Lock() if v := re.machinePool.Get(); v != nil {
if n := len(re.machine); n > 0 { return v.(*machine)
z := re.machine[n-1]
re.machine = re.machine[:n-1]
re.mu.Unlock()
return z
} }
re.mu.Unlock()
z := progMachine(re.prog) z := progMachine(re.prog)
z.re = re z.re = re
return z return z
...@@ -193,9 +187,7 @@ func (re *Regexp) get() *machine { ...@@ -193,9 +187,7 @@ func (re *Regexp) get() *machine {
// grow to the maximum number of simultaneous matches // grow to the maximum number of simultaneous matches
// run using re. (The cache empties when re gets garbage collected.) // run using re. (The cache empties when re gets garbage collected.)
func (re *Regexp) put(z *machine) { func (re *Regexp) put(z *machine) {
re.mu.Lock() re.machinePool.Put(z)
re.machine = append(re.machine, z)
re.mu.Unlock()
} }
// MustCompile is like Compile but panics if the expression cannot be parsed. // MustCompile is like Compile but panics if the expression cannot be parsed.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment