Comments (2)
The scripts for benchmark is meaningless because they're on Lua 5.4.3; Neovim is built with luaJIT.
rawget
is slower than regular table access; therefore, this suggestion should be rejected.
Benchmark
hyperfine --warmup 10 --max-runs 50 'lua -e "local foo; for _=1, 2e7 do foo = _G.string end; print(foo.len(\"bar\"))"'
results inBenchmark 1: lua -e "local foo; for _=1, 2e7 do foo = _G.string end; print(foo.len(\"bar\"))" Time (mean ± σ): 238.8 ms ± 20.0 ms [User: 235.2 ms, System: 2.4 ms] Range (min … max): 216.7 ms … 268.8 ms 11 runs
hyperfine --warmup 10 --max-runs 50 'lua -e "local foo; for _=1, 2e7 do foo = rawget(_G, \"string\") end; print(foo.len(\"bar\"))"'
results inBenchmark 1: lua -e "local foo; for _=1, 2e7 do foo = rawget(_G, \"string\") end; print(foo.len(\"bar\"))" Time (mean ± σ): 880.1 ms ± 22.6 ms [User: 878.1 ms, System: 0.9 ms] Range (min … max): 854.6 ms … 928.3 ms 10 runs
References
- https://gintenlabo.hatenablog.com/entry/20100715/1279204620 concluded on 2010 Jul 15 that regular table access is faster than
rawget
/rawset
; - https://www.luaforum.com/index.php?threads/is-there-a-way-to-reffer-a-table-itself-inside-the-same-table.469/ referred to the performance difference, but no one was sure there.
from nvim-laurel.
On LuaJIT in nvim, rawget
makes no performance difference compared with regular table access.
Benchmarks
Global access
-
Regular table access:
In /tmp/global-regular.lua,
for _=1,8e4 do print(string.len("foo")) end
hyperfine --warmup 10 --max-runs 100 'nvim --headless --noplugin -u /tmp/global-regular.lua +q'
Benchmark 1: nvim --headless --noplugin -u /tmp/global-regular.lua +q Time (mean ± σ): 262.7 ms ± 8.1 ms [User: 235.8 ms, System: 21.5 ms] Range (min … max): 249.2 ms … 276.5 ms 11 runs
-
With
rawget
:In /tmp/global-rawget.lua,
for _=1,8e4 do print(rawget(string, "len")("foo")) end
hyperfine --warmup 10 --max-runs 100 'nvim --headless --noplugin -u /tmp/global-rawget.lua +q'
Benchmark 1: nvim --headless --noplugin -u /tmp/global-rawget.lua +q Time (mean ± σ): 273.0 ms ± 9.7 ms [User: 248.3 ms, System: 21.7 ms] Range (min … max): 249.8 ms … 285.7 ms 11 runs
Local access
-
Regular table access:
In /tmp/local-regular.lua,
local foo = { bar = {baz="qux"} } for _=1,8e4 do print(foo.bar.baz) end
hyperfine --warmup 10 --max-runs 100 'nvim --headless --noplugin -u /tmp/local-regular.lua +q'
Benchmark 1: nvim --headless --noplugin -u /tmp/local-regular.lua +q Time (mean ± σ): 297.1 ms ± 9.0 ms [User: 257.8 ms, System: 35.1 ms] Range (min … max): 282.2 ms … 310.4 ms 10 runs
-
With
rawget
:In /tmp/local-rawget.lua,
local foo = { bar = {baz="qux"} } for _=1,8e4 do print(rawget(rawget(foo, "bar"), "baz")) end
hyperfine --warmup 10 --max-runs 100 'nvim --headless --noplugin -u /tmp/local-rawget.lua +q'
Benchmark 1: nvim --headless --noplugin -u /tmp/local-rawget.lua +q Time (mean ± σ): 299.1 ms ± 14.8 ms [User: 257.7 ms, System: 39.4 ms] Range (min … max): 283.1 ms … 327.5 ms 10 runs
vim.api
-
Regular table access:
In /tmp/vim-regular.lua,
for _=1,8e4 do vim.api.nvim_set_keymap("n", "s", "x") vim.api.nvim_set_keymap("x", "s", "x") end
hyperfine --warmup 10 --max-runs 100 'nvim --headless --noplugin -u /tmp/vim-regular.lua +q'
Benchmark 1: nvim --headless --noplugin -u /tmp/vim-regular.lua +q Time (mean ± σ): 1.034 s ± 0.004 s [User: 0.021 s, System: 0.006 s] Range (min … max): 1.028 s … 1.038 s 10 runs
-
With
rawget
:In /tmp/vim-rawget.lua,
for _=1,8e4 do rawget(rawget(vim, "api"), "nvim_set_keymap")("n", "s", "x") rawget(rawget(vim, "api"), "nvim_set_keymap")("x", "s", "x") end
hyperfine --warmup 10 --max-runs 100 'nvim --headless --noplugin -u /tmp/vim-rawget.lua +q'
Benchmark 1: nvim --headless --noplugin -u /tmp/vim-rawget.lua +q Time (mean ± σ): 1.032 s ± 0.005 s [User: 0.019 s, System: 0.006 s] Range (min … max): 1.025 s … 1.037 s 10 runs
from nvim-laurel.
Related Issues (20)
- fix!(autocmd|keymap): list other than anonymous function in rhs should result in string by default
- feat(option): add `wo!`, `bo!`, ..., macros HOT 1
- docs(macros): rewrite `?foo` to `[foo]` to distinguish nillable and omittable HOT 1
- docs(macros): note `#(vim.schedule #(foo))` in anti-pattern
- perf(desc): remove unnecessary desc inference HOT 2
- test(option): compiled with `--no-compiler-sandbox`, `bo!`/`wo!` fails with value in kv-table
- will it supports folkes new lazy? HOT 8
- how to get started HOT 1
- feat(autocmd): detect `&vim` indicator to set Vim Ex command
- feat(keymap): detect `&vim` indicator to set key sequence
- feat(autocmd): detect `^<.+>` from symbol/list to set command HOT 1
- feat(keymap): detect `^<.+>` from symbol/list to set command
- feat(autocmd): deprecate the options: `<command>`, `cb`, `<command>`, and `ex`
- feat(keymap): deprecate the options: `<command>`, `cb`, `<command>`, and `ex`
- feat(autocmd): deprecate quote format
- feat(keymap): deprecate quote format for callback & list for ex-command
- docs(vimdoc): update doc how to pass symbol/list as Vim script HOT 1
- feat(command): deprecate unnecessary support of quoted format for callback
- feat: add interface to set default options HOT 1
- refactor(utils): update on _G.vim.g.laurel_deprecated at compile time HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nvim-laurel.