Skip to content

Commit

Permalink
perf(bufio): reduce memory usage
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <zhaojh329@gmail.com>
  • Loading branch information
zhaojh329 committed Oct 7, 2023
1 parent 8e0aabf commit 54fa191
Showing 1 changed file with 16 additions and 22 deletions.
38 changes: 16 additions & 22 deletions bufio.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,17 @@ local M = {}
local methods = {}

function methods:size()
local mt = getmetatable(self)
return mt.b:size()
return self.b:size()
end

function methods:length()
local mt = getmetatable(self)
return mt.b:length()
return self.b:length()
end

-- Returns the next n bytes without advancing the reader
function methods:peek(n, timeout)
local mt = getmetatable(self)
local reader = mt.reader
local b = mt.b
local reader = self.reader
local b = self.b
local blen = b:length()

if blen < n then
Expand All @@ -52,9 +49,8 @@ end
an error message, followed a number indicate how many bytes have been discarded.
--]]
function methods:discard(n, timeout)
local mt = getmetatable(self)
local reader = mt.reader
local b = mt.b
local reader = self.reader
local b = self.b

if n < 1 then
return 0
Expand All @@ -81,9 +77,8 @@ end

-- Reads at most n bytes.
function methods:read(n, timeout)
local mt = getmetatable(self)
local reader = mt.reader
local b = mt.b
local reader = self.reader
local b = self.b

if n == 0 then
return ''
Expand All @@ -106,9 +101,8 @@ end
-- Reads exactly n bytes. Returns nil followed by
-- an error message if fewer bytes were read.
function methods:readfull(n, timeout)
local mt = getmetatable(self)
local reader = mt.reader
local b = mt.b
local reader = self.reader
local b = self.b
local blen = b:length()

if blen >= n then
Expand Down Expand Up @@ -155,9 +149,8 @@ end

-- Reads a single line, not including the end-of-line bytes("\r\n" or "\n").
function methods:readline(timeout)
local mt = getmetatable(self)
local reader = mt.reader
local b = mt.b
local reader = self.reader
local b = self.b

local deadtime

Expand Down Expand Up @@ -239,6 +232,8 @@ local function default_read2b(rd, b, timeout)
return r, err
end

local metatable = { __index = methods }

function M.new(reader, size)
if type(reader) ~= 'table' then
error('"reader" must be a table')
Expand Down Expand Up @@ -267,11 +262,10 @@ function M.new(reader, size)

local b = buffer.new(size)

return setmetatable({}, {
return setmetatable({
b = b,
reader = reader,
__index = methods
})
}, metatable)
end

return M

0 comments on commit 54fa191

Please sign in to comment.