Changeset

12763:d26eefe98d09

util.dbuffer: Add efficient shortcuts for discard() in certain cases If the buffer is already empty, nothing to do. If we're throwing away the whole buffer, we can just empty it and avoid read_chunk() (which in turn may collapse()). These shortcuts are much more efficient.
author Matthew Wild <mwild1@gmail.com>
date Tue, 11 Oct 2022 11:37:55 +0100
parents 12762:79b89f382290
children 12764:bf6d2f9fad4d
files util/dbuffer.lua
diffstat 1 files changed, 6 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/util/dbuffer.lua	Tue Oct 11 11:34:47 2022 +0100
+++ b/util/dbuffer.lua	Tue Oct 11 11:37:55 2022 +0100
@@ -91,8 +91,12 @@
 end
 
 function dbuffer_methods:discard(requested_bytes)
-	if requested_bytes > self._length then
-		return nil;
+	if self._length == 0 then return true; end
+	if not requested_bytes or requested_bytes >= self._length then
+		self.front_consumed = 0;
+		self._length = 0;
+		for _ in self.items:consume() do end
+		return true;
 	end
 
 	local chunk, read_bytes = self:read_chunk(requested_bytes);