RuboCop 1.93 is out! Almost three years ago I wrote about using RuboCop with Prism for the first time, and I was VERY excited about it. Since then Prism quietly became the default parser for modern Ruby code, but it always reached RuboCop through a translation layer that made it look like the good old parser gem. With this release (and RuboCop AST 1.54) the middleman is gone - RuboCop can now build its AST straight from Prism.

That sounds like the kind of change that should come with fireworks and a 10x speedup. It doesn’t, and I think the reasons why are more interesting than the change itself.

Why not just drop Parser?

That’s the first question everyone asks, usually followed by “and why don’t the cops work on Prism’s own nodes?”. I wish it were that simple. Here’s what stands in the way:

  • Prism can only parse Ruby 3.3 and newer. RuboCop can analyze code that targets anything back to Ruby 2.0, and it doesn’t care much which Ruby it runs on. Plenty of popular gems still support Rubies that were released long before Prism existed, and they lint their code as that oldest version. When RuboCop can’t figure out your target version it assumes 2.7. All of those need parser.
  • Every cop is written against the AST of parser - the node types, their children, their source maps. That’s about 600 cops in RuboCop itself, hundreds more in the extensions, and who knows how many custom cops living in private codebases. Prism’s own tree has a different shape, so moving the cops to it would break every single one of them. I did that kind of migration once, from ripper to parser, when RuboCop was tiny. Never again.
  • Autocorrection doesn’t live in the parser at all. It lives in the source layer of parser - ranges, buffers, comments and the tree rewriter that merges the edits of different cops. Prism won’t ship a rewriter, so that code has to live somewhere.
  • The new engine needs Prism 2, and some very popular gems (Ruby LSP being the one you most likely have in your bundle) still require Prism 1. Until they catch up, Bundler will happily keep you on the old path.

So Parser isn’t going anywhere just yet. But we don’t have to keep routing every single parse through it either.

What we did instead

RuboCop AST now has its own compiler that walks the Prism tree and creates the very same nodes RuboCop has always worked with - same types, same node classes, same source maps. It also converts Prism’s tokens, comments and diagnostics by itself. From the point of view of a cop nothing has changed, and that’s the whole point.

If that sounds a lot like what the translation layer (Prism::Translation::Parser, the artist formerly known as parser-prism) has been doing for years - it is. Both walk the Prism tree and produce a parser-shaped AST, and we went out of our way to produce exactly the same one. The difference is in what happens along the way, and in who owns it:

  • The translation layer is a Parser::Base subclass that drives the builder of the parser gem - the very same builder the old Racc grammar talks to. For every node it repackages Prism’s locations as parser-style tokens, hands them to the builder and lets it work out the node and its source map. It’s a brilliant way to make Prism a drop-in replacement, but it means every parse still goes through parser’s machinery, and you can only build what that builder knows how to build.
  • Our compiler creates RuboCop’s nodes and their source maps directly from Prism’s nodes and locations. No builder, no fake tokens, no Parser::Base. The only thing left from parser is the handful of classes that describe source code (ranges, buffers, source maps), and those we can carry ourselves.
  • The translation layer has to serve everyone who ever used parser, bug for bug. Ours only has to serve RuboCop, and it lives in a gem we control. When Ruby grows new syntax we can add a node for it the same day, and we no longer have to poke at the internals of someone else’s library to get things like lazily built tokens.

So think of it less as a new idea and more as taking a great idea in-house, minus a layer.

We also ship a copy of the source layer of parser (about 2,800 lines) that’s loaded only when the parser gem isn’t installed. With it, linting Ruby 3.3+ code needs nothing from parser at all.1

The new engine is called prism, and it’s what ParserEngine: default picks when your target Ruby is 3.3 or newer and Prism 2 is in your bundle. With an older Prism you stay on parser_prism (the translation layer), and for older target Rubies on parser_whitequark, exactly as before. rubocop -V tells you which one you got.

“The same nodes” is easy to say, so we checked it. The new engine and the translation layer produce identical trees, tokens, comments and diagnostics on every Ruby file of every gem I have installed - 17,580 files, 12.4 million nodes and 24.7 million tokens. And RuboCop reports exactly the same offenses on its own codebase with parser made impossible to load.

