Skip to content

Commit 6871a49

Browse files
committed
Fix timelib_parse_zone() performance problem.
This makes "new DateTimeZone("Europe/London");" 170 times faster. This is a hotfix for derickr/timelib#99
1 parent 32d4821 commit 6871a49

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

ext/date/lib/parse_date.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ static const timelib_tz_lookup_table timelib_timezone_utc[] = {
168168
{ "utc", 0, 0, "UTC" },
169169
};
170170

171+
#define MAX_ABBR_LEN 5 /* the longest name in "timezonemap.h" and "fallbackmap.h" */
172+
171173
static timelib_relunit const timelib_relunit_lookup[] = {
172174
{ "ms", TIMELIB_MICROSEC, 1000 },
173175
{ "msec", TIMELIB_MICROSEC, 1000 },
@@ -749,7 +751,7 @@ static timelib_long timelib_lookup_abbr(const char **ptr, int *dst, char **tz_ab
749751
word = timelib_calloc(1, end - begin + 1);
750752
memcpy(word, begin, end - begin);
751753

752-
if ((tp = abbr_search(word, -1, 0))) {
754+
if (end - begin < MAX_ABBR_LEN && (tp = abbr_search(word, -1, 0))) {
753755
value = tp->gmtoffset;
754756
*dst = tp->type;
755757
value -= tp->type * 3600;

ext/date/lib/parse_date.re

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ static const timelib_tz_lookup_table timelib_timezone_utc[] = {
166166
{ "utc", 0, 0, "UTC" },
167167
};
168168

169+
#define MAX_ABBR_LEN 5 /* the longest name in "timezonemap.h" and "fallbackmap.h" */
170+
169171
static timelib_relunit const timelib_relunit_lookup[] = {
170172
{ "ms", TIMELIB_MICROSEC, 1000 },
171173
{ "msec", TIMELIB_MICROSEC, 1000 },
@@ -747,7 +749,7 @@ static timelib_long timelib_lookup_abbr(const char **ptr, int *dst, char **tz_ab
747749
word = timelib_calloc(1, end - begin + 1);
748750
memcpy(word, begin, end - begin);
749751

750-
if ((tp = abbr_search(word, -1, 0))) {
752+
if (end - begin < MAX_ABBR_LEN && (tp = abbr_search(word, -1, 0))) {
751753
value = tp->gmtoffset;
752754
*dst = tp->type;
753755
value -= tp->type * 3600;

0 commit comments

Comments
 (0)