From: Oleg Bulatov Date: Fri, 23 Feb 2018 15:55:19 +0000 (+0100) Subject: regexp: Regexp shouldn't keep references to inputs X-Git-Tag: go1.11beta1~1479 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=7263540146c75de8037501b3d6fb64f59a0d1956;p=gostls13.git regexp: Regexp shouldn't keep references to inputs If you try to find something in a slice of bytes using a Regexp object, the byte array will not be released by GC until you use the Regexp object on another slice of bytes. It happens because the Regexp object keep references to the input data in its cache. Change-Id: I873107f15c1900aa53ccae5d29dbc885b9562808 Reviewed-on: https://go-review.googlesource.com/96715 Reviewed-by: Brad Fitzpatrick Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- diff --git a/src/regexp/regexp.go b/src/regexp/regexp.go index 2e4c6e8926..7d32d8d5a6 100644 --- a/src/regexp/regexp.go +++ b/src/regexp/regexp.go @@ -226,6 +226,11 @@ 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) { + // Remove references to input data that we no longer need. + z.inputBytes.str = nil + z.inputString.str = "" + z.inputReader.r = nil + re.mu.Lock() re.machine = append(re.machine, z) re.mu.Unlock()