mirror of
https://git.haproxy.org/git/haproxy.git/
synced 2025-11-11 05:51:02 +01:00
CONTRIB: halog: avoid calling time/localtime/mktime for each line
The last commit provides time-based filtering. Unfortunately, it wastes
90% of the time calling the expensive time()/localtime()/mktime()
functions.
This patch does 3 things :
- call time()/localtime() only once to initialize the correct
struct timeinfo ;
- call mktime() only when the time has changed regardless of
the current second.
- manually add the current second to the cached result.
Doing just this is enough to multiply the parsing speed by 8.
This commit is contained in:
parent
e97b904801
commit
9f66aa9cc4
@ -537,7 +537,8 @@ int convert_date_to_timestamp(const char *field)
|
|||||||
unsigned char c;
|
unsigned char c;
|
||||||
const char *b, *e;
|
const char *b, *e;
|
||||||
time_t rawtime;
|
time_t rawtime;
|
||||||
struct tm * timeinfo;
|
static struct tm * timeinfo;
|
||||||
|
static int last_res;
|
||||||
|
|
||||||
d = mo = y = h = m = s = 0;
|
d = mo = y = h = m = s = 0;
|
||||||
e = field;
|
e = field;
|
||||||
@ -651,17 +652,28 @@ int convert_date_to_timestamp(const char *field)
|
|||||||
s = s * 10 + c;
|
s = s * 10 + c;
|
||||||
}
|
}
|
||||||
|
|
||||||
time(&rawtime);
|
if (likely(timeinfo)) {
|
||||||
timeinfo = localtime(&rawtime);
|
if (timeinfo->tm_min == m &&
|
||||||
|
timeinfo->tm_hour == h &&
|
||||||
|
timeinfo->tm_mday == d &&
|
||||||
|
timeinfo->tm_mon == mo - 1 &&
|
||||||
|
timeinfo->tm_year == y - 1900)
|
||||||
|
return last_res + s;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
time(&rawtime);
|
||||||
|
timeinfo = localtime(&rawtime);
|
||||||
|
}
|
||||||
|
|
||||||
timeinfo->tm_sec = s;
|
timeinfo->tm_sec = 0;
|
||||||
timeinfo->tm_min = m;
|
timeinfo->tm_min = m;
|
||||||
timeinfo->tm_hour = h;
|
timeinfo->tm_hour = h;
|
||||||
timeinfo->tm_mday = d;
|
timeinfo->tm_mday = d;
|
||||||
timeinfo->tm_mon = mo - 1;
|
timeinfo->tm_mon = mo - 1;
|
||||||
timeinfo->tm_year = y - 1900;
|
timeinfo->tm_year = y - 1900;
|
||||||
|
last_res = mktime(timeinfo);
|
||||||
|
|
||||||
return mktime(timeinfo);
|
return last_res + s;
|
||||||
out_err:
|
out_err:
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user