Skip to content

Commit f9a1060

Browse files
authored
⚡ improve require time (#111)
decrease require time (cost on startup) from `0.791636 ms` to `4.140682` (423% faster)
1 parent f1051e5 commit f9a1060

5 files changed

Lines changed: 60 additions & 30 deletions

File tree

doc/sqlite.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ sqlite.db:select({tbl_name}, {spec}) *sqlite.db:select()*
481481
table[]
482482

483483

484-
sqlite.db:tbl({tbl_name}, {opts}) *sqlite.db:tbl()*
484+
sqlite.db:tbl({tbl_name}, {schema}) *sqlite.db:tbl()*
485485
Create new sqlite_tbl object. If {opts}.ensure = false, then on each run it
486486
will drop the table and recreate it. NOTE: this might change someday to
487487
alter the table instead. For now alteration is auto and limited to field
@@ -506,7 +506,7 @@ sqlite.db:tbl({tbl_name}, {opts}) *sqlite.db:tbl()*
506506
Parameters: ~
507507
{tbl_name} (string) the name of the table. can be new or
508508
existing one.
509-
{opts} (sqlite_schema_dict) {schema, ensure (defalut true)}
509+
{schema} (sqlite_schema_dict) {schema, ensure (defalut true)}
510510

511511
Return: ~
512512
sqlite_tbl

lua/sqlite/db.lua

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
---@brief ]]
44
---@tag sqlite.db.lua
55

6+
local u = require "sqlite.utils"
7+
local require = u.require_on_index
8+
69
local sqlite = {}
710
---@type sqlite_db
811
sqlite.db = {}
@@ -11,11 +14,10 @@ sqlite.db.__version = "v1.0.0"
1114

1215
local clib = require "sqlite.defs"
1316
local s = require "sqlite.stmt"
14-
local u = require "sqlite.utils"
1517
local h = require "sqlite.helpers"
1618
local a = require "sqlite.assert"
1719
local p = require "sqlite.parser"
18-
local flags = clib.flags
20+
local tbl = require "sqlite.tbl"
1921

2022
---Creates a new sqlite.lua object, without creating a connection to uri.
2123
---|sqlite.new| is identical to |sqlite.db:open| but it without opening sqlite db
@@ -279,7 +281,7 @@ function sqlite.db:eval(statement, params)
279281
stmt:finalize()
280282

281283
-- if no rows is returned, then check return the result of errcode == flags.ok
282-
res = rawequal(next(res), nil) and clib.last_errcode(self.conn) == flags.ok or res
284+
res = rawequal(next(res), nil) and clib.last_errcode(self.conn) == clib.flags.ok or res
283285

284286
-- fix res of its table, so that select all doesn't return { [1] = {[1] = { row }} }
285287
if type(res) == "table" and res[2] == nil and u.is_nested(res[1]) then
@@ -595,14 +597,14 @@ end
595597
---```
596598
---</pre>
597599
---@param tbl_name string: the name of the table. can be new or existing one.
598-
---@param opts sqlite_schema_dict: {schema, ensure (defalut true)}
600+
---@param schema sqlite_schema_dict: {schema, ensure (defalut true)}
599601
---@see |sqlite.tbl.new|
600602
---@return sqlite_tbl
601603
function sqlite.db:tbl(tbl_name, schema)
602604
if type(self) == "string" then
603-
return require("sqlite.tbl").new(self, schema)
605+
return tbl.new(self, schema)
604606
end
605-
return require("sqlite.tbl").new(tbl_name, schema, self)
607+
return tbl.new(tbl_name, schema, self)
606608
end
607609

608610
---DEPRECATED

lua/sqlite/init.lua

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,14 @@
7272
---| '"cascade"' : propagates the delete or update operation on the parent key to each dependent child key.
7373

7474
---@type sqlite_db
75-
return require "sqlite.db"
75+
return setmetatable({}, {
76+
__index = function(_, key)
77+
return require("sqlite.db")[key]
78+
end,
79+
__newindex = function(_)
80+
error "sqlite.lua: this shouldn't happen. Mutating sqlite base object is prohibited."
81+
end,
82+
__call = function(_, ...)
83+
return require "sqlite.db"(...)
84+
end,
85+
})

lua/sqlite/stmt.lua

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,14 @@ local clib = require "sqlite.defs"
66
local flags = clib.flags
77

88
---@class sqlstmt @Object to deal with sqlite statements
9+
---@field pstmt sqlite_blob sqlite_pstmt
10+
---@field conn sqlite_blob sqlite3 object
11+
---@field str string original statement
912
local sqlstmt = {}
1013
sqlstmt.__index = sqlstmt
1114

15+
--- TODO: refactor and make parser.lua required here only.
16+
1217
---Parse a statement
1318
---@param conn sqlite3: the database connection.
1419
---@param str string: the sqlite statement to be parsed.
@@ -18,29 +23,21 @@ sqlstmt.__index = sqlstmt
1823
function sqlstmt:parse(conn, str)
1924
assert(clib.type_of(conn) == clib.type_of_db_ptr, "Invalid connection passed to sqlstmt:parse")
2025
assert(type(str) == "string", "Invalid second argument passed to sqlstmt:parse")
21-
local o = {
26+
local o = setmetatable({
2227
str = str,
2328
conn = conn,
2429
finalized = false,
25-
}
26-
setmetatable(o, self)
27-
o:__parse()
28-
return o
29-
end
30+
}, sqlstmt)
3031

31-
---Parse self.str into an sqlite representation and set it to self.pstmt.
32-
function sqlstmt:__parse()
3332
local pstmt = clib.get_new_stmt_ptr()
34-
local code = clib.prepare_v2(self.conn, self.str, #self.str, pstmt, nil)
33+
local code = clib.prepare_v2(o.conn, o.str, #o.str, pstmt, nil)
34+
3535
assert(
3636
code == flags.ok,
37-
string.format(
38-
"sqlite.lua: sql statement parse, , stmt: `%s`, err: `(`%s`)`",
39-
self.str,
40-
clib.to_str(clib.errmsg(self.conn))
41-
)
37+
("sqlite.lua: sql statement parse, , stmt: `%s`, err: `(`%s`)`"):format(o.str, clib.last_errmsg(o.conn))
4238
)
43-
self.pstmt = pstmt[0]
39+
o.pstmt = pstmt[0]
40+
return o
4441
end
4542

4643
---Resets the parsed statement. required for parsed statements to be re-executed.
@@ -69,7 +66,7 @@ function sqlstmt:finalize()
6966
end
7067

7168
---Called before evaluating the (next iteration) of the prepared statement.
72-
---@return sqlite_flag: Possible Flags: { flags.busy, flags.done, flags.row, flags.error, flags.misuse }
69+
---@return sqlite_flags: Possible Flags: { flags.busy, flags.done, flags.row, flags.error, flags.misuse }
7370
function sqlstmt:step()
7471
local step_code = clib.step(self.pstmt)
7572
assert(
@@ -283,7 +280,6 @@ local bind_type_to_func = {
283280
---If {args[1]} is a number and {args[2]} is a value then it binds by index.
284281
---Else first argument is a table, then it binds the table to indicies, and it
285282
---works with named and unnamed.
286-
---@param ...: Either a index value pair or a table
287283
---@varargs if {args[1]} number and {args[2]} or {args[1]} table
288284
---@see sqlstmt:nparam
289285
---@see sqlstmt:param
@@ -342,15 +338,15 @@ end
342338
---@param idx number: index starting at 1
343339
---@param pointer sqlite_blob: blob to bind
344340
---@param size number: pointer size
345-
---@return sqlite_flag
341+
---@return sqlite_flags
346342
function sqlstmt:bind_blob(idx, pointer, size)
347343
return clib.bind_blob64(self.pstmt, idx, pointer, size, nil) -- Always 64? or two functions
348344
end
349345

350346
---Binds zeroblob at {idx} with {size}
351347
---@param idx number: index starting at 1
352348
---@param size number: zeroblob size
353-
---@return sqlite_flag
349+
---@return sqlite_flags
354350
function sqlstmt:bind_zeroblob(idx, size)
355351
return clib.bind_zeroblob64(self.pstmt, idx, size)
356352
end
@@ -385,15 +381,15 @@ function sqlstmt:params()
385381
end
386382

387383
---Clear the current bindings.
388-
---@return sqlite_flag
384+
---@return sqlite_flags
389385
function sqlstmt:bind_clear()
390386
self.current_bind_index = nil
391387
return clib.clear_bindings(self.pstmt)
392388
end
393389

394390
---Bind the value at the next index until all values are bound
395391
---@param value any: value to bind
396-
---@return sqlite_flag
392+
---@return sqlite_flags
397393
---@TODO does it return sqlite_flag in all cases? @conni
398394
function sqlstmt:bind_next(value)
399395
if not self.current_bind_index then

lua/sqlite/utils.lua

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,4 +235,26 @@ M.flatten = function(tbl)
235235
return result
236236
end
237237

238+
--- taken from https://github.com/tjdevries/lazy.nvim/blob/master/lua/lazy.lua
239+
M.require_on_exported_call = function(require_path)
240+
return setmetatable({}, {
241+
__index = function(_, k)
242+
return function(...)
243+
return require(require_path)[k](...)
244+
end
245+
end,
246+
})
247+
end
248+
249+
M.require_on_index = function(require_path)
250+
return setmetatable({}, {
251+
__index = function(_, key)
252+
return require(require_path)[key]
253+
end,
254+
255+
__newindex = function(_, key, value)
256+
require(require_path)[key] = value
257+
end,
258+
})
259+
end
238260
return M

0 commit comments

Comments
 (0)