Skip to content
This repository was archived by the owner on Mar 20, 2025. It is now read-only.

Commit 0cf8e0d

Browse files
committed
Double the speed of node:set() by removing the redundant type check. 40% shorter node:get()
1 parent dbb4eb3 commit 0cf8e0d

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

yottadb.lua

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -966,15 +966,15 @@ end
966966
-- @param[opt] default specify the value to return if the node has no data; if not supplied, `nil` is the default
967967
-- @return value of the node
968968
-- @see get
969-
function node:get(default) return M.get(self) or default end
969+
function node:get(default) return _yottadb.get(self) or default end
970970

971971
--- Set `node`'s value.
972972
-- Equivalent to `node.__ = x`, but 4x slower.
973973
-- If node is subclassed, then `node.__ = x` invokes the subclass's `node:__set(x)` if it exists.
974974
-- @param value New value or `nil` to delete node
975975
-- @return value
976976
-- @see set
977-
function node:set(value) assert_type(value, _string_number_nil, 1) return M.set(self, value) end
977+
function node:set(value) return _yottadb.set(self, value) end
978978

979979
--- Delete database tree (node and subnodes) pointed to by node object.
980980
-- @see node:kill
@@ -1215,7 +1215,7 @@ function node:__pairs(reverse)
12151215
local subscript = actuator(subnode)
12161216
subnode = _yottadb.cachearray_subst(subnode, subscript or '')
12171217
if subscript==nil then return nil end
1218-
local value = M.get(subnode)
1218+
local value = _yottadb.get(subnode)
12191219
return subnode, value, subscript
12201220
end
12211221
return iterator, nil, '' -- iterate using child from ''
@@ -1313,9 +1313,7 @@ end
13131313
-- and if so, invokes the method itself if self is a member of metatable 'node'
13141314
-- @param k Node attribute to look up
13151315
function node:__index(k)
1316-
-- todo: replace table ref M.get with a local function get() for 40% faster
1317-
-- this will prevent substituting M.get with another function, but that is better done with inheritance anyway
1318-
if k == '__' then return M.get(self) end -- fast but only works if metatable is top-level superclass node
1316+
if k == '__' then return _yottadb.get(self) end -- fast but only works if metatable is top-level superclass node, cf. subclass_index alternate
13191317
local __end = '__\xff'
13201318
if type(k)=='string' and k < __end and k >= '__' then -- fastest way to check if k:startswith('__') -- with majority case (lowercase key values) falling through fastest
13211319
return node[string.sub(k, 3)] -- remove leading '__' to return node:method
@@ -1338,7 +1336,7 @@ end
13381336
-- but that would not work consistently, e.g. node = 3 would set Lua local
13391337
function node:__newindex(k, v)
13401338
if k == '__' then
1341-
M.set(self, v) -- fast but only works if metatable is top-level superclass node
1339+
_yottadb.set(self, v) -- fast but only works if metatable is top-level superclass node, cf. subclass_newindex alternate
13421340
else
13431341
error(string.format("Tried to set node object %s. Did you mean to set %s.__ instead?", self[k], self[k]), 2)
13441342
end
@@ -1434,7 +1432,7 @@ node.depth = _yottadb.cachearray_depth
14341432
-- `yottadb.YDB_DATA_NOVALUE_DESC` (no value, subtree) or <br>
14351433
-- `yottadb.YDB_DATA_VALUE_DESC` (value and subtree)
14361434
-- @function node:data
1437-
node.data = M.data
1435+
node.data = _yottadb.data
14381436
--- Return true if the node has a value; otherwise false.
14391437
function node:has_value() return node.data(self)%2 == 1 end
14401438
--- Return true if the node has a tree; otherwise false.

0 commit comments

Comments
 (0)