What do we get out of it?

  • New Ruby syntax no longer has to wait for someone to invent a parser-shaped node for it upstream. That’s the part I care about most.
  • One less layer of other people’s private API between us and the parser.
  • A path to a RuboCop that doesn’t depend on parser for modern code.
  • And yes, a bit of speed. About that…

Parsing was never the bottleneck

Here’s what parsing RuboCop’s own source code (about 1,800 files) costs on my machine:

  Time
Prism alone (Prism.parse_lex) 1.0s
The new prism engine 4.5s
parser_prism (translation layer) 5.0s
The parser gem roughly 4.5x the Prism engines

So the new engine is about 10% faster than the translation layer, and allocates 9% fewer objects. Nice, but look at the first two rows - Prism needs one second, and we spend three and a half more turning its tree into ours. Most of what’s left of the parsing cost is our own node and source map building, not parsing.

And then look at the big picture. A full single-process run over those same files with the default configuration takes about 38 seconds, and the profiler puts everything parser-related (Prism, building our nodes, the tokens) at roughly a tenth of that. The other 90% is cops doing their job, and no parser in the world will make them faster.

Startup tells the same story. Running RuboCop on a single small file takes about half a second here. Close to half of that is Bundler, the rest is loading and configuring a few hundred cops, and the parser shows up for a couple of milliseconds. That’s the problem server mode solves, by the way.

These days there are projects like rbcop, which is RuboCop rewritten in Rust and claims to be 20 times faster, so it might seem we’re doing embarrassingly little to speed RuboCop up. Guilty as charged. In our defense - we’re trying to stay true to the spirit of Ruby. By running on Ruby. :D2

Known limitations

  • You need Prism 2 to get the new engine. If something in your bundle holds Prism back, nothing breaks - you just stay where you were.
  • Target Ruby versions below 3.3 still need the parser gem, and so does asking for parser_whitequark or parser_prism explicitly.
  • parser is still a dependency of both RuboCop and RuboCop AST. Dropping it would break everyone who lints old Ruby code, so that’s a conversation for RuboCop 2.0.
  • If you somehow end up with a parser engine that can’t handle your target Ruby version, RuboCop now tells you so in plain words instead of greeting you with a backtrace.

What might come next

No promises here, just the things on my mind:

  • Building source maps lazily. That’s where most of those three and a half seconds go, and many nodes never have their location looked at.
  • Linting files with syntax errors. Prism recovers from errors and still gives you a tree, which parser never could. Your editor would love that, since code is broken most of the time while you’re typing it.
  • Our own node types for new syntax, instead of waiting for upstream.
  • Giving the source layer a proper home under RuboCop::AST (with the old names kept as aliases, of course).
  • Dropping the parser dependency in RuboCop 2.0, and retiring the translation layer once Prism 2 is everywhere.

The rest of the release

The other big theme is a familiar one - autocorrection you can trust. There are 66 bug fixes in this release, and most of them are about corrections that ate a heredoc body, dropped a comment or quietly changed what the code does. Starlexxx found (with a fuzzer!) and fixed a big chunk of them, and Koichi took care of plenty more.

A couple of smaller things:

  • A config file passed with --config now has its TargetRubyVersion validated, like any other config file.
  • Lint/DuplicateMethods no longer complains about a method that’s redefined after undef, undef_method or remove_method. Thanks, APX403!

The release notes have the full list.

Epilogue

This is one of those releases where the most important change is one you’re not supposed to notice. If you do notice something - an offense that appears or disappears when you switch between ParserEngine: prism and parser_prism - that’s a bug, and I’d love to hear about it.

Big thanks to Kevin Newton and everyone working on Prism, to Koichi for keeping RuboCop in sync with it all these years, to Marc-André for reviewing the RuboCop AST side of this, and to everyone who contributed to the release!

That’s all I have for you today. Keep hacking!

  1. Not that you can uninstall it just yet - see the limitations below. ↩

  2. I was curious how much of that gap is just startup time, so I measured. On RuboCop’s own code rbcop needs about 14 times less CPU time per file, startup not included. Impressive stuff! ↩