Skip to content

Add to AnyNode and AnyNodeRef conversion methods to AstNode #4783

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

Merged
merged 1 commit into from
Jun 2, 2023

Conversation

MichaReiser
Copy link
Member

Summary

Adds the new as_any_node_ref and into_node_ref helpers to AstNode.
These methods can be useful to avoid monomorphization when working with generics constraint to AstNode.

/// Formats the leading comments of a node.
pub(crate) fn leading_comments<T>(node: &T) -> FormatLeadingComments
where
    T: AstNode,
{
    FormatLeadingComments {
        node: node.as_any_node_ref(),
    }
}

#[derive(Copy, Clone, Debug)]
pub(crate) struct FormatLeadingComments<'a> {
    node: AnyNodeRef<'a>,
}

You can see how FormatLeadingComments doesn't require a T: AstNode constraint.

Test Plan

I'm using this in my comment formatting branch.

@MichaReiser
Copy link
Member Author

MichaReiser commented Jun 1, 2023

@MichaReiser MichaReiser added the internal An internal refactor or improvement label Jun 1, 2023
@github-actions
Copy link
Contributor

github-actions bot commented Jun 1, 2023

PR Check Results

Ecosystem

✅ ecosystem check detected no changes.

Benchmark

Linux

group                                      main                                   pr
-----                                      ----                                   --
linter/all-rules/large/dataset.py          1.01     16.7±0.49ms     2.4 MB/sec    1.00     16.5±0.40ms     2.5 MB/sec
linter/all-rules/numpy/ctypeslib.py        1.00      3.9±0.13ms     4.2 MB/sec    1.00      3.9±0.11ms     4.3 MB/sec
linter/all-rules/numpy/globals.py          1.01   499.7±23.45µs     5.9 MB/sec    1.00   496.2±19.13µs     5.9 MB/sec
linter/all-rules/pydantic/types.py         1.02      6.8±0.23ms     3.7 MB/sec    1.00      6.7±0.18ms     3.8 MB/sec
linter/default-rules/large/dataset.py      1.01      7.8±0.37ms     5.2 MB/sec    1.00      7.7±0.21ms     5.3 MB/sec
linter/default-rules/numpy/ctypeslib.py    1.00  1711.9±48.40µs     9.7 MB/sec    1.06  1809.2±122.94µs     9.2 MB/sec
linter/default-rules/numpy/globals.py      1.00    203.8±7.40µs    14.5 MB/sec    1.02   208.5±10.73µs    14.2 MB/sec
linter/default-rules/pydantic/types.py     1.01      3.6±0.10ms     7.1 MB/sec    1.00      3.5±0.13ms     7.2 MB/sec
parser/large/dataset.py                    1.00      6.0±0.19ms     6.8 MB/sec    1.00      6.0±0.16ms     6.8 MB/sec
parser/numpy/ctypeslib.py                  1.00  1169.4±46.95µs    14.2 MB/sec    1.02  1189.7±45.92µs    14.0 MB/sec
parser/numpy/globals.py                    1.00   120.0±10.27µs    24.6 MB/sec    1.02    122.7±6.77µs    24.0 MB/sec
parser/pydantic/types.py                   1.00      2.6±0.10ms     9.8 MB/sec    1.01      2.6±0.09ms     9.7 MB/sec

Windows

group                                      main                                   pr
-----                                      ----                                   --
linter/all-rules/large/dataset.py          1.06     19.4±0.47ms     2.1 MB/sec    1.00     18.4±0.64ms     2.2 MB/sec
linter/all-rules/numpy/ctypeslib.py        1.05      4.9±0.16ms     3.4 MB/sec    1.00      4.7±0.17ms     3.5 MB/sec
linter/all-rules/numpy/globals.py          1.03   572.2±18.59µs     5.2 MB/sec    1.00   556.3±16.55µs     5.3 MB/sec
linter/all-rules/pydantic/types.py         1.04      8.2±0.26ms     3.1 MB/sec    1.00      7.8±0.27ms     3.3 MB/sec
linter/default-rules/large/dataset.py      1.14     10.2±0.29ms     4.0 MB/sec    1.00      8.9±0.25ms     4.6 MB/sec
linter/default-rules/numpy/ctypeslib.py    1.11      2.1±0.07ms     7.9 MB/sec    1.00  1884.7±53.96µs     8.8 MB/sec
linter/default-rules/numpy/globals.py      1.03    243.7±9.19µs    12.1 MB/sec    1.00   235.7±13.27µs    12.5 MB/sec
linter/default-rules/pydantic/types.py     1.08      4.5±0.12ms     5.7 MB/sec    1.00      4.1±0.22ms     6.2 MB/sec
parser/large/dataset.py                    1.00      7.1±0.17ms     5.7 MB/sec    1.29      9.2±0.39ms     4.4 MB/sec
parser/numpy/ctypeslib.py                  1.00  1358.2±33.61µs    12.3 MB/sec    1.24  1683.3±55.23µs     9.9 MB/sec
parser/numpy/globals.py                    1.00    139.7±6.20µs    21.1 MB/sec    1.19    166.0±9.82µs    17.8 MB/sec
parser/pydantic/types.py                   1.00      3.1±0.13ms     8.2 MB/sec    1.25      3.9±0.22ms     6.6 MB/sec

@MichaReiser MichaReiser requested a review from konstin June 1, 2023 14:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
internal An internal refactor or improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants