Skip to content

windows: Elide division-by-zero checks in Instant::now()#157328

Open
mathisbot wants to merge 1 commit into
rust-lang:mainfrom
mathisbot:opt-std-windows-time
Open

windows: Elide division-by-zero checks in Instant::now()#157328
mathisbot wants to merge 1 commit into
rust-lang:mainfrom
mathisbot:opt-std-windows-time

Conversation

@mathisbot
Copy link
Copy Markdown
Contributor

This PR teaches LLVM that the frequency of the performance counter is non null so it is able to remove division by zero checks.
This removes the panic path in mul_div_u64 and should make calls to Instant::now() (very slightly) faster.

As seen in the assembly (see godbolt below), telling LLVM that the frequency is non zero suffices to get the optimization, but I don't know if it could be a great idea to also update the signature of mul_div_u64?

MSDN page for QueryPerformanceFrequency:

On systems that run Windows XP or later, the function will always succeed when given valid parameters and will thus never return zero.

Godbolt: https://rust.godbolt.org/z/xr6x8MrPE

@rustbot rustbot added O-windows Operating system: Windows S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jun 2, 2026
@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Jun 2, 2026

ChrisDenton is not on the review rotation at the moment.
They may take a while to respond.

@rustbot
Copy link
Copy Markdown
Collaborator

rustbot commented Jun 2, 2026

r? @ChrisDenton

rustbot has assigned @ChrisDenton.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: @ChrisDenton

Comment thread library/std/src/sys/pal/windows/time.rs Outdated
this removes the panic path when dividing by the frequency.
this also makes calls to Instant::now() faster.
@mathisbot mathisbot force-pushed the opt-std-windows-time branch from fd3a114 to c2b9f39 Compare June 2, 2026 21:50
@ChrisDenton
Copy link
Copy Markdown
Member

Thanks!

@bors r+ rollup

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Jun 2, 2026

📌 Commit c2b9f39 has been approved by ChrisDenton

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 2, 2026
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Jun 2, 2026
windows: Elide division-by-zero checks in Instant::now()

This PR teaches LLVM that the frequency of the performance counter is non null so it is able to remove division by zero checks.
This removes the panic path in `mul_div_u64` and should make calls to `Instant::now()` (very slightly) faster.

As seen in the assembly (see godbolt below), telling LLVM that the frequency is non zero suffices to get the optimization, but I don't know if it could be a great idea to also update the signature of `mul_div_u64`?

MSDN page for [QueryPerformanceFrequency](https://learn.microsoft.com/en-us/windows/win32/api/profileapi/nf-profileapi-queryperformancefrequency):
> On systems that run Windows XP or later, the function will always succeed when given valid parameters and will thus never return zero.

Godbolt: https://rust.godbolt.org/z/xr6x8MrPE
jhpratt added a commit to jhpratt/rust that referenced this pull request Jun 2, 2026
…ChrisDenton

windows: Elide division-by-zero checks in Instant::now()

This PR teaches LLVM that the frequency of the performance counter is non null so it is able to remove division by zero checks.
This removes the panic path in `mul_div_u64` and should make calls to `Instant::now()` (very slightly) faster.

As seen in the assembly (see godbolt below), telling LLVM that the frequency is non zero suffices to get the optimization, but I don't know if it could be a great idea to also update the signature of `mul_div_u64`?

MSDN page for [QueryPerformanceFrequency](https://learn.microsoft.com/en-us/windows/win32/api/profileapi/nf-profileapi-queryperformancefrequency):
> On systems that run Windows XP or later, the function will always succeed when given valid parameters and will thus never return zero.

Godbolt: https://rust.godbolt.org/z/xr6x8MrPE
@jhpratt
Copy link
Copy Markdown
Member

jhpratt commented Jun 2, 2026

@bors yield

for encompassing rollup

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Jun 2, 2026

Auto build was cancelled. Cancelled workflows:

The next pull request likely to be tested is #157340.

rust-bors Bot pushed a commit that referenced this pull request Jun 2, 2026
Rollup of 2 pull requests

Successful merges:

 - #157328 (windows: Elide division-by-zero checks in Instant::now())
 - #157336 (Enable `clippy::mem_replace_with_default`)
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Jun 2, 2026
windows: Elide division-by-zero checks in Instant::now()

This PR teaches LLVM that the frequency of the performance counter is non null so it is able to remove division by zero checks.
This removes the panic path in `mul_div_u64` and should make calls to `Instant::now()` (very slightly) faster.

As seen in the assembly (see godbolt below), telling LLVM that the frequency is non zero suffices to get the optimization, but I don't know if it could be a great idea to also update the signature of `mul_div_u64`?

MSDN page for [QueryPerformanceFrequency](https://learn.microsoft.com/en-us/windows/win32/api/profileapi/nf-profileapi-queryperformancefrequency):
> On systems that run Windows XP or later, the function will always succeed when given valid parameters and will thus never return zero.

Godbolt: https://rust.godbolt.org/z/xr6x8MrPE
@jhpratt
Copy link
Copy Markdown
Member

jhpratt commented Jun 2, 2026

same thing

@bors yield

@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Jun 2, 2026

Auto build was cancelled. Cancelled workflows:

The next pull request likely to be tested is #157340.

rust-bors Bot pushed a commit that referenced this pull request Jun 2, 2026
Rollup of 2 pull requests

Successful merges:

 - #157328 (windows: Elide division-by-zero checks in Instant::now())
 - #157336 (Enable `clippy::mem_replace_with_default`)
@rust-bors

This comment has been minimized.

rust-bors Bot pushed a commit that referenced this pull request Jun 2, 2026
windows: Elide division-by-zero checks in Instant::now()

This PR teaches LLVM that the frequency of the performance counter is non null so it is able to remove division by zero checks.
This removes the panic path in `mul_div_u64` and should make calls to `Instant::now()` (very slightly) faster.

As seen in the assembly (see godbolt below), telling LLVM that the frequency is non zero suffices to get the optimization, but I don't know if it could be a great idea to also update the signature of `mul_div_u64`?

MSDN page for [QueryPerformanceFrequency](https://learn.microsoft.com/en-us/windows/win32/api/profileapi/nf-profileapi-queryperformancefrequency):
> On systems that run Windows XP or later, the function will always succeed when given valid parameters and will thus never return zero.

Godbolt: https://rust.godbolt.org/z/xr6x8MrPE
@rust-bors rust-bors Bot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Jun 2, 2026
@rust-bors
Copy link
Copy Markdown
Contributor

rust-bors Bot commented Jun 2, 2026

💔 Test for c09f2f7 failed: CI. Failed job:

@jhpratt
Copy link
Copy Markdown
Member

jhpratt commented Jun 2, 2026

not related to this PR. The tree is currently closed for that reason

@bors retry

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 2, 2026
@rust-log-analyzer
Copy link
Copy Markdown
Collaborator

A job failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

O-windows Operating system: Windows S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants