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

feat: nicer skip context for macro/attribute #5459

Merged
merged 2 commits into from
Jul 31, 2022

Conversation

tommilligan
Copy link
Contributor

Follow up from #5347 (comment) in discussion with @ytmimi

Refactors the way named items are skipped such that:

  • skipping each item is constant time
  • implementation is the same for attributes/macros
  • either all items can be skipped, or a set of named items

This should result in no change in behaviour.

src/skip.rs Outdated Show resolved Hide resolved
src/skip.rs Outdated

impl SkipNameContext {
pub(crate) fn append(&mut self, values: Vec<String>) {
self.values.extend(values);
Copy link
Contributor

Choose a reason for hiding this comment

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

once self.all=true does it make sense to extend? Could this be:

Suggested change
self.values.extend(values);
if !self.all {
self.values.extend(values);
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed - I refactored the inner types, but made sure the same logic applies.

src/skip.rs Outdated Show resolved Hide resolved
src/visitor.rs Outdated
Comment on lines 775 to 781
for macro_selector in config.skip_macro_invocations().0 {
match macro_selector {
MacroSelector::Name(name) => macro_names.push(name.to_string()),
MacroSelector::All => skip_context.all_macros = true,
MacroSelector::All => skip_context.macros.set_all(true),
}
}
skip_context.update_macros(macro_names);
skip_context.macros.append(macro_names);
Copy link
Contributor

Choose a reason for hiding this comment

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

Along with the comments above, I wonder if it makes sense to avoid calling to_string when "*" is in the skip_macro_invocations list by making the following update:

let contains_all = config.skip_macro_invocations().0.any(MacroSelector::is_all);

if contains_all {
    skip_context.macros.set_all(true);
} else {
    skip_context.macros.append(config.skip_macro_invocations().0.collect());
}

The code might need to be written a little differently, but I hope the general idea is understood.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think realistically it's unlikely we see a case that makes it worth this optimization, at the cost of reading the vec twice. My expectation is that "*", if given, will be the only string in the vec, as it makes all other names redundant. The unit tests that check combinations of the two are just for completeness.

@tommilligan
Copy link
Contributor Author

Updated to use the Extend trait and rename a bunch of append calls to extend for correctness.

Refactored the inner type to an enum, since it matches the behaviour nicely.

@tommilligan tommilligan requested a review from ytmimi July 26, 2022 07:16
@ytmimi
Copy link
Contributor

ytmimi commented Jul 28, 2022

@tommilligan did you push the changes? I don't think I"m seeing anything different than last time. I'm also not seeing any impls for Extend. Am I missing something?

@ytmimi ytmimi added the p-low label Jul 28, 2022
@tommilligan
Copy link
Contributor Author

@tommilligan did you push the changes? I don't think I"m seeing anything different than last time. I'm also not seeing any impls for Extend. Am I missing something?

Sorry - now pushed. Was working on the train, looks like the connection failed and I didn't notice, sorry.

Copy link
Member

@calebcartwright calebcartwright left a comment

Choose a reason for hiding this comment

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

This lgtm but will hold off on merging in case you want to take a final pass @ytmimi

@@ -775,10 +775,10 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
for macro_selector in config.skip_macro_invocations().0 {
match macro_selector {
MacroSelector::Name(name) => macro_names.push(name.to_string()),
MacroSelector::All => skip_context.all_macros = true,
MacroSelector::All => skip_context.macros.skip_all(),
Copy link
Member

Choose a reason for hiding this comment

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

I'd think we could technically break after this arm because if we reach it then it's a bit moot whether there's additional entries. However, it shouldn't really matter. I don't even a super villain could be pathological enough to make it matter

Suggested change
MacroSelector::All => skip_context.macros.skip_all(),
MacroSelector::All => {
skip_context.macros.skip_all();
break;
}

Copy link
Contributor

@ytmimi ytmimi left a comment

Choose a reason for hiding this comment

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

@tommilligan thanks for your work on this! I like the update and I'm ready to get this pulled in!

If you can rebase this on master then I can merge, otherwise @calebcartwright can merge it.

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.

3 participants