shithub: mc

Download patch

ref: d0c579ea046b64d878d6fd0d9bcc8f0af9fdde73
parent: 037c644bc26ee88d2fd2b0c9988bbcbdeaf46659
author: Ori Bernstein <[email protected]>
date: Mon Sep 4 18:54:37 EDT 2017

Reduce the number of allocations in regex.

	365us to 262.us

--- a/lib/regex/interp.myr
+++ b/lib/regex/interp.myr
@@ -127,7 +127,7 @@
 			std.sbputb(sb, str[i])
 			i++
 		else
-			len = thr.mend[0]
+			len = thr.mgroup[0][1]
 			s = str[i:len + i]
 			dosubst(sb, re, thr, s, subst)
 			i += len
@@ -142,10 +142,10 @@
 
 	off = 0
 	for var i = 1; i < re.nmatch; i++
-		if thr.mstart[i] != -1 && thr.mend[i] != -1
-			std.sbputs(sb, str[off:thr.mstart[i]])
+		if thr.mgroup[i][0] != -1 && thr.mgroup[i][1] != -1
+			std.sbputs(sb, str[off:thr.mgroup[i][0]])
 			std.sbputs(sb, subst[i - 1])
-			off = thr.mend[i]
+			off = thr.mgroup[i][1]
 		;;
 	;;
 	std.sbputs(sb, str[off:])
@@ -182,8 +182,8 @@
 	;;
 	ret = std.slalloc(re.nmatch)
 	for var i = 0; i < re.nmatch; i++
-		if thr.mstart[i] != -1 && thr.mend[i] != -1
-			ret[i] = re.str[thr.mstart[i]:thr.mend[i]]
+		if thr.mgroup[i][0] != -1 && thr.mgroup[i][1] != -1
+			ret[i] = re.str[thr.mgroup[i][0]:thr.mgroup[i][1]]
 		else
 			ret[i] = [][:]
 		;;
@@ -200,8 +200,8 @@
 	;;
 	ret = std.slalloc(re.nmatch)
 	for var i = 0; i < re.nmatch; i++
-		if thr.mstart[i] != -1 && thr.mend[i] != -1
-			ret[i] = (thr.mstart[i], thr.mend[i])
+		if thr.mgroup[i][0] != -1 && thr.mgroup[i][1] != -1
+			ret[i] = (thr.mgroup[i][0], thr.mgroup[i][1])
 		else
 			ret[i] = (-1, -1)
 		;;
@@ -233,11 +233,10 @@
 		re.traces = [][:]
 		std.slpush(&re.traces, std.mkbs())
 	;;
-	re.runq.mstart = std.slalloc(re.nmatch)
-	re.runq.mend = std.slalloc(re.nmatch)
+	re.runq.mgroup = std.slalloc(re.nmatch)
 	for var i = 0; i < re.nmatch; i++
-		re.runq.mstart[i] = -1
-		re.runq.mend[i] = -1
+		re.runq.mgroup[i][0] = -1
+		re.runq.mgroup[i][1] = -1
 	;;
 	while re.nthr > 0
 		while re.runq != Zthr
@@ -302,8 +301,7 @@
 */
 const step = {re, thr, curip
 	var str
-	var mstart
-	var mend
+	var mgroup
 
 	str = re.str
 	match re.prog[thr.ip]
@@ -375,22 +373,21 @@
 	| `Ilbra m:
 		trace(re, thr, "\t{}:\tLbra {}\n", thr.ip, m)
 		trace(re, thr, "\t\tmatch start = {}\n", re.strp)
-		thr.mstart[m] = re.strp
+		thr.mgroup[m][0] = re.strp
 		hit(re, thr)
 		thr.ip++
 		-> false
 	| `Irbra m:
 		trace(re, thr, "\t{}:\tRbra {}\n", thr.ip, m)
-		thr.mend[m] = re.strp
+		thr.mgroup[m][1] = re.strp
 		hit(re, thr)
 		thr.ip++
 		-> false
 	| `Ifork (lip, rip):
 		trace(re, thr, "\t{}:\tFork ({}, {})\n", thr.ip, lip, rip)
-		mstart = std.sldup(thr.mstart)
-		mend = std.sldup(thr.mend)
+		mgroup = std.sldup(thr.mgroup)
 		hit(re, thr)
-		fork(re, rip, curip, mstart, mend)
+		fork(re, rip, curip, mgroup)
 		if re.debug
 			std.slpush(&re.traces, std.bsdup(re.traces[thr.tid]))
 		;;
@@ -410,7 +407,7 @@
 	-> true
 }
 
-const fork = {re, ip, curip, mstart, mend
+const fork = {re, ip, curip, mgroup
 	var thr
 
 	if ip == curip /* loop detection */
@@ -418,8 +415,7 @@
 	;;
 	thr = mkthread(re, ip)
 	thr.next = re.runq
-	thr.mstart = mstart
-	thr.mend = mend
+	thr.mgroup = mgroup
 	re.runq = thr
 }
 
@@ -458,8 +454,7 @@
 	thr.dead = false
 	thr.matched = false
 
-	thr.mstart = [][:]
-	thr.mend = [][:]
+	thr.mgroup = [][:]
 
 	re.nthr++
 
@@ -468,8 +463,7 @@
 
 const thrfree = {re, thr
 	trace(re, thr, "\t\tcleanup {}\n", thr.tid)
-	std.slfree(thr.mstart)
-	std.slfree(thr.mend)
+	std.slfree(thr.mgroup)
 	std.free(thr)
 }
 
--- a/lib/regex/types.myr
+++ b/lib/regex/types.myr
@@ -68,8 +68,7 @@
 		dead	: bool		/* thread died */
 		matched	: bool		/* thread matched */
 
-		mstart	: std.size[:]	/* match starts */
-		mend	: std.size[:]	/* match ends */
+		mgroup	: std.size[2][:]	/* match starts */
 	;;
 
 	pkglocal type reinst = union