Skip to content

I asked Claude to research and optimize bon compile times, and it f*cking did it. ~30% improvement - #405

Merged
Veetaha merged 5 commits into
masterfrom
perf/compile-time
Oct 2, 2026
Merged

Veetaha merged 5 commits into
masterfrom
perf/compile-time

Conversation

@Veetaha

@Veetaha Veetaha commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator

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 bon came 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:

let other_items: syn::File = syn::parse2(other_items).map_err(|err| {

The macro parsed its whole output into a syn::File only 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 and TokenTrees, 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 resulting TokenStream into a syn::File would 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 bon is now faster than typed-builder according to the compilation benchmarks in this repo (which I also extended a bit with Claude 🐱 in #406):

Suite bon before bon after typed-builder derive_builder
100 structs, 10 fields 2.32 s 1.56 s (-33%) 1.66 s 1.03 s
10 structs, 50 fields 2.10 s 1.49 s (-29%) 1.99 s 0.42 s

Here 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:

Crate Before After
tellers-mtproto 30.2 s 22.7 s (-25%)
frankenstein 20.0 s 17.4 s (-13%)
aws_lambda_events 13.5 s 12.0 s (-11%)
openai-client-base 60.6 s 55.3 s (-9%)
hf-hub 3.7 s 3.3 s (-9%)

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

The unconstrained associated type allows external implementations of the supposedly sealed State trait.

Review effort: Balanced
Findings: 1 High severity

Open (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.

Comment thread bon-macros/src/builder/builder_gen/state_mod.rs Outdated
@Veetaha
Veetaha force-pushed the perf/compile-time branch from daf1492 to 81fca87 Compare October 2, 2026 17:16
@Veetaha
Veetaha changed the base branch from master to chore/toolchain-benchmarks October 2, 2026 17:17
Base automatically changed from chore/toolchain-benchmarks to master October 2, 2026 20:17
@Veetaha
Veetaha marked this pull request as ready for review October 2, 2026 20:17
@Veetaha
Veetaha force-pushed the perf/compile-time branch 2 times, most recently from 4f4984d to 71213ca Compare October 2, 2026 20:22
@Veetaha
Veetaha requested a balanced review from Copilot October 2, 2026 20:22

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

The associated-type projection allows external implementations to bypass the new State seal.

Review effort: Balanced
Findings: 1 High severity

Open (1)
Resolved since last review (1)

Comment thread bon-macros/src/builder/builder_gen/state_mod.rs Outdated
@Veetaha Veetaha changed the title perf: speed up compilation of the generated code I asked Claude to optimize bon compile times, and it f*cking did it. 35% improvement Oct 2, 2026
@Veetaha Veetaha changed the title I asked Claude to optimize bon compile times, and it f*cking did it. 35% improvement I asked Claude to research and optimize bon compile times, and it f*cking did it. 35% improvement Oct 2, 2026
@Veetaha
Veetaha force-pushed the perf/compile-time branch from 71213ca to b124021 Compare October 2, 2026 20:44
@Veetaha
Veetaha requested a balanced review from Copilot October 2, 2026 20:44

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟢 Approval recommended

The optimizations preserve generated behavior and include regression coverage for the revised sealing mechanism.

Review effort: Balanced
Findings: None

Resolved since last review (1)

@Veetaha
Veetaha force-pushed the perf/compile-time branch from b124021 to 74d841f Compare October 2, 2026 20:57
@Veetaha Veetaha changed the title I asked Claude to research and optimize bon compile times, and it f*cking did it. 35% improvement I asked Claude to research and optimize bon compile times, and it f*cking did it. ~30% improvement Oct 2, 2026
@Veetaha
Veetaha merged commit e26222c into master Oct 2, 2026
33 checks passed
@Veetaha
Veetaha deleted the perf/compile-time branch October 2, 2026 21:27
@github-actions github-actions Bot mentioned this pull request Oct 2, 2026
@Veetaha

Veetaha commented Oct 2, 2026 •

Copy link
Copy Markdown
Collaborator Author

@ayrat555 check the PR description out, lol

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants