Make Buffer::write_element non-failable#6613
Merged
MichaReiser merged 1 commit intomainfrom Aug 16, 2023
Merged
Conversation
Member
Author
|
Current dependencies on/for this PR: This comment was auto-generated by Graphite. |
Contributor
PR Check ResultsBenchmarkLinuxWindows |
charliermarsh
approved these changes
Aug 16, 2023
Member
charliermarsh
left a comment
There was a problem hiding this comment.
Nice! I'm surprised this is so expensive, did it surprise you or did you have intuition here?
Member
Author
I had some intuition:
I'm mainly surprised that we can't see the same results on our benchmark runner. |
Member
Ah yeah, I've seen this somewhere, maybe it's just an issue in the Rust repo... |
This was referenced Nov 24, 2025
graphite-app bot
pushed a commit
to oxc-project/oxc
that referenced
this pull request
Nov 25, 2025
This PR is a follow-up to astral-sh/ruff#6613. ### Why can we do it https://github.com/oxc-project/oxc/blob/622e000934d97ade5588f1e8cc3badde96e7328e/crates/oxc_formatter/src/formatter/diagnostics.rs#L11-L29 We can also make it non-failable since the entire program only has two points that can trigger an error, neither of which needs to interrupt the program immediately, and both can be improved to avoid making FormatResult everywhere 1. `FormatError::PoorLayout` This error may occur only when handling call arguments, and only the first or the last argument is a `Function` or `ArrowFunctionExpression`. The error may be thrown during the formatting of them. I commented out the related logic of this in this PR and fixed it in #16093 to make it easier to review 2. `FormatError::InvalidDocument` This is a printer error, but it may also occur only when we format `TaggedTemplateExpression`. There is a formatting logic that will format the template expression and print the IR immediately. If the IR we generated is incorrect, then the error will occur, but this is quite rare! So it's not worth using `FormatResult` everywhere to handle it. Currently, I just `unwrap` it. If we really need to handle it, we can store the error somewhere and return it after formatting in the future. https://github.com/oxc-project/oxc/blob/78cd5910985cece594e74884717f77ad75347b8a/crates/oxc_formatter/src/write/template.rs#L602-L603 ### Key changes The most significant change is: ```diff pub trait Format<'ast, T = ()> { - fn fmt(&self, f: &mut Formatter<'_, 'ast>) -> FormatResult<()>; + fn fmt(&self, f: &mut Formatter<'_, 'ast>); } ``` All remaining changes to the order stem from the above change: 1. Remove all `FormatResult` usages 2. Remove all `?` operators that pair with the `format!` macro 4. Commented out `PoorLayout` related logic, which is going to be fixed in the #16093 ### Benefits We don't need to deal with `Option`, and removing all `Option` handling makes the code cleaner and more readable. Of course, the performance improved a little bit
leaysgur
pushed a commit
to oxc-project/oxc
that referenced
this pull request
Nov 26, 2025
This PR is a follow-up to astral-sh/ruff#6613. ### Why can we do it https://github.com/oxc-project/oxc/blob/622e000934d97ade5588f1e8cc3badde96e7328e/crates/oxc_formatter/src/formatter/diagnostics.rs#L11-L29 We can also make it non-failable since the entire program only has two points that can trigger an error, neither of which needs to interrupt the program immediately, and both can be improved to avoid making FormatResult everywhere 1. `FormatError::PoorLayout` This error may occur only when handling call arguments, and only the first or the last argument is a `Function` or `ArrowFunctionExpression`. The error may be thrown during the formatting of them. I commented out the related logic of this in this PR and fixed it in #16093 to make it easier to review 2. `FormatError::InvalidDocument` This is a printer error, but it may also occur only when we format `TaggedTemplateExpression`. There is a formatting logic that will format the template expression and print the IR immediately. If the IR we generated is incorrect, then the error will occur, but this is quite rare! So it's not worth using `FormatResult` everywhere to handle it. Currently, I just `unwrap` it. If we really need to handle it, we can store the error somewhere and return it after formatting in the future. https://github.com/oxc-project/oxc/blob/78cd5910985cece594e74884717f77ad75347b8a/crates/oxc_formatter/src/write/template.rs#L602-L603 ### Key changes The most significant change is: ```diff pub trait Format<'ast, T = ()> { - fn fmt(&self, f: &mut Formatter<'_, 'ast>) -> FormatResult<()>; + fn fmt(&self, f: &mut Formatter<'_, 'ast>); } ``` All remaining changes to the order stem from the above change: 1. Remove all `FormatResult` usages 2. Remove all `?` operators that pair with the `format!` macro 4. Commented out `PoorLayout` related logic, which is going to be fixed in the #16093 ### Benefits We don't need to deal with `Option`, and removing all `Option` handling makes the code cleaner and more readable. Of course, the performance improved a little bit
taearls
pushed a commit
to taearls/oxc
that referenced
this pull request
Dec 11, 2025
This PR is a follow-up to astral-sh/ruff#6613. ### Why can we do it https://github.com/oxc-project/oxc/blob/622e000934d97ade5588f1e8cc3badde96e7328e/crates/oxc_formatter/src/formatter/diagnostics.rs#L11-L29 We can also make it non-failable since the entire program only has two points that can trigger an error, neither of which needs to interrupt the program immediately, and both can be improved to avoid making FormatResult everywhere 1. `FormatError::PoorLayout` This error may occur only when handling call arguments, and only the first or the last argument is a `Function` or `ArrowFunctionExpression`. The error may be thrown during the formatting of them. I commented out the related logic of this in this PR and fixed it in oxc-project#16093 to make it easier to review 2. `FormatError::InvalidDocument` This is a printer error, but it may also occur only when we format `TaggedTemplateExpression`. There is a formatting logic that will format the template expression and print the IR immediately. If the IR we generated is incorrect, then the error will occur, but this is quite rare! So it's not worth using `FormatResult` everywhere to handle it. Currently, I just `unwrap` it. If we really need to handle it, we can store the error somewhere and return it after formatting in the future. https://github.com/oxc-project/oxc/blob/78cd5910985cece594e74884717f77ad75347b8a/crates/oxc_formatter/src/write/template.rs#L602-L603 ### Key changes The most significant change is: ```diff pub trait Format<'ast, T = ()> { - fn fmt(&self, f: &mut Formatter<'_, 'ast>) -> FormatResult<()>; + fn fmt(&self, f: &mut Formatter<'_, 'ast>); } ``` All remaining changes to the order stem from the above change: 1. Remove all `FormatResult` usages 2. Remove all `?` operators that pair with the `format!` macro 4. Commented out `PoorLayout` related logic, which is going to be fixed in the oxc-project#16093 ### Benefits We don't need to deal with `Option`, and removing all `Option` handling makes the code cleaner and more readable. Of course, the performance improved a little bit
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.

Summary
This PR changes the
Buffer::write_elementreturn type fromFormatResult<()>to()(void).Local benchmarks show that this improves performance significantly and the only possible error for
write_elementto fail is ifVecfails to allocate, which we don't need to handle.There's one downside to this. The reason why
Buffer::write_elementis that a wrapperBuffer(see the removedPreambleBuffer) can intercept the writing of an element and write additional content.This is a neat feature but wrapping
Buffers (especially if done recursively, e.g. in every Expression) comes with a significant performance penalty because each wrapping requires a dynamic dispatch call. That's whyBuffers haven't been used as much and are generally discouraged.There's a more performant solution for preambles today that didn't exist when the Buffer was introduced.
However, this doesn't work for use cases where the preamble itself is expensive OR for cases where an element should be written as a response to another element (e.g. write a line break after each "break_here" text).
Test Plan
cargo test