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

refactor(rust!): Refactor AnyValue supertype logic #15280

Merged
merged 7 commits into from
Mar 25, 2024
Merged

Conversation

stinodego
Copy link
Member

@stinodego stinodego commented Mar 25, 2024

Changes

  • Rename existing public util any_values_to_dtype to any_values_to_supertype_and_n_dtypes and move it to the polars_core::utils module. (this is the breaking part)
  • Add two public utils based on existing functionality:
    • dtypes_to_supertype for inferring the supertype of multiple dtypes
    • any_values_to_supertype for inferring the supertype of a collection of AnyValues

@github-actions github-actions bot added breaking rust Change that breaks backwards compatibility for the Rust crate internal An internal refactor or improvement rust Related to Rust Polars labels Mar 25, 2024
{
let mut supertype = DataType::Null;
let mut dtypes = PlHashSet::<DataType>::new();
for av in values {
Copy link
Member

Choose a reason for hiding this comment

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

Just for context. The indexset perf might be better, as you keep a smaller loop. Now you have a branch on every iteration of N and we expect N to be much bigger than K, where N is no. of elements and K is no. of unique datatypes.

Copy link
Member Author

@stinodego stinodego Mar 25, 2024

Choose a reason for hiding this comment

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

I didn't realize this type of loop would be slower - I figured the collect to the IndexSet would have to do something similar (check whether the dtype hash is already in the set before adding it). Any recommendations on where I can read up on this kind of stuff?

The benefit of this implementation is that it can early exit if there is no supertype (before going through all N values), but I guess that's relatively rare so if it makes the loop slower it's not worth it.

Copy link
Member

Choose a reason for hiding this comment

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

The indexset is a different way of storing the values (inline in a vec, instead of the hash slots), the hash slots will store indexes to that vec. Can elaborate Thursday. ^^

but I guess that's relatively rare so if it makes the loop slower

I am not saying that it does :P. It is tough to tell. Though often errors in tight loops are not good. But I think the runtime is in the hashtable more than the branches here.

@@ -419,3 +420,12 @@ pub(crate) fn py_object_to_any_value(ob: &PyAny, strict: bool) -> PyResult<AnyVa
})
})
}

fn any_values_to_supertype_and_n_types(values: &[AnyValue]) -> PolarsResult<(DataType, usize)> {
Copy link
Member

Choose a reason for hiding this comment

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

Can you make this generic? Then fn any_values_to_supertype can dispatch to this one and just get the DataType out.

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 solved it slightly differently (extract the logic and re-use).

Copy link

codecov bot commented Mar 25, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 81.32%. Comparing base (53f5536) to head (aad4c59).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #15280      +/-   ##
==========================================
- Coverage   81.32%   81.32%   -0.01%     
==========================================
  Files        1359     1360       +1     
  Lines      176072   176070       -2     
  Branches     2526     2526              
==========================================
- Hits       143193   143188       -5     
- Misses      32396    32399       +3     
  Partials      483      483              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@stinodego stinodego marked this pull request as draft March 25, 2024 13:41
@stinodego stinodego marked this pull request as ready for review March 25, 2024 14:08
Copy link
Member

@ritchie46 ritchie46 left a comment

Choose a reason for hiding this comment

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

Clean second pass. 👍

@ritchie46 ritchie46 merged commit 705b148 into main Mar 25, 2024
24 checks passed
@ritchie46 ritchie46 deleted the supertype-dup branch March 25, 2024 19:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking rust Change that breaks backwards compatibility for the Rust crate internal An internal refactor or improvement rust Related to Rust Polars
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants