Skip to content

Improve performance of System::Time.monotonic on Windows#16555

Open
straight-shoota wants to merge 7 commits intocrystal-lang:masterfrom
straight-shoota:perf/time.monotonic-windows
Open

Improve performance of System::Time.monotonic on Windows#16555
straight-shoota wants to merge 7 commits intocrystal-lang:masterfrom
straight-shoota:perf/time.monotonic-windows

Conversation

@straight-shoota
Copy link
Member

No description provided.

@straight-shoota straight-shoota self-assigned this Jan 6, 2026
@straight-shoota straight-shoota added performance platform:windows Windows support based on the MSVC toolchain / Win32 API topic:stdlib:time labels Jan 6, 2026
LibC.QueryPerformanceCounter(out ticks)
frequency = performance_frequency
{ticks // frequency, (ticks.remainder(frequency) * NANOSECONDS_PER_SECOND / frequency).to_i32}
{ticks // frequency, (ticks.remainder(frequency) &* NANOSECONDS_PER_SECOND / frequency).to_i32!}
Copy link
Contributor

Choose a reason for hiding this comment

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

The float division can probably be replaced with an integer one.

Also I would prefer tdiv over // here (or % over remainder) as this way they would have the same sign behavior.

@ysbaddaden
Copy link
Collaborator

Damn, tdivmod raises a math overflow.

@straight-shoota
Copy link
Member Author

Are you sure it's tdivmod raising an error? It shouldn't raise on any other conditions than //.

The exception seems to come from the event loop.
And all other windows runners are stalled.

@straight-shoota
Copy link
Member Author

The problem seems to be that the type of the output parameter ticks changes when I assign the return value of tdivmod.
That shouldn't affect the previous typing for the lib call, but apparently it does 🤷

@ysbaddaden
Copy link
Collaborator

Nice catch.

Indeed, it wasn't tdivmod raising a math overflow, but System::WaitableTimer#set that does checked mul / add / sub ops on Time::Instant 🙇

@straight-shoota straight-shoota added this to the 1.20.0 milestone Feb 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance platform:windows Windows support based on the MSVC toolchain / Win32 API topic:stdlib:time

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants