Comparison

util/dbuffer.lua @ 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
parent 12762:79b89f382290
child 12975:d10957394a3c
comparison
equal deleted inserted replaced
12762:79b89f382290 12763:d26eefe98d09
89 end 89 end
90 return nil; 90 return nil;
91 end 91 end
92 92
93 function dbuffer_methods:discard(requested_bytes) 93 function dbuffer_methods:discard(requested_bytes)
94 if requested_bytes > self._length then 94 if self._length == 0 then return true; end
95 return nil; 95 if not requested_bytes or requested_bytes >= self._length then
96 self.front_consumed = 0;
97 self._length = 0;
98 for _ in self.items:consume() do end
99 return true;
96 end 100 end
97 101
98 local chunk, read_bytes = self:read_chunk(requested_bytes); 102 local chunk, read_bytes = self:read_chunk(requested_bytes);
99 requested_bytes = requested_bytes - read_bytes; 103 requested_bytes = requested_bytes - read_bytes;
100 if requested_bytes == 0 then -- Already read everything we need 104 if requested_bytes == 0 then -- Already read everything we need