{"slug": "speeding-up-ratchets-with-resharp", "title": "Speeding Up Ratchets with Resharp", "summary": "Danver Braganza released Ratchets v0.4.0, switching its regex engine from Rust's regex crate to Resharp, resulting in a 15% speed improvement on the Sculptor codebase. The tool enforces code style by ratcheting down forbidden patterns, now also supporting lookaround assertions.", "body_md": "## Ratchets Run Faster with Resharp\n\n*Switching from the regex crate in Rust to Resharp makes Ratchets run 15% faster on the Sculptor codebase*\n\nby [Danver Braganza](/) on *2026-06-22*\n\nYesterday, I released [Ratchets v0.4.0](https://crates.io/crates/ratchets/0.4.0), a breaking change which switched the\nregular expression engine from the widely-used Rust `regex`\n\ncrate to [ Resharp](https://docs.rs/resharp/latest/resharp/).\nAlthough this was originally driven by my desire to support lookaround assertions, I noticed a healthy speed-up when\nusing the new version of Ratchets on the\n\n[Sculptor codebase](https://github.com/imbue-ai/sculptor). With\n\n*no other changes*, Ratchets is now approximately 15% faster on our codebase.\n\n## What is a ratchet?\n\nI was first introduced to the concept of ratchets by [this qntm blog article](https://qntm.org/ratchet), which describes\na system to prevent developers from adding more instances of a particular pattern to a codebase. A ratchet system\ntolerates existing instances of a forbidden pattern by maintaining a count, and it forbids increases while enforcing\nthat decreases are recorded and committed. The count is therefore encouraged to decrease over time, tightening the\nstrictness on the codebase–hence the name. The ratchet system saves you the effort of migrating the entire codebase\nwhenever you introduce a new rule.\n\nAt [Imbue](imbue.com), we’ve embraced ratchets since before the dawn of agentic coding, and we started out deploying\nthem in the fashion above. That is, we used ratchets to encourage our human developers to adhere to our evolving style\nguide and to ensure that our code evolved in a good direction. But as agentic engineering has taken off, we’ve found\nthat the use-case for ratchets has changed. Today, it is fairly simple to launch a swarm of agents to migrate your entire\ncodebase away from a bad pattern, and so the use-case for ratchets would apparently be removed.\n\nHowever, I’ve found that there’s still a good reason to use a ratchet system. Although I can’t speak for Fable because my time with it was extremely brief, my experience with other coding agent models testifies poorly to their ability to adhere to a style guide. Because their tendency is to generate code in line with their training set, you find yourself in tension with that tendency when you attempt to constrain them with elaborate rules. This costs input tokens, and using an agent-as-judge to validate style is even more expensive. If you still care about code style—as we do—then a ratchet enforcement system which flags problems to the agent can be very helpful.\n\nRatchets are even more useful when you really need that escape hatch to allow a forbidden pattern into your codebase.\nOne example we find is type-checker suppressions. A coding agent’s propensity might be to reach for `# pyrefly: ignore`\n\nrather than understanding the type boundary. However, sometimes that might be the right decision, and the coding agent\nitself does not have sufficient context and judgement to come to the right decision. In this case, you can separate\nthe judgement of whether to use the expedient path from the coding agent, and leave it up to a planning agent. Only the\nplanning agent has the authority to bump the ratchet, and it does that with a wider perspective than a particular coding\nagent. In this way, certain language features can be used only sparingly, if there is no other way to achieve a goal.\nThis is the approach I’ve taken in [Rust Bucket](https://github.com/imbue-ai/rust-bucket), my agentic workflow and\nscaffold for Rust programs.\n\n## How `Ratchets`\n\nuses regular expressions\n\nI could dedicate a separate article to the implementation details of [Ratchets](https://github.com/imbue-ai/ratchets),\nso to keep this short, it has two mechanisms for detecting forbidden patterns:\n\n- abstract syntax tree queries,\n- and regular expressions\n\nI decided to implement abstract syntax tree queries via [tree-sitter](https://docs.rs/tree-sitter/latest/tree_sitter/)\nsince they are much more precise than regular expressions. For example, regular expressions can be sensitive to\nformatting and line-wrapping, which were specific issues that led to miscounting by our previous implementation. The\ntree-sitter queries are syntax-aware, and are robust to these kinds of problems.\n\nHowever, there were still a few rules that were best expressed as regular expressions–most commonly rules about comment\nstyle. I’d initially used the `regex`\n\ncrate, but I was surprised to find that it did not\nsupport proper lookaround. It was this need, rather than a desire for performance optimization, that led me to look into\nreplacing it with [Resharp](https://docs.rs/resharp/latest/resharp/). I’d remembered [reading their article on Hacker\nNews](https://news.ycombinator.com/item?id=47206647) and had been pleased to see [they’d released a Rust\nport](https://iev.ee/blog/symbolic-derivatives-and-the-rust-rewrite-of-resharp/).\n\nAn agent was able to perform the replacement with ease, and the copious tests we had on the codebase gave me confidence.\nOnce the replacement was completed, I wanted to check whether there was going to be a performance difference. I was very\npleased to find that replacing `regex`\n\nwith `Resharp`\n\n, and no other changes, made Ratchets run significantly faster on\nthe Sculptor codebase.\n\n## Not a conclusion\n\nI don’t have a polished final conclusion to leave with you, because this space is evolving so quickly and I’m still working through many of these thoughts in my head. But in lieu of that, here are a few observations:\n\n- If you have a Rust program that makes heavy use of regular expressions, you should evaluate Resharp.\n- While agentic engineering is improving by leaps and bounds, the scaffolding and tools you give your agents dramatically improve your outcomes for the same budget of human attention and tokens.\n- There’s still value in being a human engineer who is plugged in to developments in the ecosystem.\n- There’s still a role for human judgement in the development process.\n[Ratchets is open source](https://github.com/imbue-ai/ratchets)and permissively licensed. Please give it a look and see if it’s for you!", "url": "https://wpnews.pro/news/speeding-up-ratchets-with-resharp", "canonical_source": "https://danverbraganza.com/writings/ratchets-run-faster-with-resharp", "published_at": "2026-06-26 23:13:00+00:00", "updated_at": "2026-06-26 23:35:46.364015+00:00", "lang": "en", "topics": ["developer-tools"], "entities": ["Ratchets", "Resharp", "Sculptor", "Imbue", "Rust Bucket", "qntm", "Fable", "Pyrefly"], "alternates": {"html": "https://wpnews.pro/news/speeding-up-ratchets-with-resharp", "markdown": "https://wpnews.pro/news/speeding-up-ratchets-with-resharp.md", "text": "https://wpnews.pro/news/speeding-up-ratchets-with-resharp.txt", "jsonld": "https://wpnews.pro/news/speeding-up-ratchets-with-resharp.jsonld"}}