Skip to content

Use a Bloom filter for formatter comment lookups - #29852

Merged
MichaReiser merged 4 commits into
mainfrom
micha/formatter-comment-bloom-filter
Oct 30, 2026
Merged

MichaReiser merged 4 commits into
mainfrom
micha/formatter-comment-bloom-filter

Conversation

@MichaReiser

Copy link
Copy Markdown
Member

Summary

Most nodes have no attached comments. A 256-byte Bloom filter lets the formatter skip the comment map lookup when a node's address maps to a clear bit. On a hit, the formatter still checks the map.

Building and printing pre-parsed documents on arm64 took 4.82% fewer instructions across five Ruff benchmark fixtures and 3.40% fewer across ten Python standard-library modules, measured as the median of 21 paired runs against main. A synthetic workload with leading and trailing comments on every assignment took 0.38% more instructions.

Test Plan

The formatter tests passed, including a forced filter collision, and all benchmark inputs produced byte-for-byte identical output.

@MichaReiser MichaReiser added performance Potential performance improvement formatter Related to the Ruff formatter labels Oct 30, 2026
@astral-automations-bot

astral-automations-bot Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

ruff-ecosystem results

Formatter (stable)

✅ ecosystem check detected no format changes.

Formatter (preview)

✅ ecosystem check detected no format changes.

Formatter (stable) · Formatter (preview)


Results for 5140094 · Workflow run

@MichaReiser
MichaReiser merged commit 00212f8 into main Oct 30, 2026
56 checks passed
@MichaReiser
MichaReiser deleted the micha/formatter-comment-bloom-filter branch October 30, 2026 21:10
MichaReiser added a commit that referenced this pull request Oct 30, 2026
)

## Summary

When source maps are disabled and the Bloom filter rules out comments on
a node, the formatter can call `fmt_fields` directly. This skips cloning
the comment handle and writing empty leading and trailing comments.
Nodes with possible comments or source maps still take the existing
path.

Compared with #29852, the fast path used 4.44% fewer instructions across
Ruff’s five formatter benchmarks and 2.79% fewer across ten Python
standard-library modules. Synthetic assignments with leading and
trailing comments used 0.13% more. These are medians from 21 paired
arm64 trials that include trivia construction and printing.

## Test Plan

The formatter tests passed, and the parent and this change produced
identical formatted text and source-map markers across the comparison
corpora.

This branch was previously deployed

1 inactive deployment
automations — 51400941 Deployed Oct 30, 2026 by MichaReiser via security-review / security review #96355
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

formatter Related to the Ruff formatter performance Potential performance improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants