I asked Claude to research and optimize bon compile times, and it f*cking did it. ~30% improvement - #405
Merged
Conversation
There was a problem hiding this comment.
Copilot review overview
🟡 Changes recommended
The unconstrained associated type allows external implementations of the supposedly sealed State trait.
Review effort: Balanced
Findings: 1
What changed in this PR
Optimizes generated builder compilation and expands benchmarks to include builder usage.
Changes:
- Applies lint attributes directly without reparsing generated code.
- Replaces sealing constants with associated types.
- Adds builder calls to benchmarks and updates results/toolchain snapshots.
| File | Description |
|---|---|
rust-toolchain.toml |
Updates Rust to 1.99.0. |
bon/tests/integration/ui/compile_fail/diagnostic_on_unimplemented.stderr |
Updates diagnostics snapshot. |
bon/tests/integration/ui/compile_fail/attr_required.stderr |
Updates diagnostics snapshot. |
bon/tests/integration/ui/compile_fail/attr_bon.stderr |
Updates diagnostics snapshot. |
bon-macros/tests/snapshots/setters_docs_and_vis.rs |
Updates generated attribute ordering. |
bon-macros/src/util/mod.rs |
Removes the obsolete item utility. |
bon-macros/src/util/item.rs |
Deletes item attribute mutation support. |
bon-macros/src/builder/builder_gen/state_mod.rs |
Changes typestate sealing generation. |
bon-macros/src/builder/builder_gen/mod.rs |
Applies attributes directly to generated items. |
bon-macros/src/builder/builder_gen/builder_derives/mod.rs |
Returns derives as separate generated items. |
benchmarks/compilation/src/structs_100_fields_10.rs |
Adds benchmark builder calls. |
benchmarks/compilation/src/structs_10_fields_50.rs |
Adds benchmark builder calls. |
benchmarks/compilation/src/lib.rs |
Allows benchmark helper functions. |
benchmarks/compilation/results.md |
Records updated benchmark results. |
benchmarks/compilation/codegen/src/main.rs |
Generates both suites with usage functions. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Veetaha
force-pushed
the
perf/compile-time
branch
from
October 2, 2026 17:16
daf1492 to
81fca87
Compare
Veetaha
marked this pull request as ready for review
October 2, 2026 20:17
Veetaha
force-pushed
the
perf/compile-time
branch
2 times, most recently
from
October 2, 2026 20:22
4f4984d to
71213ca
Compare
Veetaha
force-pushed
the
perf/compile-time
branch
from
October 2, 2026 20:44
71213ca to
b124021
Compare
Veetaha
force-pushed
the
perf/compile-time
branch
from
October 2, 2026 20:57
b124021 to
74d841f
Compare
Merged
Collaborator
Author
|
@ayrat555 check the PR description out, lol |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

I was watching a video from Theo about his tokenmaxxing. While I disagree with him at times, I still like watching his videos to see what's new and interesting in the AI world. One phrase in that video caught my attention. I can't find it right now, but it was smth about "Give Claude your problem, not your solution". I knew that already, but for some reason, the idea of asking Claude to research and optimize
boncame to me only after he said that, so I gave it a second thought... And Claude did it. In 49 minutes. It found a dumb bottleneck....It also suggested a new typestate design with GATs, which looks scary and still doesn't provide a full win over the current design in terms of compile time perf, so I rejected that
The bottleneck was this line of code:
bon/bon-macros/src/builder/builder_gen/mod.rs
Line 80 in 61fb924
The macro parsed its whole output into a
syn::Fileonly to add#[allow]attributes to each item. That... was about half of the macro's run time.The KEY WORD is "macro run time". For some reason I never suspected there could be any bottleneck in bon's macro run time. The pure logic of generating
TokenStreams andTokenTrees, pfft, how hard can it be? Lol, not if I make it harder than it needs to be. IDK why, but I thought parsing the entire resultingTokenStreaminto asyn::Filewould cost nothing.Hell no, it's something. It's half of the macro run time spent on doing that... And for what? For adding
#[allow]attributes to all items in that output, which could be done way more efficiently with a little code change. I am surprised I made that mistake (well, the 2 years younger me did). I would never have thought about this if it wasn't for Claude Opus 5.5 doing all the grunt work of profiling the build times and finding this bottleneck.There are a couple of other less important optimisations, not worth mentioning in this description. So the final result is that
bonis now faster thantyped-builderaccording to the compilation benchmarks in this repo (which I also extended a bit with Claude 🐱 in #406):bonbeforebonaftertyped-builderderive_builderHere are the build times of some heavy real-world downstream users of
bon. The numbers are less impressive here, because a bunch of other things contribute to the build times of these crates. These are clean builds of the crate itself, with its dependencies already compiled:tellers-mtprotofrankensteinaws_lambda_eventsopenai-client-basehf-hub