Skip to content

Commit 4ec602e

Browse files
committed
[date_time_scanner] try to optimize
1 parent e6cc3c6 commit 4ec602e

File tree

3 files changed

+47
-10
lines changed

3 files changed

+47
-10
lines changed

src/base/date_time_scanner.cc

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
#include "config.h"
3737
#include "date_time_scanner.cfg.hh"
3838
#include "injector.hh"
39+
#include "math_util.hh"
3940
#include "ptimec.hh"
4041
#include "scn/scn.h"
4142

@@ -336,6 +337,19 @@ date_time_scanner::scan(const char* time_dest,
336337
return retval;
337338
}
338339

340+
void
341+
date_time_scanner::clear()
342+
{
343+
this->dts_base_time = 0;
344+
this->dts_base_tm = exttm{};
345+
this->dts_fmt_lock = -1;
346+
this->dts_fmt_len = -1;
347+
this->dts_last_tv = timeval{};
348+
this->dts_last_tm = exttm{};
349+
this->dts_localtime_cached_gmt = 0;
350+
this->dts_localtime_cached_tm = tm{};
351+
}
352+
339353
void
340354
date_time_scanner::set_base_time(time_t base_time, const tm& local_tm)
341355
{
@@ -371,7 +385,29 @@ date_time_scanner::to_localtime(time_t t, exttm& tm_out)
371385
-= this->dts_local_offset_expiry % EXPIRE_TIME;
372386
} else {
373387
time_t adjust_gmt = t + this->dts_local_offset_cache;
374-
secs2tm(adjust_gmt, &tm_out.et_tm);
388+
auto adjust_gmt_min = adjust_gmt / 60;
389+
if (this->dts_localtime_cached_gmt == adjust_gmt_min)
390+
{
391+
tm_out.et_tm = this->dts_localtime_cached_tm;
392+
tm_out.et_tm.tm_sec = adjust_gmt % 60;
393+
} else {
394+
secs2tm(adjust_gmt, &tm_out.et_tm);
395+
this->dts_localtime_cached_gmt = adjust_gmt_min;
396+
this->dts_localtime_cached_tm = tm_out.et_tm;
397+
this->dts_localtime_cached_tm.tm_sec = 0;
398+
}
399+
#if 0
400+
{
401+
tm verify_tm;
402+
secs2tm(adjust_gmt, &verify_tm);
403+
require(tm_out.et_tm.tm_year == verify_tm.tm_year);
404+
require(tm_out.et_tm.tm_mon == verify_tm.tm_mon);
405+
require(tm_out.et_tm.tm_mday == verify_tm.tm_mday);
406+
require(tm_out.et_tm.tm_hour == verify_tm.tm_hour);
407+
require(tm_out.et_tm.tm_min == verify_tm.tm_min);
408+
require(tm_out.et_tm.tm_sec == verify_tm.tm_sec);
409+
}
410+
#endif
375411
}
376412
tm_out.et_gmtoff = 0;
377413
#ifdef HAVE_STRUCT_TM_TM_ZONE

src/base/date_time_scanner.hh

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,7 @@
5050
struct date_time_scanner {
5151
date_time_scanner() { this->clear(); }
5252

53-
void clear()
54-
{
55-
this->dts_base_time = 0;
56-
this->dts_base_tm = exttm{};
57-
this->dts_fmt_lock = -1;
58-
this->dts_fmt_len = -1;
59-
this->dts_last_tv = timeval{};
60-
this->dts_last_tm = exttm{};
61-
}
53+
void clear();
6254

6355
struct lock_state {
6456
int ls_fmt_index{-1};
@@ -106,6 +98,8 @@ struct date_time_scanner {
10698
time_t dts_local_offset_cache{0};
10799
time_t dts_local_offset_valid{0};
108100
time_t dts_local_offset_expiry{0};
101+
time_t dts_localtime_cached_gmt{0};
102+
tm dts_localtime_cached_tm{};
109103
const date::time_zone* dts_default_zone{nullptr};
110104

111105
static const int EXPIRE_TIME = 15 * 60;

src/logfile.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,13 @@ logfile::rebuild_index(std::optional<ui_clock::time_point> deadline)
12071207
} else {
12081208
retval = rebuild_result_t::NEW_LINES;
12091209
}
1210+
1211+
{
1212+
auto est_rem = this->estimated_remaining_lines();
1213+
if (est_rem > 0) {
1214+
this->lf_index.reserve(this->lf_index.size() + est_rem);
1215+
}
1216+
}
12101217
} else if (this->lf_sort_needed) {
12111218
retval = rebuild_result_t::NEW_ORDER;
12121219
this->lf_sort_needed = false;

0 commit comments

Comments
 (0)