Reduce DateTimeOffset churn in the cron next-fire-time loop (3.x)#3129
Merged
Conversation
Port of the main-branch optimization (#3128) to 3.x. GetTimeAfter rebuilt a DateTimeOffset after every field even when the field already matched - and since each d.Year / d.Month / d.Day / ... access re-decomposes the tick count, an unchanged rebuild cost several decompositions plus a recomposition. In the common steady state most fields are already satisfied, so this was pure waste. - The second / minute / hour / day / month / year sections now skip rebuilding the date when the field did not change. The value they used to rebuild is provably identical to the existing one (same components, milliseconds already stripped at loop entry, same offset), so behavior is unchanged. - BitUtil.TrailingZeroCount / PopCount / TryGetMinValueStartingFrom are marked AggressiveInlining; the dead "start outside [0,63]" guard on the hottest method becomes a Debug.Assert (callers always pass an in-range value). Next-occurrence computation is 27-36% faster (largest on day-of-week, L and year-constrained expressions, which iterate the loop more). All 1401 unit tests pass on net10.0 and net472 (the net472 run exercises the De Bruijn fallback), including the randomized differential test added in #3126. Note: 3.x's L/W day-of-month handling is already allocation-free (it computes the day inline, with no per-month SortedSet), so the companion L/W zero-allocation change in #3128 has no 3.x counterpart. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WEQMdnqSWJkTN7KDemLtre
|
This was referenced Jun 27, 2026
build(deps): Bump Quartz.AspNetCore from 3.18.1 to 3.18.2
eurofurence/ef-app_backend-dotnet-core#433
Open
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary
Port to 3.x of the main-branch
DateTimeOffset-churn optimization (#3128), follow-up to #3126.GetTimeAfterrebuilt aDateTimeOffsetafter every field even when the field already matched — and since eachd.Year/d.Month/d.Day/ … access re-decomposes the tick count, an unchanged rebuild cost several decompositions plus a recomposition. In the common steady state most fields are already satisfied, so this was wasted work.BitUtil.TrailingZeroCount/PopCount/TryGetMinValueStartingFromare marked[MethodImpl(AggressiveInlining)]; the dead "startoutside[0,63]" guard on the hottest method becomes aDebug.Assert.Results
BenchmarkDotNet, AMD Ryzen 9 5950X, .NET 10, ShortRun.GetNextValidTimeAfter, before (origin/3.x= #3126) → after:0/15 * * * * ?0 0,10,…,50 * * * ?0 0/5 * * * ?0 15 10 * * ?0 0-30 9-17 * * ?0 0 8-18 ? * MON-FRI0 0 12 * * ?0 15 10 L * ?0 15 10 LW * ?0 15 10 ? * 6L0 15 10 ? * 6#3 *27–36% faster (the win is larger than on main because the monolithic 3.x loop rebuilt the date after every field). Allocation is unchanged (already zero on these paths).
Validation
Note: 3.x's
L/Wday-of-month handling is already allocation-free (it computes the day inline, with no per-monthSortedSet), so the companion L/W zero-allocation change in #3128 has no 3.x counterpart.🤖 Generated with Claude Code
https://claude.ai/code/session_01WEQMdnqSWJkTN7KDemLtre