Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(294)

Unified Diff: src/pkg/regexp/regexp.go

Issue 44150043: code review 44150043: regexp: use sync.Pool (Closed)
Patch Set: diff -r afe351cefb6f https://go.googlecode.com/hg/ Created 10 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/pkg/regexp/regexp.go
===================================================================
--- a/src/pkg/regexp/regexp.go
+++ b/src/pkg/regexp/regexp.go
@@ -85,9 +85,8 @@
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 @@
// 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
+ if v := re.machinePool.Get(); v != nil {
+ return v.(*machine)
}
- re.mu.Unlock()
z := progMachine(re.prog)
z.re = re
return z
@@ -193,9 +187,7 @@
// 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.
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b