Skip to content

Commit fb8296c

Browse files
authored
Perf: Trie implementation is space efficient (#131)
* ref(utils): renames byte_is_valid_colorchar to byte_is_valid_color_char * test: adds test cases for nonalphanumeric custom_names including spaces * fix(util): do not add spaces as an additional color character for checking valid color chars * chore: adds error message if trie returns length but not rgb_hex * doc: updates readme and ldoc * ref(trie): dynamically build char_lookup based on inserted words, removes need for adding additional characters manually * ref(tailwind_names): tailwind names uses same trie as names * fix: removes reference to tailwind_names module * feat: Trie dynamically allocates memory for each node and resizes capacity when needed. Starts with an initial capacity of 8 * chore: removes old ldoc html file * doc: let ldoc decide which markdown parser to use * test: creates start_minimal-trie.sh script to run trie tests * test: creates makefile for executing testing targets * chore: creates trie benchmarks for different initial capacity settings
1 parent 8c7f243 commit fb8296c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+945
-867
lines changed

Makefile

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Define variables for script paths
2+
SCRIPTS_DIR=scripts
3+
TRIE_TEST_SCRIPT=$(SCRIPTS_DIR)/trie-test.sh
4+
TRIE_BENCHMARK_SCRIPT=$(SCRIPTS_DIR)/trie-benchmark.sh
5+
MINIMAL_SCRIPT=$(SCRIPTS_DIR)/minimal-colorizer.sh
6+
7+
help:
8+
@echo "Available targets:"
9+
@echo " make trie - Run trie test and benchmark"
10+
@echo " make trie-test - Run trie test"
11+
@echo " make trie-benchmark - Run trie benchmark"
12+
@echo " make minimal - Run the minimal script"
13+
@echo " make clean - Remove test/colorizer_*"
14+
15+
trie: trie-test trie-benchmark
16+
17+
trie-test:
18+
@echo "Running trie test..."
19+
@bash $(TRIE_TEST_SCRIPT)
20+
21+
trie-benchmark:
22+
@echo "Running trie benchmark..."
23+
@bash $(TRIE_BENCHMARK_SCRIPT)
24+
25+
minimal:
26+
@echo "Running minimal config..."
27+
@bash $(MINIMAL_SCRIPT)
28+
29+
clean:
30+
@echo "Removing test/colorizer_repro"
31+
@rm -rf test/colorizer_repro
32+
@echo "Removing test/trie/colorizer_trie"
33+
@rm -rf test/trie/colorizer_trie
34+
35+
.PHONY: help trie trie-test trie-benchmark minimal clean

README.md

+112-23
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
- [Lazyload Colorizer with Lazy.nvim](#lazyload-colorizer-with-lazynvim)
1717
- [Tailwind](#tailwind)
1818
- [Testing](#testing)
19+
- [Minimal Colorizer](#minimal-colorizer)
20+
- [Trie](#trie)
21+
- [Test](#test)
22+
- [Benchmark](#benchmark)
23+
- [Results](#results)
1924
- [Extras](#extras)
2025
- [TODO](#todo)
2126
<!--toc:end-->
@@ -114,10 +119,8 @@ library to do custom highlighting themselves.
114119
115120
```lua
116121
require("colorizer").setup({
117-
-- Filetype options. Accepts table like `user_default_options`
118-
filetypes = { "*" },
119-
-- Buftype options. Accepts table like `user_default_options`
120-
buftypes = {},
122+
filetypes = { "*" }, -- Filetype options. Accepts table like `user_default_options`
123+
buftypes = {}, -- Buftype options. Accepts table like `user_default_options`
121124
-- Boolean | List of usercommands to enable. See User commands section.
122125
user_commands = true, -- Enable all or some usercommands
123126
lazy_load = false, -- Lazily schedule buffer highlighting setup function
@@ -144,19 +147,18 @@ library to do custom highlighting themselves.
144147
css = false, -- Enable all CSS *features*:
145148
-- names, RGB, RGBA, RRGGBB, RRGGBBAA, AARRGGBB, rgb_fn, hsl_fn
146149
css_fn = false, -- Enable all CSS *functions*: rgb_fn, hsl_fn
147-
-- Highlighting mode. 'background'|'foreground'|'virtualtext'
148-
mode = "background", -- Set the display mode
149-
-- Tailwind colors. boolean|'normal'|'lsp'|'both'. True is same as normal
150+
-- Tailwind colors. boolean|'normal'|'lsp'|'both'. True sets to 'normal'
150151
tailwind = false, -- Enable tailwind colors
151152
tailwind_opts = { -- Options for highlighting tailwind names
152-
update_names = false, -- When using tailwind = 'both', update tailwind
153-
-- names from LSP results. See tailwind section
153+
update_names = false, -- When using tailwind = 'both', update tailwind names from LSP results. See tailwind section
154154
},
155155
-- parsers can contain values used in `user_default_options`
156156
sass = { enable = false, parsers = { "css" } }, -- Enable sass colors
157+
-- Highlighting mode. 'background'|'foreground'|'virtualtext'
158+
mode = "background", -- Set the display mode
157159
-- Virtualtext character to use
158160
virtualtext = "",
159-
-- Display virtualtext inline with color
161+
-- Display virtualtext inline with color. boolean|'before'|'after'. True sets to 'after'
160162
virtualtext_inline = false,
161163
-- Virtualtext highlight mode: 'background'|'foreground'
162164
virtualtext_mode = "foreground",
@@ -167,14 +169,6 @@ library to do custom highlighting themselves.
167169
})
168170
```
169171

170-
Highlighting modes:
171-
172-
- `background`: sets the background text color.
173-
- `foreground`: sets the foreground text color.
174-
- `virtualtext`: indicate the color behind the virtualtext.
175-
176-
Virtualtext symbol can be displayed at end of line, or
177-
178172
Setup examples:
179173

180174
```lua
@@ -379,20 +373,116 @@ are cached and returned on `WinScrolled` event.
379373

380374
## Testing
381375

382-
For troubleshooting use `test/minimal.lua`.
383-
Startup neovim with `nvim --clean -u minimal.lua` in the `test` directory.
376+
### Minimal Colorizer
384377

385-
Alternatively, use the following script from root directory:
378+
For troubleshooting use `test/minimal-colorizer.lua`.
379+
Startup neovim with `nvim --clean -u minimal-colorizer.lua` in the `test` directory.
380+
381+
Alternatively,
386382

387383
```bash
388-
scripts/start_minimal.sh
384+
make minimal
385+
```
386+
387+
or
388+
389+
```bash
390+
scripts/minimal-colorizer.sh
389391
```
390392

391393
To test colorization with your config, edit `test/expect.lua` to see expected
392394
highlights.
393395
The returned table of `user_default_options` from `text/expect.lua` will be used
394396
to conveniently reattach Colorizer to `test/expect.lua` on save.
395397

398+
### Trie
399+
400+
Colorizer uses a space efficient LuaJIT Trie implementation, which starts with
401+
an initial node capacity of 8 bytes and expands capacity per node when needed.
402+
403+
The trie can be tested and benchmarked using `test/trie/test.lua` and
404+
`test/trie/benchmark.lua` respectively.
405+
406+
#### Test
407+
408+
```bash
409+
# runs both trie-test and trie-benchmark targets
410+
make trie
411+
```
412+
413+
```bash
414+
# runs trie test which inserts words and checks longest prefix
415+
make trie-test
416+
```
417+
418+
#### Benchmark
419+
420+
```bash
421+
scripts/trie-test.sh
422+
```
423+
424+
```bash
425+
# runs benchmark for different node initial capacity allocation
426+
make trie-benchmark
427+
```
428+
429+
```bash
430+
scripts/trie-benchmark.sh
431+
```
432+
433+
##### Results
434+
435+
Inserting 7245 words: using uppercase, lowercase, camelcase from `vim.api.nvim_get_color_map()` and Tailwind colors
436+
437+
| Initial Capacity | Resize Count | Insert Time (ms) | Lookup Time (ms) |
438+
| ---------------- | ------------ | ---------------- | ---------------- |
439+
| 1 | 3652 | 25 | 16 |
440+
| 2 | 2056 | 11 | 8 |
441+
| 4 | 1174 | 6 | 5 |
442+
| 8 | 576 | 7 | 5 |
443+
| 16 | 23 | 7 | 5 |
444+
| 32 | 1 | 8 | 6 |
445+
| 64 | 0 | 10 | 7 |
446+
447+
Inserting 1000 randomized words
448+
449+
| Initial Capacity | Resize Count | Insert Time (ms) | Lookup Time (ms) |
450+
| ---------------- | ------------ | ---------------- | ---------------- |
451+
| 1 | 434 | 1 | 0 |
452+
| 2 | 234 | 1 | 1 |
453+
| 4 | 129 | 1 | 0 |
454+
| 8 | 51 | 1 | 0 |
455+
| 16 | 17 | 1 | 1 |
456+
| 32 | 3 | 1 | 2 |
457+
| 64 | 1 | 2 | 1 |
458+
| 128 | 0 | 4 | 1 |
459+
460+
Inserting 10,000 randomized words
461+
462+
| Initial Capacity | Resize Count | Insert Time (ms) | Lookup Time (ms) |
463+
| ---------------- | ------------ | ---------------- | ---------------- |
464+
| 1 | 4614 | 9 | 7 |
465+
| 2 | 2106 | 8 | 8 |
466+
| 4 | 842 | 9 | 7 |
467+
| 8 | 362 | 9 | 8 |
468+
| 16 | 208 | 11 | 9 |
469+
| 32 | 113 | 14 | 11 |
470+
| 64 | 24 | 21 | 14 |
471+
| 128 | 0 | 34 | 25 |
472+
473+
Inserting 100,000 randomized words
474+
475+
| Initial Capacity | Resize Count | Insert Time (ms) | Lookup Time (ms) |
476+
| ---------------- | ------------ | ---------------- | ---------------- |
477+
| 1 | 40656 | 160 | 117 |
478+
| 2 | 21367 | 116 | 111 |
479+
| 4 | 11604 | 122 | 109 |
480+
| 8 | 5549 | 133 | 113 |
481+
| 16 | 1954 | 141 | 138 |
482+
| 32 | 499 | 173 | 158 |
483+
| 64 | 100 | 233 | 173 |
484+
| 128 | 0 | 343 | 198 |
485+
396486
## Extras
397487

398488
Documentation is generated using ldoc. See
@@ -402,6 +492,5 @@ Documentation is generated using ldoc. See
402492

403493
- [ ] Add more color types ( var, advanced css functions )
404494
- [ ] Add more display modes. E.g - sign column
405-
- [ ] Use a more space efficient trie implementation.
406495
- [ ] Support custom parsers
407496
- [ ] Options support providing function to enable/disable instead of just boolean

0 commit comments

Comments
 (0)