diff util/smqueue.lua @ 14198:73903352de86

util.smqueue: Refactor checkpoint logic and add tests The previous checkpoint logic used recursion, and the code was more complex than needed due to maintenance of the self._next_checkpoint field. This switches to a loop, which should be a bit more efficient, partly because there are fewer calls (although it was a tail call) and because table lookups can now be cached between each execution. _call_checkpoints() is now called unconditionally for every ack, which is simpler though adds a function call, it should be more than balanced out by the other changes.
author Matthew Wild <mwild1@gmail.com>
date Tue, 26 May 2026 15:36:36 +0100
parents 1e01b91cf94d
children
line wrap: on
line diff
--- a/util/smqueue.lua	Tue May 26 12:31:32 2026 +0100
+++ b/util/smqueue.lua	Tue May 26 15:36:36 2026 +0100
@@ -31,10 +31,8 @@
 		table.insert(acked, v);
 	end
 
-	local c = self._next_checkpoint;
-	if c and h >= c then
-		self:_call_checkpoints(h);
-	end
+	self:_call_checkpoints(h);
+
 	return acked
 end
 
@@ -68,22 +66,17 @@
 
 function smqueue:add_checkpoint(cb, ud)
 	table.insert(self._checkpoints, { self._head; cb; ud });
-	if not self._next_checkpoint then
-		self._next_checkpoint = self._head;
-	end
 end
 
 function smqueue:_call_checkpoints(h)
 	local checkpoints = self._checkpoints;
-
-	local c = table.remove(checkpoints, 1);
-	self._next_checkpoint = checkpoints[1] and checkpoints[1][1] or nil;
+	local c = checkpoints[1];
 
-	local cb, ud = c[2], c[3];
-	cb(ud);
-
-	if h >= self._next_checkpoint then
-		return self:_call_checkpoints(h)
+	while c and h >= c[1] do
+		table.remove(checkpoints, 1);
+		local cb, ud = c[2], c[3];
+		cb(ud);
+		c = checkpoints[1];
 	end
 end