Skip to content

Performance + maintainability improvements for HLC [timestamps, headers, coordinator, GUID factory]#34

Merged
dexcompiler merged 10 commits intomainfrom
hlc-perf-improvements
Feb 8, 2026
Merged

Performance + maintainability improvements for HLC [timestamps, headers, coordinator, GUID factory]#34
dexcompiler merged 10 commits intomainfrom
hlc-perf-improvements

Conversation

@dexcompiler
Copy link
Copy Markdown
Owner

HLC timestamp

  • Optimized WriteTo(Span<byte>)/ReadFrom(ReadOnlySpan<byte>) using BinaryPrimitives for counter/node.
  • Added HlcTimestamp.Parse(ReadOnlySpan<char>) to enable allocation-free parsing from header segments.
  • Small tweaks to packed-int conversion and parsing to reduce casts/scans.

HLC coordinator + messaging header

  • Reduced allocations in HlcMessageHeader.ToString( ) by using string.Create + Guid.TryFormat("N") instead of repeated concatenation.
  • Removed Split allocations in Parse(string) by using span-based parsing.
  • HlcStatistics: use long.Abs(delta) to avoid the Math.Abs(long.MinValue) overflow edge-case.

HLC coordinator file organization
Split HlcCoordinator.cs supporting types into dedicated files for readability and smaller diffs:

  • HlcStatistics → HlcStatistics.cs
  • HlcMessageHeader → HlcMessageHeader.cs
  • HlcClusterRegistry → HlcClusterRegistry.cs

HlcGuidFactory hot paths

  • Increased random refill buffer size to reduce RNG refill frequency under lock.
  • Simplified Witness(HlcTimestamp) max selection to avoid temporary struct construction and repeated comparisons.
  • Tightened UUID packing in CreateGuidFromHlc(HlcTimestamp, ReadOnlySpan<byte>) using BinaryPrimitives for counter/node, while preserving version/variant bit layout.
  • Simplified Witness(long) max computation logic.

@dexcompiler
Copy link
Copy Markdown
Owner Author

@copilot review

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 8, 2026

@dexcompiler I've opened a new pull request, #35, to work on those changes. Once the pull request is ready, I'll request review from you.

@dexcompiler
Copy link
Copy Markdown
Owner Author

@cursoragent review this pr

@cursor
Copy link
Copy Markdown

cursor bot commented Feb 8, 2026

PR review

I pulled hlc-perf-improvements and ran the full test suite in Release: all green (Clockworks.Tests 109/109, property-tests 58/58).

What looks great

  • Timestamp serialization: BinaryPrimitives use in HlcTimestamp.WriteTo/ReadFrom is clean and should be measurably faster on hot paths.
  • Header formatting/parsing: HlcMessageHeader.ToString() switching to string.Create + Guid.TryFormat("N") is a solid allocation reduction, and the span-based Parse avoids Split churn.
  • Coordinator refactor: splitting HlcStatistics / HlcMessageHeader / HlcClusterRegistry into dedicated files improves readability with minimal behavioral risk.
  • GUID factory: larger RNG refill buffer + the simplified Witness max-selection logic should reduce lock hold time and temporary struct work.

Nits / edge-case notes (non-blocking unless you want to tighten)

  • HlcTimestamp.FromPackedInt64 future-proofing: it currently does packed >> 16 (signed shift). If the packed value ever has bit 63 set, that sign-extends and can yield a negative wallTimeMs. It’s “far future” for ms-in-48-bits, but easy to avoid by shifting as ulong during unpack.
  • Parsing strictness: HlcTimestamp.Parse(ReadOnlySpan<char>) now uses first @ / first . before it (via IndexOf). That’s probably what we want, just noting it’s slightly stricter/different than “last separator” behavior on malformed inputs.
  • HlcMessageHeader.TryParse: it’s still exception-driven (catch-all). If this is on a hot ingress path with potentially invalid inputs, a span-based TryParse (using Guid.TryParseExact(..., "N"), etc.) would avoid exception overhead.

Overall: LGTM—the perf/memory changes are sensible, the refactor improves maintainability, and tests pass.

Open in Cursor Open in Web

Performance + maintainability improvements for HLC [timestamps, headers, coordinator, GUID factory]
@dexcompiler
Copy link
Copy Markdown
Owner Author

@cursoragent I have the made some changes to HlcMessageHeader.TryParse. See commit

@cursor
Copy link
Copy Markdown

cursor bot commented Feb 8, 2026

Nice update — the new HlcMessageHeader.TryParse is now non-exceptional on invalid input and stays allocation-free on the fast path (span slicing + Guid.TryParseExact("N") + HlcTimestamp.TryParse). I also like that you added coverage for timestamp-only / timestamp+correlation / invalid cases in HlcMessageHeaderTests.

One small correctness nit: HlcTimestamp.TryParse(ReadOnlySpan<char>) currently returns true on empty input (and returns a default timestamp). That makes HlcMessageHeader.TryParse("") return false (due to the outer null/empty check), but it could still accept empty timestamp segments in other call paths. I’d recommend making HlcTimestamp.TryParse return false when s.IsEmpty (more consistent with TryParse conventions).

Tests still pass for me in Release after the change (now 112 unit tests + 58 property tests).

Open in Cursor Open in Web

@dexcompiler dexcompiler merged commit 84a4e65 into main Feb 8, 2026
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