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

Add dedicated structs for BindingKind variants #3672

Merged
merged 1 commit into from Mar 22, 2023
Merged

Conversation

charliermarsh
Copy link
Member

Summary

This is a non-behavior-changing refactor to move the BindingKind variants to use dedicated structs.

Test Plan

Verify that all automated checks pass as expected.

pub name: &'a str,
/// The full name of the submodule being imported.
/// Given `import foo.bar`, `full_name` would be "foo.bar".
pub full_name: &'a 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.

I think we can improve some of these abstractions (better names, shared traits). This PR doesn't attempt to improve the object hierarchy at all -- it's just a "straight" migration.

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good. I hoped to find a name in the python specification but didn't find any other than dotted name. What came to my mind is that this is the path but there's no precedence for this term

StarImportation(StarImportation),
Importation(Importation<'a>),
FromImportation(FromImportation<'a>),
SubmoduleImportation(SubmoduleImportation<'a>),
Copy link
Member Author

Choose a reason for hiding this comment

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

Learning opportunity for me: when would we want to Box these?

Copy link
Contributor

Choose a reason for hiding this comment

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

When we want the enum to have the size of a box instead of the biggest struct, I think.

Copy link
Member

Choose a reason for hiding this comment

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

Boxing a variant reduces the size of that variant to a single pointer instead of the size of the inner struct + padding.

I recommend boxing when one variant is significantly larger than all other variants.

@github-actions
Copy link
Contributor

github-actions bot commented Mar 22, 2023

PR Check Results

Ecosystem

✅ ecosystem check detected no changes.

Benchmark

Linux

group                                      main                                   pr
-----                                      ----                                   --
linter/all-rules/large/dataset.py          1.01     13.8±0.07ms     2.9 MB/sec    1.00     13.6±0.06ms     3.0 MB/sec
linter/all-rules/numpy/ctypeslib.py        1.00      3.6±0.01ms     4.7 MB/sec    1.00      3.6±0.01ms     4.7 MB/sec
linter/all-rules/numpy/globals.py          1.00    492.1±0.98µs     6.0 MB/sec    1.01    495.1±1.76µs     6.0 MB/sec
linter/all-rules/pydantic/types.py         1.01      6.0±0.03ms     4.2 MB/sec    1.00      6.0±0.02ms     4.3 MB/sec
linter/default-rules/large/dataset.py      1.02      7.3±0.02ms     5.5 MB/sec    1.00      7.2±0.03ms     5.6 MB/sec
linter/default-rules/numpy/ctypeslib.py    1.00   1638.6±2.45µs    10.2 MB/sec    1.00   1631.5±8.71µs    10.2 MB/sec
linter/default-rules/numpy/globals.py      1.00    179.1±0.44µs    16.5 MB/sec    1.03    183.7±4.14µs    16.1 MB/sec
linter/default-rules/pydantic/types.py     1.01      3.4±0.02ms     7.5 MB/sec    1.00      3.4±0.01ms     7.5 MB/sec

Windows

group                                      main                                   pr
-----                                      ----                                   --
linter/all-rules/large/dataset.py          1.01     21.2±1.01ms  1966.1 KB/sec    1.00     20.9±0.80ms  1992.2 KB/sec
linter/all-rules/numpy/ctypeslib.py        1.00      5.5±0.25ms     3.0 MB/sec    1.00      5.5±0.35ms     3.0 MB/sec
linter/all-rules/numpy/globals.py          1.03   719.5±57.55µs     4.1 MB/sec    1.00   700.8±62.16µs     4.2 MB/sec
linter/all-rules/pydantic/types.py         1.01      9.1±0.39ms     2.8 MB/sec    1.00      9.0±0.38ms     2.8 MB/sec
linter/default-rules/large/dataset.py      1.00     10.2±0.69ms     4.0 MB/sec    1.15     11.7±0.80ms     3.5 MB/sec
linter/default-rules/numpy/ctypeslib.py    1.03      2.5±0.20ms     6.7 MB/sec    1.00      2.4±0.10ms     6.9 MB/sec
linter/default-rules/numpy/globals.py      1.00   279.7±20.23µs    10.5 MB/sec    1.02   284.0±16.68µs    10.4 MB/sec
linter/default-rules/pydantic/types.py     1.00      5.1±0.25ms     5.0 MB/sec    1.06      5.3±0.40ms     4.8 MB/sec

Copy link
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

🎸🎸

BindingKind::Importation(.., full_name) => full_name,
BindingKind::FromImportation(.., full_name) => full_name.as_str(),
BindingKind::SubmoduleImportation(.., full_name) => full_name,
BindingKind::Importation(Importation { full_name, .. }) => full_name,
Copy link
Member

Choose a reason for hiding this comment

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

Nit. This could be moved to a 'full_name' method on BindingKind to avoid repetition

Copy link
Member Author

Choose a reason for hiding this comment

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

It doesn't exist on all variants though -- so would it return Option<&str>?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, it would have to return an Option

@@ -366,6 +385,54 @@ impl nohash_hasher::IsEnabled for BindingId {}
// StarImportation
// FutureImportation

#[derive(Clone, Debug)]
pub struct Export {
/// The names of the bindings exported via `__all__`.
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for commenting the fields.

pub name: &'a str,
/// The full name of the submodule being imported.
/// Given `import foo.bar`, `full_name` would be "foo.bar".
pub full_name: &'a str,
Copy link
Member

Choose a reason for hiding this comment

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

Sounds good. I hoped to find a name in the python specification but didn't find any other than dotted name. What came to my mind is that this is the path but there's no precedence for this term

StarImportation(StarImportation),
Importation(Importation<'a>),
FromImportation(FromImportation<'a>),
SubmoduleImportation(SubmoduleImportation<'a>),
Copy link
Member

Choose a reason for hiding this comment

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

Boxing a variant reduces the size of that variant to a single pointer instead of the size of the inner struct + padding.

I recommend boxing when one variant is significantly larger than all other variants.

@charliermarsh charliermarsh merged commit 189c9d4 into main Mar 22, 2023
13 checks passed
@charliermarsh charliermarsh deleted the charlie/structs branch March 22, 2023 23:08
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.

None yet

3 participants