Code Monkey home page Code Monkey logo

Comments (2)

aileot avatar aileot commented on May 24, 2024

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 in
    Benchmark 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 in
    Benchmark 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

from nvim-laurel.

aileot avatar aileot commented on May 24, 2024

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 (minmax):   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 (minmax):   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 (minmax):   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 (minmax):   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 (minmax):    1.028 s1.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 (minmax):    1.025 s1.037 s    10 runs

from nvim-laurel.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.