ref: c191c8825c094e3563094104b80df47c735c95fa
parent: 17b6a133ca281986b0dfb9a1ced306e4fc99eb92
author: Ori Bernstein <[email protected]>
date: Wed Dec 27 17:04:40 EST 2017
Limit the size of the thread cache. And clean it up on exit.
--- a/lib/regex/interp.myr
+++ b/lib/regex/interp.myr
@@ -22,6 +22,7 @@
/* Ugly: for performance. std.option() should be used instead when unions get faster. */
const Zthr = (0 : rethread#)
+const Maxfree = 128
const exec = {re, str
var thr, m
@@ -164,13 +165,14 @@
next = thr.next
thrfree(re, thr)
;;
- for thr = re.freed; thr != Zthr; thr = next
+ for thr = re.free; thr != Zthr; thr = next
next = thr.next
thrfree(re, thr)
;;
re.runq = Zthr
re.expired = Zthr
- re.freed = Zthr
+ re.free = Zthr
+ re.nfree = 0
re.nexttid = 0
re.nthr = 0
}
@@ -445,9 +447,10 @@
const mkthread = {re, ip
var thr : rethread#
- if re.freed != Zthr
- thr = re.freed
- re.freed = thr.next
+ if re.free != Zthr
+ thr = re.free
+ re.free = thr.next
+ re.nfree--
else
thr = std.alloc()
;;
@@ -462,8 +465,13 @@
}
const thrfree = {re, thr
- thr.next = re.freed
- re.freed = thr
+ if re.nfree >= Maxfree
+ std.free(thr)
+ else
+ thr.next = re.free
+ re.free = thr
+ re.nfree++
+ ;;
}
const within = {re, str
--- a/lib/regex/types.myr
+++ b/lib/regex/types.myr
@@ -23,7 +23,8 @@
runq : rethread#
expired : rethread#
expiredtail : rethread#
- freed : rethread#
+ free : rethread#
+ nfree : std.size
proglen : std.size
prog : reinst[:]
nthr : std.size
@@ -71,6 +72,7 @@
mgroup : std.size[2][32] /* match starts */
;;
+
pkglocal type reinst = union
/* direct consumers */