Skip to content

feat: Wrap Error into a Box so it's size shrinks from 104 bytes to 8 bytes - #230

Merged
martin-g merged 3 commits into
apache:mainfrom
Kriskras99:feat/better_box_error
Jul 22, 2025
Merged

martin-g merged 3 commits into
apache:mainfrom
Kriskras99:feat/better_box_error

Conversation

@Kriskras99

@Kriskras99 Kriskras99 commented Jul 19, 2025 •

Copy link
Copy Markdown
Contributor

Supersedes/Closes #229.

The size of a Result is as large as it's largest variant, thus AvroResult is currently always at least 104 bytes. Therefore, even if the function only returns a usize on success the function still needs to (stack) allocate 104 bytes.

I've renamed the current Error enum to Details, and actually made it a bit bigger by unboxing Schema arguments. The new Error type is just a wrapper around a Box<Details>.

To keep the code simple and make as few changes as possible, I've added a construction function for every variant of Details. I've also changed any ok_or(Error...) to an ok_or_else(Error... as constructing the type is now "expensive" because it does an allocation.

This is a breaking change if the user is matching on the error type. The fix is easy as the user can use details or into_details to get the inner Details to match on. When the user is constructing a struct-like variant of the type this is also a breaking change. This can be fixed by changing the curly braces to round brackets and removing the item names.

@Kriskras99
Kriskras99 force-pushed the feat/better_box_error branch from 2f41217 to 2b070db Compare July 19, 2025 18:49
@Kriskras99

Copy link
Copy Markdown
Contributor Author

I also think Details::GetLogicalTypeVariant(serde_json::Value) is unused, if we're doing a breaking change on the error type might as well remove it.

In the same vein we could remove Details::DeflateCompressFinish(std::io::Error) which was deprecated in the last release.

@martin-g martin-g 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.

I like this better than #229 !
I understand that it is a bigger API break than #229 but I think it will be easier to maintain in the future since there is just one Box<> to have in mind.

Comment thread avro/src/lib.rs Resolved
Comment thread avro/src/error.rs Resolved
@martin-g

Copy link
Copy Markdown
Member

I also think Details::GetLogicalTypeVariant(serde_json::Value) is unused, if we're doing a breaking change on the error type might as well remove it.

Let's deprecate it for now. We could remove it few releases later.

We have few other items deprecated in older versions (e.g. 0.11 and 0.15) which we could remove for 0.20! But in a separate PR.

@Kriskras99

Copy link
Copy Markdown
Contributor Author

Shall I mark it as deprecated in this PR or do you also want that in a separate PR?

@martin-g

Copy link
Copy Markdown
Member

Shall I mark it as deprecated in this PR or do you also want that in a separate PR?

This PR is OK!

Kriskras99 and others added 2 commits July 21, 2025 10:31
…o 8 bytes

The size of a `Result` is as large as it's largest variant, thus `AvroResult` is
currently always at least 104 bytes. Therefore, even if the function only returns
a `usize` on success the function still needs to (stack) allocate 104 bytes.

I've renamed the current `Error` enum to `Details`, and actually made it a bit
bigger by unboxing `Schema` arguments. The new `Error` type is just a wrapper
around a `Box<Details>`.

To keep the code simple and make as few changes as possible, I've added a construction
function for every variant of `Details`. I've also changed any `ok_or(Error...`) to an
`ok_or_else(Error...` as constructing the type is now "expensive" because it does an allocation.

This is a breaking change if the user is matching on the error type. The fix is easy as the
user can use `details` or `into_details` to get the inner `Details` to match on. When the
user is constructing a struct-like variant of the type this is also a breaking change. This
can be fixed by changing the curly braces to round brackets and removing the item names.
@Kriskras99
Kriskras99 force-pushed the feat/better_box_error branch from 2b070db to b8be1fe Compare July 21, 2025 10:39
@Kriskras99

Copy link
Copy Markdown
Contributor Author

I've marked GetLogicalTypeVariant as deprecated, in addition I found that EnumSymbolIndex, ConvertU32ToUsize, BigDecimalSign, and DeflateCompress are also unused and thus marked them as deprecated too.

@Kriskras99

Copy link
Copy Markdown
Contributor Author

Comment thread avro/src/bigdecimal.rs Resolved
Comment thread avro/src/codec.rs Resolved
Comment thread avro/src/decimal.rs Resolved
@martin-g

Copy link
Copy Markdown
Member

@Kriskras99 I have no other comments on the PR!
I'll leave it open for a day or two, so others can give their opinions too.

@martin-g
martin-g merged commit 4a00c00 into apache:main Jul 22, 2025
@Kriskras99
Kriskras99 deleted the feat/better_box_error branch July 22, 2025 05:43
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