Skip to content

Optimization: Skip CalDateTime.Add(Duration) for Empty Durations #876

@axunonb

Description

@axunonb

Summary

During recurrence evaluation, CalDateTime.Add(Duration) may be invoked with an empty duration. This introduces unnecessary overhead, especially in high-frequency scenarios like GetOccurrences.

Proposed Optimization

Introduce a shortcut to bypass the operation when the duration is empty:

if (d.IsEmpty) return this;

Benchmark Results

Environment:

  • BenchmarkDotNet v0.15.0
  • Windows 11 (24H2, HudsonValley)
  • Intel Core i7-13700K, 24 logical cores
  • .NET SDK 9.0.306, Host: .NET 8.0.21
  • Toolchain: InProcessNoEmitToolchain

Without Shortcut

Method Mean Error StdDev Gen0 Gen1 Allocated
SerializeCalendar 7.457 μs 0.0559 μs 0.0495 μs 1.5640 0.0381 24.05 KB
DeserializeCalendar 15.619 μs 0.0640 μs 0.0599 μs 3.1128 0.1526 47.89 KB
GetOccurrences 924.022 μs 9.4472 μs 7.8888 μs 159.1797 99.6094 2438.35 KB

With Shortcut

Method Mean Error StdDev Gen0 Gen1 Allocated
SerializeCalendar 7.517 μs 0.0412 μs 0.0386 μs 1.5640 0.0381 24.05 KB
DeserializeCalendar 15.349 μs 0.0581 μs 0.0485 μs 3.1128 0.1526 47.89 KB
GetOccurrences 831.747 μs 5.5135 μs 4.8875 μs 149.4141 98.6328 2297.72 KB

Impact

  • ~10% improvement in GetOccurrences performance
  • Reduced allocations and GC pressure
  • No behavioral change - pure optimization

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions