Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
timeutils: cache mktime() return values for an hour
Without this patch, cached_mktime() produces a cached value for every second, and would call mktime() again as the second changes. This can be problematic when timestamps are parsed from different seconds in succession (e.g. when the syslog timestamp is 1 second away from a timestamp being parsed using strptime or date-parser). In these cases the cache was invalidated and mktime() was called again. mktime() is _slow_ as it even calls tzset() and validates that /etc/localtime is still pointing to the same timezone. This patch changes the caching strategy: - instead of specific seconds we calculate the cached value for the top of the hour (e.g. minutes==seconds==0) - timezones never change within the same hour, so as long as we are trying to use the cached value, we will do so as long as the year/month/day/hour value matches (plus isdst) - with this, mktime() will be called once every hour. If there's some fluctuation between subsequent timestamps at the turn of the hour, we can still exhibit limited caching (e.g. subsequent timestamps from the last hour and this one), but this is a lot better than the existing behaviour where we do that for every second. Signed-off-by: Balazs Scheidler <balazs.scheidler@axoflow.com>
- Loading branch information