perf: add fast path to vim.validate (#28977)

For many small/simple functions (like those found in shared.lua), the
runtime of vim.validate can far exceed the runtime of the function
itself. Add an "overload" to vim.validate that uses a simple assertion
pattern, rather than parsing a full "validation spec".
This commit is contained in:
Gregory Anders
2024-05-27 08:08:23 -05:00
committed by GitHub
parent c4eb0b64bd
commit 48251134ee
3 changed files with 100 additions and 15 deletions

View File

@@ -1408,7 +1408,25 @@ describe('lua stdlib', function()
exec_lua("vim.validate{arg1={{}, 't' }, arg2={ 'foo', 's' }}")
exec_lua("vim.validate{arg1={2, function(a) return (a % 2) == 0 end, 'even number' }}")
exec_lua("vim.validate{arg1={5, {'n', 's'} }, arg2={ 'foo', {'n', 's'} }}")
vim.validate('arg1', 5, 'number')
vim.validate('arg1', '5', 'string')
vim.validate('arg1', { 5 }, 'table')
vim.validate('arg1', function()
return 5
end, 'function')
vim.validate('arg1', nil, 'number', true)
vim.validate('arg1', nil, 'string', true)
vim.validate('arg1', nil, 'table', true)
vim.validate('arg1', nil, 'function', true)
matches('arg1: expected number, got nil', pcall_err(vim.validate, 'arg1', nil, 'number'))
matches('arg1: expected string, got nil', pcall_err(vim.validate, 'arg1', nil, 'string'))
matches('arg1: expected table, got nil', pcall_err(vim.validate, 'arg1', nil, 'table'))
matches('arg1: expected function, got nil', pcall_err(vim.validate, 'arg1', nil, 'function'))
matches('arg1: expected string, got number', pcall_err(vim.validate, 'arg1', 5, 'string'))
matches('arg1: expected table, got number', pcall_err(vim.validate, 'arg1', 5, 'table'))
matches('arg1: expected function, got number', pcall_err(vim.validate, 'arg1', 5, 'function'))
matches('arg1: expected number, got string', pcall_err(vim.validate, 'arg1', '5', 'number'))
matches('expected table, got number', pcall_err(exec_lua, "vim.validate{ 1, 'x' }"))
matches('invalid type name: x', pcall_err(exec_lua, "vim.validate{ arg1={ 1, 'x' }}"))
matches('invalid type name: 1', pcall_err(exec_lua, 'vim.validate{ arg1={ 1, 1 }}'))