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

Improve PageTableIndex and PageOffset. #122

Merged
merged 2 commits into from
Feb 10, 2020
Merged

Conversation

m-ou-se
Copy link
Contributor

@m-ou-se m-ou-se commented Jan 25, 2020

  • There are now const functions to make them: new_truncate.
  • The new truncating are used in VirtAddr to avoid needless checks.
  • The guarantee that they are in 0..512 and 0..4096 is documented.
  • This guarantee is used in Index for PageTable to avoid needless checks.
  • They are now both Into, to make it easier to use them as index.

- There are now const functions to make them: new_truncate.
- The new truncating are used in VirtAddr to avoid needless checks.
- The guarantee that they are in 0..512 and 0..4096 is documented.
- This guarantee is used in Index for PageTable to avoid needless checks.
- They are now both Into<usize>, to make it easier to use them as index.
@phil-opp
Copy link
Member

Thanks a lot for the pull request! Most of the changes look good to me, I'm just not sure about the new uses of unsafe.

While I agree that the two get_unchecked calls should be safe in this case, they're always a maintainance burden because they require a more closer review of any future changes to the PageTableIndex type. So I'm not sure whether they are worth it.

I would prefer if we could run some benchmarks before adding any unsafe blocks only for performance optimization. Maybe the compiler is able to eliminate the bounds check for the safe variant too? If not, maybe the the performance impact is negligible?

I think it's best to merge this pull request without the unsafe changes first and then discuss these changes in a followup PR.

@phil-opp
Copy link
Member

@m-ou-se Friendly ping :).

The compiler probably optimizes the checks away anyway.
@m-ou-se
Copy link
Contributor Author

m-ou-se commented Feb 10, 2020

Pong!

@m-ou-se
Copy link
Contributor Author

m-ou-se commented Feb 10, 2020

While I agree that the two get_unchecked calls should be safe in this case, they're always a maintainance burden because they require a more closer review of any future changes to the PageTableIndex type.

As the documentation now makes the guarantee that a PageTableIndex is always <512, users of this crate might also start to use unsafe indexing using it. So I don't think there would be much space for changing PageTableIndex anyway. But I definitely agree that less unsafe code is better. In this case, I'd guess that in pretty much all cases where PageTableIndex is used, the compiler can track the index back to where it is created, and should know it is <512, thus dropping any redundant asserts.

I added #[inline] to make sure the compiler can 'see' into this function from outside this create and do this optimization. Maybe all trivial/small functions in this crate should be #[inline], but that's another issue.

@phil-opp
Copy link
Member

Thanks a lot for the update!

Maybe all trivial/small functions in this crate should be #[inline], but that's another issue.

Sounds reasonable. I think with link time optimization, the compiler does cross-crate inlining also without the attribute, but not everyone is using lto.

@phil-opp phil-opp merged commit ff7d878 into rust-osdev:master Feb 10, 2020
@m-ou-se m-ou-se deleted the pageindex branch February 10, 2020 11:25
phil-opp added a commit that referenced this pull request Feb 10, 2020
@phil-opp
Copy link
Member

Released as version 0.9.1

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.

2 participants