From 28c85aa7e4f4ea9f165659c51f22d49090b0612b Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Sat, 29 Jul 2023 10:44:56 +0200 Subject: [PATCH] Optimize `NaiveDate::add_days` for small values --- src/naive/date.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/naive/date.rs b/src/naive/date.rs index 860593498d..0dcb36fcda 100644 --- a/src/naive/date.rs +++ b/src/naive/date.rs @@ -782,9 +782,15 @@ impl NaiveDate { /// Add a duration of `i32` days to the date. pub(crate) const fn add_days(self, days: i32) -> Option { - if days == 0 { - return Some(self); + // fast path if the result is within the same year + const ORDINAL_MASK: i32 = 0b1_1111_1111_0000; + if let Some(ordinal) = ((self.ymdf & ORDINAL_MASK) >> 4).checked_add(days) { + if ordinal > 0 && ordinal <= 365 { + let year_and_flags = self.ymdf & !ORDINAL_MASK; + return Some(NaiveDate { ymdf: year_and_flags | (ordinal << 4) }); + } } + // do the full check let year = self.year(); let (mut year_div_400, year_mod_400) = div_mod_floor(year, 400); let cycle = internals::yo_to_cycle(year_mod_400 as u32, self.of().ordinal());