Skip to content

Commit

Permalink
perf: add benchmarkk
Browse files Browse the repository at this point in the history
  • Loading branch information
glepnir committed Aug 26, 2024
1 parent b02fbaf commit 4a246ea
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 6 deletions.
49 changes: 49 additions & 0 deletions bench/get_line.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
local ffi = require('ffi')
local api = vim.api

-- FFI definition for ml_get
ffi.cdef([[
typedef int32_t linenr_T;
char *ml_get(linenr_T lnum);
]])

local C = ffi.C

-- Function to get line using ml_get
local function get_line_with_ml_get(lnum)
return ffi.string(C.ml_get(lnum))
end

-- Function to get line using api.nvim_buf_get_lines
local function get_line_with_api(bufnr, lnum)
return api.nvim_buf_get_lines(bufnr, lnum - 1, lnum, false)[1]
end

-- Benchmark function
local function benchmark(func, bufnr, lnum, iterations)
local start_time = vim.uv.hrtime()
for _ = 1, iterations do
func(bufnr, lnum)
end
local end_time = vim.uv.hrtime()
return (end_time - start_time) / iterations
end

-- Example usage
local bufnr = api.nvim_get_current_buf()
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, { ('text '):rep(100) })
local lnum = 1
local iterations = 10000

-- Warm up
get_line_with_ml_get(lnum)
get_line_with_api(bufnr, lnum)

-- Run benchmarks
local time_ml_get = benchmark(function(_, lnum)
return get_line_with_ml_get(lnum)
end, bufnr, lnum, iterations)
local time_api = benchmark(get_line_with_api, bufnr, lnum, iterations)

print(string.format('ml_get: %.3f ns per call', time_ml_get))
print(string.format('api.nvim_buf_get_lines: %.3f ns per call', time_api))
8 changes: 4 additions & 4 deletions bench/space_or_tab.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
local function only_spaces_or_tabs(text)
for i = 1, #text do
local c = text:sub(i, i)
if c ~= ' ' and c ~= '\t' then
local byte = string.byte(text, i)
if byte ~= 32 and byte ~= 9 then -- 32 is space, 9 is tab
return false
end
end
Expand Down Expand Up @@ -30,5 +30,5 @@ local time2 = benchmark(only_spaces_or_tabs_regex, text, iterations)
print('Time for only_spaces_or_tabs:', time1)
print('Time for only_spaces_or_tabs_regex:', time2)

-- Time for only_spaces_or_tabs: 0.07258
-- Time for only_spaces_or_tabs_regex: 0.093389
-- Time for only_spaces_or_tabs: 0.027799
-- Time for only_spaces_or_tabs_regex: 0.093943
4 changes: 2 additions & 2 deletions lua/indentmini/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ local context = { snapshot = {} }
--- @return boolean true only have space or tab
local function only_spaces_or_tabs(text)
for i = 1, #text do
local c = text:sub(i, i)
if c ~= ' ' and c ~= '\t' then
local byte = string.byte(text, i)
if byte ~= 32 and byte ~= 9 then -- 32 is space, 9 is tab
return false
end
end
Expand Down

0 comments on commit 4a246ea

Please sign in to comment.