perf: filetype sorter calls vim.filetype.match on every comparison

Open Beginner friendly
#3,382 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
77/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
lua, neovim

Research direction

Start by reading the filetype sorter and the sort.sorter function path; the issue does not name their files, so locate them in the sorting code. Check how often vim.filetype.match runs during a sort and how the cache can be rebuilt on each sort. Done when each name is resolved once per sort, ordering stays identical, and the reported directory sort is faster.

Written by the indexing model from the issue text.

Description

Problem

With sort.sorter = "filetype", Sorter:filetype calls vim.filetype.match twice per comparison. A merge sort makes O(n log n) comparisons, and vim.filetype.match is expensive.

The sort.sorter function path also calls vim.filetype.match once per node.

Impact

Sorting a directory with 3.3k entries took 4459ms on master. This runs on every reload of that directory.

Proposed fix

Resolve each name's filetype once per sort, using a cache that is rebuilt on every sort so that vim.filetype.add changes still apply. In testing this brings the same sort down to 532ms, with identical ordering.

Environment: macOS, Nvim v0.13.0-dev, LuaJIT 2.1. Test repo: 16.8k files and folders, 3.1k git status entries (modified, untracked, ignored). Measured headless with vim.uv.hrtime() and the LuaJIT profiler (jit.p).

Dominant language
Lua
Stars
8.7k
Forks
642
Avg merge
1d 4m
Merged PRs (30d)
11

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from nvim-tree/nvim-tree.lua

All issues in nvim-tree/nvim-tree.lua

Similar issues

More Lua issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.