Skip to content

Commit 793f722

Browse files
committed
Add performance section to the docs
1 parent 6820e64 commit 793f722

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

docs/src/index.md

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,39 @@ to their generic docstrings. Here are some of those supported functions:
9090
- `eachmatch`
9191
- `match`
9292
- `occursin`
93-
<!-- - `startswith` [TODO] -->
94-
<!-- - `endswith` [TODO] -->
95-
<!-- - `findnext` [TODO] -->
96-
<!-- - `findprev` [TODO] -->
9793

94+
## Performance
95+
96+
The code search performance bottleneck is parsing. The search itself is about 20x faster
97+
than parsing and similar in performance to an optimized regex library. Consequently, if you
98+
want high performance repeated code search, you should cache parsed SyntaxNodes and pass
99+
them directly to search functions.
100+
101+
#### Benchmarks
102+
103+
Using the [395 lines of source code of this package as of 6820e64232](https://github.com/LilithHafner/CodeSearch.jl/blob/6820e642320f803407bcbc07e691277dc4d91ae4/src/CodeSearch.jl)
104+
as a test case, on a 2022 M2 mac running [Asahi Linux](https://asahilinux.org/), we can see the following performance:
105+
106+
| Operation | Time | Time per line | Benchmark |
107+
|------------------------|--------------|-------------|-------------|
108+
| Searching a string | `541.0 μs` | `1.37 μs` | `@b collect(eachmatch(j"* !== nothing", node)) seconds=1` |
109+
| Parsing a string | `516.8 μs` | `1.31 μs` | `@b parseall(SyntaxNode, str, ignore_errors=true) seconds=1` |
110+
| Searching a SyntaxNode | `20.9 μs` | `53.0 ns` | `@b collect(eachmatch(j"* !== nothing", node)) seconds=1` |
111+
| Regex search | `22.7 μs` | `57.5 ns` | `@b collect(eachmatch(r".* !== nothing", str)) seconds=1` |
112+
113+
#### Setup for benchmarks
114+
```julia
115+
shell> git clone https://github.com/LilithHafner/CodeSearch.jl CodeSearch
116+
[...]
117+
118+
shell> cd CodeSearch
119+
120+
shell> git checkout 6820e642320f803407bcbc07e691277dc4d91ae4
121+
[...]
122+
123+
julia> using CodeSearch, JuliaSyntax, Chairmarks
124+
125+
julia> str = read("src/CodeSearch.jl", String);
126+
127+
julia> node = parseall(SyntaxNode, str, ignore_errors=true);
128+
```

0 commit comments

Comments
 (0)