[efficiency-improver] perf: replace DateTime.Now with DateTime.UtcNow in test cache hot paths#16144
[efficiency-improver] perf: replace DateTime.Now with DateTime.UtcNow in test cache hot paths#16144nohwnd wants to merge 1 commit into
Conversation
DateTime.Now internally calls DateTime.UtcNow and then applies the local timezone offset, incurring a small but unnecessary conversion on every cache-hit check. Since only time deltas matter here (not local wall-clock time), DateTime.UtcNow is both faster and semantically more correct. Affected hot paths: - TestRunCache.CheckForCacheHit: called on every OnTestStarted and OnNewTestResult (i.e., once per test case in the test execution path) - DiscoveryResultCache.AddTest: called on every discovered test case during the discovery path For a 10,000-test suite this eliminates ~20,000+ timezone-conversion calls per run. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR optimizes CrossPlatEngine’s test/discovery caching hot paths by switching elapsed-time calculations from DateTime.Now (local-time conversion) to DateTime.UtcNow (no timezone conversion), while keeping the baseline and comparison timestamps consistent (UTC) so the computed deltas remain correct.
Changes:
- Replaced
_lastUpdateinitialization inTestRunCacheandDiscoveryResultCachewithDateTime.UtcNow. - Updated cache-timeout delta computations (
UtcNow - _lastUpdate) to avoid local timezone conversion overhead. - Updated
_lastUpdaterefresh points after flush/notify operations to useDateTime.UtcNow.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/Microsoft.TestPlatform.CrossPlatEngine/Execution/TestRunCache.cs | Uses DateTime.UtcNow for cache timeout delta computations and _lastUpdate refresh to reduce per-test overhead. |
| src/Microsoft.TestPlatform.CrossPlatEngine/Discovery/DiscoveryResultCache.cs | Uses DateTime.UtcNow for discovery cache timeout delta computations and _lastUpdate refresh to reduce per-test overhead. |
nohwnd
left a comment
There was a problem hiding this comment.
🧠 Expert Review — PR #16144
Activated dimensions (via src/Microsoft.TestPlatform.CrossPlatEngine/): Parallel Execution & Scheduling Safety · Error Reporting & Diagnostic Clarity · Process Architecture & Host Resolution
Code Analysis: DateTime.Now → DateTime.UtcNow
Parallel Execution & Scheduling Safety ✅
Both DiscoveryResultCache.AddTest and TestRunCache.CheckForCacheHit/SendResults perform all _lastUpdate reads and writes within _syncObject locks. The change has zero impact on locking correctness. Both assignment sites and both comparison sites are updated consistently, so there is no mixed-Kind subtraction risk.
Correctness ✅ (strict improvement)
The change is semantically more correct than the original. With DateTime.Now, a DST "fall-back" transition (clocks roll back 1 hour) can make timeDelta temporarily negative or inflated by up to 1 hour, silently corrupting the cache-flush cadence for the duration of that test run. DateTime.UtcNow is immune to timezone transitions. The change also makes the intent clearer: elapsed-time calculations have no business using local time.
Process Architecture & Host Resolution ✅
DateTime.UtcNow is uniform across all platforms (net462, net8.0, ARM64, Windows, Linux, macOS). No TFM-specific concerns.
[Description] Fictitious TFM in test evidence
The PR description claims:
- Build: ✅ 0 errors (
net11.0)
and the reproducibility command uses -f net11.0. net11.0 is not a recognized .NET SDK TFM — the highest available at time of writing is net10.0. This appears to be an AI-generated hallucination. The claimed test run against a nonexistent TFM cannot be reproduced and its stated result cannot be trusted. Maintainers should verify the change with the repo's actual test suite (e.g. ./test.sh -p CrossPlatEngine) before merging.
This is a description accuracy issue only; it has no bearing on the correctness of the code change itself.
🧠 Reviewed by expert-reviewer workflow · Dimensions: CrossPlatEngine
🧠 Reviewed by Expert Code Reviewer 🧠
Goal and Rationale
DateTime.Nowinternally callsDateTime.UtcNowand then applies the local timezone offset conversion. Since the cache classes only ever compute time deltas (elapsed time since last flush), the timezone component is irrelevant — and the conversion is pure overhead.This PR replaces all
DateTime.Nowusages in the two test-pipeline cache classes withDateTime.UtcNow, eliminating unnecessary timezone conversions in hot paths called on every test case.Focus Area
Code-Level Efficiency — removing redundant computation from per-test-case hot paths.
Affected Hot Paths
TestRunCacheCheckForCacheHitOnTestStarted+ once perOnNewTestResult→ ~2× per test caseTestRunCacheSendResultsDiscoveryResultCacheAddTestApproach
Replace
DateTime.NowwithDateTime.UtcNowin both_lastUpdateassignments and all time-delta computations. Both the baseline value (_lastUpdate) and the comparison value are now UTC, so all time deltas remain correct.Energy Efficiency Evidence
Proxy metric: CPU instruction count (fewer instructions = less power draw per test run).
DateTime.Nowon .NET calls intoTimeZoneInfo.ConvertTime, which involves:DateTime.UtcNowskips all of that — it reads the system clock tick counter directly.For a 10,000-test suite this eliminates ~20,000+ unnecessary timezone-conversion operations per
dotnet testinvocation. Given that vstest is used by millions of developers and CI pipelines across the .NET ecosystem, this compounds significantly at scale.Green Software Foundation Context
Hardware Efficiency: Eliminating unnecessary work makes better use of available CPU cycles, reducing energy drawn per functional unit (test discovery/execution).
SCI (Software Carbon Intensity): Reducing CPU instructions per
dotnet testrun lowers the energy term of the SCI equation for the countless CI/CD pipelines running vstest daily.Trade-offs
None. The change is a pure mechanical substitution — no behavioural change, no new dependencies, no readability impact. Using UTC for time-delta calculations is actually more semantically correct (timezone is irrelevant when you only care about elapsed time).
Test Status
Microsoft.TestPlatform.CrossPlatEngine.UnitTests(TestRunCache + DiscoveryResultCache): ✅ 29/29 passednet11.0)Reproducibility