(Re)-Introducing mezura: The Rust line-counter that's here to eat the big dogs
This project (github.com/subamanis/mezura) started back in 2021, as my entry point to Rust, and has become the fastest and most accurate line counter (with arguably the most features too) in the world. I have been slowly improving it here and there over the years, but only recently decided to get serious.
Here is what it does that cloc, tokei and scc do not, and the numbers behind the two claims I make about it.
The whole Linux kernel, counted ^ (some languages skipped for ss purposes)
What is different
The short list: it identifies a file of a contested extension (.m, .h,...) by its content using heuristics and sets aside files that only look like code (a make .d, a ProGuard .pro, bundles, generated files). --explain shows a file line by line with the verdict for each line; --diff compares against a git revision, two revisions, or an earlier run; targets can be named and the report grouped by them; nested languages and sections like <script> and <style> count as the languages they hold; keywords of your choosing are counted only where they are code; everything printed can be themed,... among other things.
Each of these has a section in the README, with screenshots.
The project has a lib target published separately, so you can use mezura's engine directly in your projects (mezura-core). There is also an MCP server (mezura-mcp).
Speed
The whole Linux kernel tree, 63,864 files and 36 million lines, on a Ryzen 7 9700X and a PCIe-4.0 Nvme, with settings picked so that every counter does equal work, so the comparison is completely fair.
Debian 13:
mezura 3.1.1 166 ms
scc 4.1.0 210 ms
tokei 15.0.0 414 ms
Windows 11:
mezura 3.1.1 255 ms
scc 4.1.0 539 ms
tokei 15.0.0 632 ms
Very detailed benchmarking data can be found under example-run in the LineBench project I created, to standardize performance testing for line counters, and provide useful insights and advanced metrics. The example run contains the timings both with each counter's defaults and also with the equal-work flags, the methodology, the exact command lines, the checks every run carries (idle machine, clean corpus, clean tree, the commit measured), and the script that produced all of it, so anyone can rerun the numbers on their own machine.
Accuracy
I do not want to grade my own homework, so the correctness argument lives in a separate project, LineJudge. I think that the linejudge project, that I started to ensure fair correctness assessment, is the most interesting part of this whole ordeal and deserves its own post. Check it out.
How
Most counters carry one piece of state through a file, "am I inside a comment right now", and each line takes the verdict of the region it sits in. mezura reads every line on its own and sorts it into one of nine classes: words in code, words only in a comment, comment words beside code, string content, punctuation in code or in a comment, blank inside a comment, blank inside a string, blank. The walk that does this knows where every string and comment begins and ends, nested comments and Lua's --[==[ levels and raw strings and escapes and line continuations included, which is where the language quirks live and where the other counters miscount. The two different counting models mezura supports are folds over those nine classes: the content model asks what each line says, and the region model adds the classes up the way the other counters do, so it reproduces their numbers from the same walk.
The speed comes from doing little per line. Each line is scanned once for all the symbols its language declares, with memchr, which searches up to three bytes in one SIMD pass, so the symbols are grouped by the byte that finds them and most languages need a single pass per line; only at a candidate position is the rest of a symbol compared. Bundled and generated files are recognised from their head and skipped before any parsing. The directory walk and the counting are separate thread pools over a work-stealing queue.. All three crates are #![forbid(unsafe_code)].
Install
cargo install mezura, or a binary for Windows, Linux or macOS from the releases page. Over eighty languages ship with it, each one a text file you can edit or copy to add your own.
Happy to answer anything about the challenges, the parser, the two models, or the conformance suite.