Skip to content

Inline formatter printing hot paths#26504

Merged
charliermarsh merged 3 commits into
mainfrom
charlie/codex-inline-formatter-printing
Jul 2, 2026
Merged

Inline formatter printing hot paths#26504
charliermarsh merged 3 commits into
mainfrom
charlie/codex-inline-formatter-printing

Conversation

@charliermarsh

@charliermarsh charliermarsh commented Jul 1, 2026

Copy link
Copy Markdown
Member

Summary

The formatter calls Printer::print_element for every rendered format element and Printer::print_text for every text element. LLVM leaves both private methods out of line even when they are marked #[inline] and the release profile uses fat LTO, so these hot loops retain out-of-line calls.

This marks both functions as #[inline(always)], allowing the printing loop and its common text paths to optimize together. It follows the same general source-level inlining approach as #26429 and closed #26473, but targets the printing pass rather than fitting or comment extraction. The methods have a small, fixed set of call sites, and the release binary's .text grows by 11,264 bytes (0.36%).

Performance

To test whether ordinary #[inline] is sufficient and whether LTO changes the result, I built the exact base with no annotation, #[inline], and #[inline(always)] under both the profiling profile (LTO disabled) and the release profile (fat LTO). Plain #[inline] neither removed the out-of-line symbols nor changed .text, and CodSpeed reported 0.0% in both profiles. Forced inlining removed both symbols and preserved the improvement under fat LTO:

Profile Annotation Overall impact .text delta
Profiling (no LTO) #[inline] 0.0% 0 bytes
Profiling (no LTO) #[inline(always)] +6.2% +11,152 bytes (+0.35%)
Release (fat LTO) #[inline] 0.0% 0 bytes
Release (fat LTO) #[inline(always)] +6.0% +11,264 bytes (+0.36%)

The release/fat-LTO comparison on the existing non-microbenchmark formatter suite reports:

Benchmark Base Head Speedup
formatter[large/dataset.py] 7.53 ms 7.02 ms 7.3%
formatter[pydantic/types.py] 2.99 ms 2.79 ms 7.2%
formatter[numpy/ctypeslib.py] 1.57 ms 1.48 ms 5.9%
formatter[unicode/pypinyin.py] 580.30 µs 550.17 µs 5.5%
formatter[numpy/globals.py] 217.95 µs 209.04 µs 4.3%

@codspeed-hq

codspeed-hq Bot commented Jul 1, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 5.99%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 5 improved benchmarks
✅ 55 untouched benchmarks
⏩ 93 skipped benchmarks1

Performance Changes

Mode Benchmark BASE HEAD Efficiency
Simulation formatter[pydantic/types.py] 3.2 ms 2.9 ms +7.23%
Simulation formatter[large/dataset.py] 7.9 ms 7.4 ms +7.16%
Simulation formatter[numpy/ctypeslib.py] 1.7 ms 1.6 ms +5.9%
Simulation formatter[unicode/pypinyin.py] 612.9 µs 580.8 µs +5.53%
Simulation formatter[numpy/globals.py] 231.6 µs 222.3 µs +4.17%

Tip

Curious why this is faster? Use the CodSpeed MCP and ask your agent.


Comparing charlie/codex-inline-formatter-printing (1521be2) with main (c360a05)

Open in CodSpeed

Footnotes

  1. 93 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@camc314

camc314 commented Jul 1, 2026

Copy link
Copy Markdown

nice 🚀 succeeding where I failed 🙂

@charliermarsh charliermarsh changed the title [codex] Inline formatter printing hot paths Inline formatter printing hot paths Jul 1, 2026
@charliermarsh charliermarsh added performance Potential performance improvement formatter Related to the formatter labels Jul 1, 2026
@charliermarsh
charliermarsh marked this pull request as ready for review July 1, 2026 12:44

@MichaReiser MichaReiser left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is silly

}

/// Prints a single element and push the following elements to queue
// LLVM considers this function too large to inline on its own, but it is called for every

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LLVM does have a point, given that the function is called twice. But it's probably fine, we may want to reconsider if the function is ever called in three places.

@charliermarsh

Copy link
Copy Markdown
Member Author

Why is it silly

@charliermarsh
charliermarsh merged commit d1cf0f8 into main Jul 2, 2026
47 checks passed
@charliermarsh
charliermarsh deleted the charlie/codex-inline-formatter-printing branch July 2, 2026 15:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

formatter Related to the formatter performance Potential performance improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants