Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: Use Cow in get_format_string in FFI_ArrowSchema #6853

Merged
merged 6 commits into from
Dec 7, 2024

Conversation

andygrove
Copy link
Member

Which issue does this PR close?

N/A

Rationale for this change

We're using a lot of FFI in Comet and are looking for any performance wins we can get.

ffi_arrow_schema_try_from
                        time:   [497.34 ns 499.01 ns 500.56 ns]
                        change: [-11.996% -11.725% -11.490%] (p = 0.00 < 0.05)
                        Performance has improved.

What changes are included in this PR?

Use Cow in get_format_string to reduce some string allocations.

Are there any user-facing changes?

No

@github-actions github-actions bot added the arrow Changes to the arrow crate label Dec 6, 2024
@andygrove andygrove requested review from viirya and comphead December 6, 2024 23:34
@@ -685,66 +686,68 @@ impl TryFrom<&DataType> for FFI_ArrowSchema {
}
}

fn get_format_string(dtype: &DataType) -> Result<String, ArrowError> {
fn get_format_string(dtype: &DataType) -> Result<Cow<'static, str>, ArrowError> {
Copy link
Member

Choose a reason for hiding this comment

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

Looks like we don't need it to be owned. Maybe just &str?

Copy link
Member Author

Choose a reason for hiding this comment

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

Are you saying we can use &str return instead of Cow? some of the data types use format! to create new strings so I don't think that can work?

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I see. I didn't see there are Cow::Owned.

@viirya
Copy link
Member

viirya commented Dec 7, 2024

It fails msrv check. Otherwise the performance looks good.

arrow-schema/Cargo.toml Outdated Show resolved Hide resolved
Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Thanks @andygrove and @viirya -- this looks good to me as well (modulo the test failures)

DataType::Struct(_) => Ok("+s".to_string()),
DataType::Map(_, _) => Ok("+m".to_string()),
DataType::RunEndEncoded(_, _) => Ok("+r".to_string()),
DataType::Null => Ok(Cow::Borrowed("n")),
Copy link
Contributor

Choose a reason for hiding this comment

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

This syntax is totally fine (and very explicit)

I think you can express the same thing using into which is less verbose:

For example

Suggested change
DataType::Null => Ok(Cow::Borrowed("n")),
DataType::Null => Ok("n".into()),

Copy link
Member Author

Choose a reason for hiding this comment

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

That is much nicer. Updated.

@alamb alamb added the api-change Changes to the arrow API label Dec 7, 2024
@@ -685,66 +686,68 @@ impl TryFrom<&DataType> for FFI_ArrowSchema {
}
}

fn get_format_string(dtype: &DataType) -> Result<String, ArrowError> {
fn get_format_string(dtype: &DataType) -> Result<Cow<'static, str>, ArrowError> {
Copy link
Contributor

Choose a reason for hiding this comment

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

strictly speaking this is an API change, but since the next scheduled release is a major one (allows API changes) we can merge it in to main without worry

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think that this function is exposed as part of the public API for this crate?

Copy link
Member

Choose a reason for hiding this comment

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

It is only used internally.

Copy link
Contributor

@alamb alamb left a comment

Choose a reason for hiding this comment

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

Thanks @andygrove and @viirya

@andygrove
Copy link
Member Author

Thanks @alamb and @viirya for the reviews

@andygrove andygrove merged commit 9bbbb28 into apache:main Dec 7, 2024
27 checks passed
@andygrove andygrove deleted the ffi-schema-bench branch December 7, 2024 21:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrow Changes to the arrow crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants