Skip to content

Commit

Permalink
feat: improve performance (chaitin#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
blaisewang authored Aug 3, 2023
1 parent 3a03524 commit 16b3935
Show file tree
Hide file tree
Showing 6 changed files with 285 additions and 200 deletions.
4 changes: 4 additions & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
std = "ngx_lua"
redefined = false
max_line_length = 130
max_code_line_length = 130
max_string_line_length = 130
max_comment_line_length = 130
30 changes: 8 additions & 22 deletions lib/resty/t1k/buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,33 @@ local _M = {
_VERSION = '1.0.0',
}

function _M:new ()
local o = {}
function _M:new (o)
o = o or {}
setmetatable(o, self)
self.__index = self
return o
end

function _M:add_lf ()
self:add("\x0a")
end

function _M:add_crlf ()
self:add("\x0d\x0a")
end

function _M:add (...)
for _, v in ipairs({ ... }) do
table.insert(self, v)
end
end

function _M:add_kv_lf (k, v)
self:add(k, ":", v)
self:add_lf()
function _M:add_crlf ()
self:add("\r\n")
end

function _M:add_kv_crlf (k, v)
self:add(k, ": ", v)
self:add_crlf()
end

function _M:tostring (sep, i, j)
return table.concat(self, sep, i, j)
self:add(k, ": ", v, "\r\n")
end

function _M:len()
local l = 0
local len = 0
for _, v in ipairs(self) do
l = l + #v
len = len + #v
end
return l
return len
end

return _M
24 changes: 21 additions & 3 deletions lib/resty/t1k/file.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@
local buffer = require "resty.t1k.buffer"

local _M = {
_VERSION = '1.0.0'
}

function _M.read(p)
local buffer_size = 2 ^ 13

function _M.read(p, size)
size = (not size or size < 0) and 0 or size

local f, err = io.open(p, "rb")
if not f or err then
return nil, err, nil
end

local c = f:read("*all")
local left = size
local buf = buffer:new()

while left ~= 0 do
local block_size = math.min(left, buffer_size)
local block = f:read(block_size)
if not block then
break
end
buf:add(block)
left = math.max(left - block_size, 0)
end
f:close()
return true, nil, c

return true, nil, buf
end

return _M
Loading

0 comments on commit 16b3935

Please sign in to comment.