diff options
author | Matthew Wild <mwild1@gmail.com> | 2022-10-11 11:37:55 +0100 |
---|---|---|
committer | Matthew Wild <mwild1@gmail.com> | 2022-10-11 11:37:55 +0100 |
commit | ffa72d829be2b988074f269fc5a2177b74c77ab4 (patch) | |
tree | c318824aa4423563fa383bfb6589260ea7d81d6d /util | |
parent | 586a0d8493acfd511a2d2319ad68628d585f94df (diff) | |
download | prosody-ffa72d829be2b988074f269fc5a2177b74c77ab4.tar.gz prosody-ffa72d829be2b988074f269fc5a2177b74c77ab4.zip |
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.
Diffstat (limited to 'util')
-rw-r--r-- | util/dbuffer.lua | 8 |
1 files changed, 6 insertions, 2 deletions
diff --git a/util/dbuffer.lua b/util/dbuffer.lua index 6c671fa3..0a36288d 100644 --- a/util/dbuffer.lua +++ b/util/dbuffer.lua @@ -91,8 +91,12 @@ function dbuffer_methods:read_until(char) 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); |