Skip to content

Commit

Permalink
AVRO-4007: [rust] Faster is_nullable for UnionSchema (#2961)
Browse files Browse the repository at this point in the history
* Faster `is_nullable` for UnionSchema

I'm writing several gigabytes of Avro and noticed that it seems
oddly slow. I ran a profile and noticed that about 25% of my total
run time was being spent in `UnionSchema::is_nullable`.

It looks like what's happening is that the test `x == Schema::Null`
is slow because the equality test involves a schema canonicalization.

I've updated the match to match against Schema::Null instead and see
a significant performance increase.

* Fix formatting

* Apply clippy suggestion

Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>

---------

Signed-off-by: Martin Tzvetanov Grigorov <[email protected]>
Co-authored-by: Martin Grigorov <[email protected]>
Co-authored-by: Martin Tzvetanov Grigorov <[email protected]>
  • Loading branch information
3 people committed Jun 24, 2024
1 parent 6f7be10 commit 4eda118
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion lang/rust/avro/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,7 @@ impl UnionSchema {

/// Returns true if the any of the variants of this `UnionSchema` is `Null`.
pub fn is_nullable(&self) -> bool {
!self.schemas.is_empty() && self.schemas.iter().any(|s| s == &Schema::Null)
self.schemas.iter().any(|x| matches!(x, Schema::Null))
}

/// Optionally returns a reference to the schema matched by this value, as well as its position
Expand Down

0 comments on commit 4eda118

Please sign in to comment.