Skip to content

fix(peer): stop sleeping while generating a GUID on Windows - #81

Merged
Segfaultd merged 1 commit into
masterfrom
fix/windows-guid-sleep
Oct 18, 2026
Merged

Segfaultd merged 1 commit into
masterfrom
fix/windows-guid-sleep

Conversation

@Segfaultd

@Segfaultd Segfaultd commented Oct 18, 2026 •

Copy link
Copy Markdown
Member

Why

Every RakPeer generates its GUID in its constructor via Get64BitUniqueRandomNumber. On Windows that function harvested timer jitter by calling RakSleep(1) eight times. A Windows Sleep(1) lasts a whole timer tick (about 15.6 ms by default), so constructing one peer cost about 125 ms. Unit tests on the simulated network create two or more peers each, so they took about 250 ms each on Windows and 0 ms on Linux. That was most of the Windows unit suite's run time.

What

The jitter loop is removed. The value was already made unique by the OS entropy source (std::random_device) and the per-process counter mixed in afterwards, which is exactly what every other platform uses. Non-Windows code is unchanged.

Results (Windows Debug)

Unit suite Before After
UnitTests.exe, one process 32.7 s 3.0 s
ctest -L unit -j 8 5.0 s 1.8 s

Tests

  • PeerGuid.UniqueRandomNumbersAreDistinctBackToBack (unit): 10,000 back-to-back values are all distinct, and none is 0 or the unassigned sentinel. Before this change it took 20 minutes on Windows; now it takes 7 ms.
  • PeerGuidTiming.GeneratingGuidsDoesNotSleep (integration, since it reads the wall clock): 256 GUIDs in under 100 ms. On the old code, 64 GUIDs took 7.9 s. Putting a single RakSleep(1) back makes it fail: 256 took about 3.97 s. The bound still catches a sleep at a 1 ms timer resolution, because 256 sleeps would take at least 256 ms.
  • Windows Debug and Release: full suite 350/350, new tests 20/20.

Summary by CodeRabbit

  • Performance
    • Generating peer identifiers on Windows is faster, avoiding delays previously caused by repeated sleep cycles.
  • Bug Fixes
    • Peer identifier generation continues to produce nonzero values and avoids the unassigned identifier value. Tests also verify that repeated calls produce distinct identifiers in a test run.

Get64BitUniqueRandomNumber harvested timer jitter on Windows by calling
RakSleep(1) eight times. A Windows Sleep(1) lasts a whole timer tick
(~15.6 ms by default), so every RakPeer took ~125 ms to construct. That
was most of the unit suite's run time on Windows, where tests on the
simulated network should take no time at all.

The jitter added nothing: the value is already made unique by the OS
entropy source and the per-process counter mixed in afterwards, which
is all every other platform uses.

Windows Debug unit suite: 32.7 s -> 3.0 s in one process, and
5.0 s -> 1.8 s under ctest -j 8.
@coderabbitai

coderabbitai Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 4ee01a6c-3e4f-41b2-8dd9-b98d6ac9aa38
📥 Commits

Reviewing files that changed from the base of the PR and between a710192 and dd5b1b8.

📒 Files selected for processing: 3
  • Source/src/peer.cpp
  • Tests/Integration/PeerGuidTimingTests.cpp
  • Tests/Unit/PeerGuidTests.cpp

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

Walkthrough

The Windows GUID-generation path no longer uses sleep cycles to mix timer jitter. New tests check generation time and verify that repeated values are nonzero, differ from the unassigned GUID value, and are unique within a test run.

Changes

GUID generation

Layer / File(s) Summary
Update Windows GUID generation
Source/src/peer.cpp
The Windows path removes eight sleep cycles. It continues to mix the clock value with OS entropy and an atomic sequence.
Check GUID generation
Tests/Integration/PeerGuidTimingTests.cpp, Tests/Unit/PeerGuidTests.cpp
The integration test checks that 256 calls take less than 100 ms. The unit test checks 10,000 generated values for nonzero, non-sentinel, and unique results.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Merge Risk: ⚪ Minimal · up to dd5b1

This change removes unnecessary sleeps from Windows GUID generation, which speeds up peer construction. No actionable merge-blocking risk remains.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 3 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: removing sleeps from Windows GUID generation.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches: 💡 1
📝 Generate docstrings: 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

A rabbit counts each GUID with care,
No sleepy cycles slow the hare.
The clock and entropy join the run,
Each value checks against the one.
Quick paws test the sequence through,
And find each number fresh and new.

Comment @coderabbitai help to get the list of available commands.

@Segfaultd
Segfaultd merged commit ccde486 into master Oct 18, 2026
12 checks passed
@Segfaultd
Segfaultd deleted the fix/windows-guid-sleep branch October 18, 2026 16:34
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.

1 participant