[SERVER-68289] Avoid excessive allocations in timelib library Created: 26/Jul/22  Updated: 29/Oct/23  Resolved: 29/Sep/22

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: 6.2.0-rc0

Type: Task Priority: Major - P3
Reporter: Alberto Massari Assignee: Alberto Massari
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
is depended on by SERVER-60141 Upgrade timelib to 2021.09 or later Closed
Backwards Compatibility: Fully Compatible
Sprint: QE 2022-09-19, QE 2022-10-03
Participants:

 Description   

Whenever a time object is manipulated, the associated timezone is queried for its offset from the UTC, using these patterns:

void timelib_update_from_sse(timelib_time *tm)
...
			gmt_offset = timelib_get_time_zone_info(tm->sse, tm->tz_info);
			timelib_unixtime2gmt(tm, tm->sse + gmt_offset->offset);
			timelib_time_offset_dtor(gmt_offset);

static void do_adjust_timezone(timelib_time *tz, timelib_tzinfo *tzi)
...
			current = timelib_get_time_zone_info(tz->sse, tzi);
			after = timelib_get_time_zone_info(tz->sse - current->offset, tzi);
			tz->is_localtime = 1;
 
			in_transition = (
				((tz->sse - after->offset) >= (after->transition_time + (current->offset - after->offset))) &&
				((tz->sse - after->offset) < after->transition_time)
			);
 
			if ((current->offset != after->offset) && !in_transition) {
				adjustment = -after->offset;
			} else {
				adjustment = -current->offset;
			}
			timelib_time_offset_dtor(current);
			timelib_time_offset_dtor(after);

Seconds TimeZone::utcOffset(Date_t date) const {
    if (isTimeZoneIDZone()) {
        auto* offset = timelib_get_time_zone_info(
            durationCount<Seconds>(date.toDurationSinceEpoch()), _tzInfo.get());
        auto timezoneOffsetFromUTC = Seconds(offset->offset);
        timelib_time_offset_dtor(offset);

In these cases the caller is only interested in the offset from UTC, instead of the full set of information available from timelib_get_time_zone_info; using this API has the side effect of requiring the allocations and immediate deallocation of two memory buffers (for the returned struct and for the string representing the timezone name).
The code could use a simpler API similar to timelib_get_current_offset (that accepts a full timelib_time rather than the time and the timezone) in order to return just the offset without the extra information



 Comments   
Comment by Githook User [ 29/Sep/22 ]

Author:

{'name': 'Alberto Massari', 'email': 'alberto.massari@mongodb.com', 'username': 'albymassari'}

Message: SERVER-68289 Use new, faster, API from timelib
Branch: master
https://github.com/mongodb/mongo/commit/610dbc3b0bea69b9622429e4d5e550b95d2f8ffe

Generated at Thu Feb 08 06:10:24 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.