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

de-macro-ize feature gate checking #66945

Closed
wants to merge 1 commit into from

Conversation

Centril
Copy link
Contributor

@Centril Centril commented Dec 2, 2019

This PR removes the usage of macros in feature gating preferring to take in symbols instead. The aim is to make feature gating more understandable overall while also reducing some duplication.

To check whether a feature gate is active, tcx.features().on(sym::my_feature) can be used.
Inside PostExpansionVisitor it's however better to use self.gate(...) which provides a nicer API atop of that. Outside of the visitor, if gate_feature can be used, it should be preferred over feature_err.

As a follow-up, and once #66878 is merged, it might be a good idea to add a method to Session for more convenient access to gate_feature and feature_err respectively.

Originally I intended to go for a HashSet representation, but this felt like a smaller change to just expand to a match on the symbol => field pairs.

r? @oli-obk

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Dec 2, 2019
@rust-highfive

This comment has been minimized.

@bors

This comment has been minimized.

@bors

This comment has been minimized.

@Mark-Simulacrum
Copy link
Member

It would be good to make sure this is not a perf regression, a big match statement is presumably worse than direct field access -- but it might not matter in practice (and maybe LLVM is amazing and optimizes out the match anyway).

@Centril

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Awaiting bors try build completion

@bors

This comment has been minimized.

@Centril Centril added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Dec 2, 2019
@Centril
Copy link
Contributor Author

Centril commented Dec 3, 2019

@bors try

@bors
Copy link
Contributor

bors commented Dec 3, 2019

⌛ Trying commit 359de34 with merge 19878fe...

bors added a commit that referenced this pull request Dec 3, 2019
de-macro-ize feature gate checking

This PR removes the usage of macros in feature gating preferring to take in symbols instead. The aim is to make feature gating more understandable overall while also reducing some duplication.

To check whether a feature gate is active, `tcx.features().on(sym::my_feature)` can be used.
Inside `PostExpansionVisitor` it's however better to use `self.gate(...)` which provides a nicer API atop of that. Outside of the visitor, if `gate_feature` can be used, it should be preferred over `feature_err`.

As a follow-up, and once #66878 is merged, it might be a good idea to add a method to `Session` for more convenient access to `gate_feature` and `feature_err` respectively.

Originally I intended to go for a `HashSet` representation, but this felt like a smaller change to just expand to a `match` on the symbol => field pairs.

r? @oli-obk
@bors
Copy link
Contributor

bors commented Dec 3, 2019

☀️ Try build successful - checks-azure
Build commit: 19878fe (19878feff6a7970a045b38ffbc68f6a70e9a95a2)

@rust-timer
Copy link
Collaborator

Queued 19878fe with parent f577b0e, future comparison URL.

@rust-timer
Copy link
Collaborator

Finished benchmarking try commit 19878fe, comparison URL.

/// Panics if the symbol doesn't correspond to a declared feature.
pub fn on(&self, name: Symbol) -> bool {
match name {
$(sym::$feature => self.$feature,)+
Copy link
Contributor

@petrochenkov petrochenkov Dec 5, 2019

Choose a reason for hiding this comment

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

match is a pretty inefficient tool in this case (in terms of performance).
Simple linear (not even binary) search in an array could be better here.
And hash map would be both simple and fast, see how built-in attributes gating is implemented.

Copy link
Contributor

Choose a reason for hiding this comment

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

I wish we had field pointers (like &MyStruct::my_field).
They can be emulated with offset_of, but that would mean macros again.

Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps the field approach can be kept with the use of macros being minimized rather than fully eliminated.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Initially I had a HashSet but was hoping that the match would turn into a huge lookup table and be better that way. I can certainly try a HashSet again and see if it changes anything.

Otherwise, perhaps field accesses outside check.rs would be workable (ostensibly that's where the regressions are coming from in some hot loop somewhere?) -- that should still leave us with no macros.

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 unclear to me -- why is the macro itself bad? It looks like this is still using a macro. It looks also like we're still generating the fields, so we why can't expose those is not clear to me.

/// Is the given feature enabled?
///
/// Panics if the symbol doesn't correspond to a declared feature.
pub fn on(&self, name: Symbol) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

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

If the field approach is rejected after all in favor of a method, could it be named enabled instead of on?
"on" looks like it's... an event handler of sorts? onKeyPress anyone?
The "on/off" association certainly didn't come immediately to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Heh; I went with .active(..) first, then changed to .enabled(..) and finally to .on(..) to make it shorter. 😂 .enabled(..) is fine by me.

@JohnCSimon
Copy link
Member

Ping from triage:
@Centril can you address the merge conflicts?

@joelpalmer
Copy link

Ping from Triage: Any updates @Centril?

@Dylan-DPC-zz
Copy link

Closing this due to inactivity

@Dylan-DPC-zz Dylan-DPC-zz added S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 1, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants