Skip to content

Commit

Permalink
Fix timelib_parse_zone() performance problem.
Browse files Browse the repository at this point in the history
This makes "new DateTimeZone("Europe/London");" 170 times faster.

This is a hotfix for derickr/timelib#99
  • Loading branch information
dstogov committed Aug 31, 2021
1 parent 32d4821 commit 6871a49
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 3 additions & 1 deletion ext/date/lib/parse_date.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ static const timelib_tz_lookup_table timelib_timezone_utc[] = {
{ "utc", 0, 0, "UTC" },
};

#define MAX_ABBR_LEN 5 /* the longest name in "timezonemap.h" and "fallbackmap.h" */

This comment has been minimized.

Copy link
@mvorisek

mvorisek Aug 31, 2021

Contributor

@dstogov ext/date/lib/parse_date.re changed in 2bf451b , but this file remain unchanged, is that correct?

This comment has been minimized.

Copy link
@dstogov

dstogov Aug 31, 2021

Author Member

@derickr it's better to regenerate it, of course. thanks for catching this.


static timelib_relunit const timelib_relunit_lookup[] = {
{ "ms", TIMELIB_MICROSEC, 1000 },
{ "msec", TIMELIB_MICROSEC, 1000 },
Expand Down Expand Up @@ -749,7 +751,7 @@ static timelib_long timelib_lookup_abbr(const char **ptr, int *dst, char **tz_ab
word = timelib_calloc(1, end - begin + 1);
memcpy(word, begin, end - begin);

if ((tp = abbr_search(word, -1, 0))) {
if (end - begin < MAX_ABBR_LEN && (tp = abbr_search(word, -1, 0))) {
value = tp->gmtoffset;
*dst = tp->type;
value -= tp->type * 3600;
Expand Down
4 changes: 3 additions & 1 deletion ext/date/lib/parse_date.re
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ static const timelib_tz_lookup_table timelib_timezone_utc[] = {
{ "utc", 0, 0, "UTC" },
};

#define MAX_ABBR_LEN 5 /* the longest name in "timezonemap.h" and "fallbackmap.h" */

static timelib_relunit const timelib_relunit_lookup[] = {
{ "ms", TIMELIB_MICROSEC, 1000 },
{ "msec", TIMELIB_MICROSEC, 1000 },
Expand Down Expand Up @@ -747,7 +749,7 @@ static timelib_long timelib_lookup_abbr(const char **ptr, int *dst, char **tz_ab
word = timelib_calloc(1, end - begin + 1);
memcpy(word, begin, end - begin);

if ((tp = abbr_search(word, -1, 0))) {
if (end - begin < MAX_ABBR_LEN && (tp = abbr_search(word, -1, 0))) {
value = tp->gmtoffset;
*dst = tp->type;
value -= tp->type * 3600;
Expand Down

0 comments on commit 6871a49

Please sign in to comment.