shithub: mc

Download patch

ref: a434ff7d4dfa4121e1070bbba0754bbbd04630c4
parent: 3ca7a098ddb3aacabafb6c20a57feec1dbafe4ab
author: Ori Bernstein <ori@eigenstate.org>
date: Wed Dec 27 16:03:13 EST 2017

Speed up regex.

	Double performance.

--- a/lib/regex/interp.myr
+++ b/lib/regex/interp.myr
@@ -235,7 +235,6 @@
 		re.traces = [][:]
 		std.slpush(&re.traces, std.mkbs())
 	;;
-	re.runq.mgroup = std.slalloc(re.nmatch)
 	for var i = 0; i < re.nmatch; i++
 		re.runq.mgroup[i][0] = -1
 		re.runq.mgroup[i][1] = -1
@@ -306,7 +305,7 @@
 */
 const step = {re, thr, curip
 	var str
-	var mgroup
+	var nthr
 
 	str = re.str
 	match re.prog[thr.ip]
@@ -377,7 +376,6 @@
 		;;
 	| `Ilbra m:
 		trace(re, thr, "\t{}:\tLbra {}\n", thr.ip, m)
-		trace(re, thr, "\t\tmatch start = {}\n", re.strp)
 		thr.mgroup[m][0] = re.strp
 		hit(re, thr)
 		thr.ip++
@@ -390,9 +388,13 @@
 		-> false
 	| `Ifork (lip, rip):
 		trace(re, thr, "\t{}:\tFork ({}, {})\n", thr.ip, lip, rip)
-		mgroup = std.sldup(thr.mgroup)
 		hit(re, thr)
-		fork(re, rip, curip, mgroup)
+		if rip != curip
+			nthr = mkthread(re, rip)
+			nthr.next = re.runq
+			nthr.mgroup = thr.mgroup
+			re.runq = nthr
+		;;
 		if re.debug
 			std.slpush(&re.traces, std.bsdup(re.traces[thr.tid]))
 		;;
@@ -412,18 +414,6 @@
 	-> true
 }
 
-const fork = {re, ip, curip, mgroup
-	var thr
-
-	if ip == curip /* loop detection */
-		-> void
-	;;
-	thr = mkthread(re, ip)
-	thr.next = re.runq
-	thr.mgroup = mgroup
-	re.runq = thr
-}
-
 const die = {re, thr, msg
         /*
  	  we can have die called on a thread
@@ -450,13 +440,17 @@
 const mkthread = {re, ip
 	var thr : rethread#
 
-	thr = std.alloc()
+	if re.freed != Zthr
+		thr = re.freed
+		re.freed = thr.next
+	else
+		thr = std.alloc()
+	;;
 	thr.next = Zthr
 	thr.ip = ip
 	thr.tid = re.nexttid++
 	thr.dead = false
 	thr.matched = false
-	thr.mgroup = [][:]
 	re.nthr++
 
 	-> thr
@@ -463,8 +457,8 @@
 }
 
 const thrfree = {re, thr
-	std.slfree(thr.mgroup)
-	std.free(thr)
+	thr.next = re.freed
+	re.freed = thr
 }
 
 const within = {re, str 
--- a/lib/regex/types.myr
+++ b/lib/regex/types.myr
@@ -23,6 +23,7 @@
 		runq	: rethread#
 		expired	: rethread#
 		expiredtail	: rethread#
+		freed	: rethread#
 		proglen	: std.size
 		prog	: reinst[:]
 		nthr	: std.size
@@ -68,7 +69,7 @@
 		dead	: bool		/* thread died */
 		matched	: bool		/* thread matched */
 
-		mgroup	: std.size[2][:]	/* match starts */
+		mgroup	: std.size[2][32]	/* match starts */
 	;;
 
 	pkglocal type reinst = union