Skip to content

Commit c646f54

Browse files
committed
[pcre2pp] try to optimize matches... but doesn't do much...
1 parent 07aaba3 commit c646f54

File tree

8 files changed

+87
-48
lines changed

8 files changed

+87
-48
lines changed

src/base/time_util.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ exttm::from_tv(const timeval& tv)
380380
{
381381
auto retval = exttm{};
382382

383-
retval.et_tm = *gmtime(&tv.tv_sec);
383+
gmtime_r(&tv.tv_sec, &retval.et_tm);
384384
retval.et_nsec = tv.tv_usec * 1000;
385385

386386
return retval;

src/log_format.cc

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,11 +1401,9 @@ external_log_format::scan(logfile& lf,
14011401
continue;
14021402
}
14031403

1404-
auto match_res = pat->capture_from(line_sf)
1405-
.into(md)
1406-
.matches(PCRE2_NO_UTF_CHECK)
1407-
.ignore_error();
1408-
if (!match_res) {
1404+
auto found_match
1405+
= pat->capture_from(line_sf).into(md).found_p(PCRE2_NO_UTF_CHECK);
1406+
if (!found_match) {
14091407
if (!this->lf_pattern_locks.empty() && pat_index != -1) {
14101408
curr_fmt = -1;
14111409
pat_index = -1;

src/log_format_fwd.hh

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,18 @@
3232
#ifndef lnav_log_format_fwd_hh
3333
#define lnav_log_format_fwd_hh
3434

35+
#include <chrono>
36+
#include <cstdint>
37+
#include <optional>
3538
#include <utility>
3639

3740
#include <sys/types.h>
41+
#include <time.h>
3842

3943
#include "ArenaAlloc/arenaalloc.h"
4044
#include "base/file_range.hh"
45+
#include "base/intern_string.hh"
46+
#include "base/log_level_enum.hh"
4147
#include "base/map_util.hh"
4248
#include "base/string_attr_type.hh"
4349
#include "byte_array.hh"
@@ -153,11 +159,12 @@ public:
153159
ll_sub_offset(0), ll_valid_utf(1), ll_level(lev), ll_module_id(mod),
154160
ll_meta_mark(0), ll_expr_mark(0)
155161
{
156-
memset(this->ll_schema, 0, sizeof(this->ll_schema));
162+
this->ll_schema[0] = 0;
163+
this->ll_schema[1] = 0;
157164
}
158165

159166
logline(file_off_t off,
160-
const struct timeval& tv,
167+
const timeval& tv,
161168
log_level_t lev,
162169
uint8_t mod = 0,
163170
uint8_t opid = 0)
@@ -166,7 +173,8 @@ public:
166173
ll_expr_mark(0)
167174
{
168175
this->set_time(tv);
169-
memset(this->ll_schema, 0, sizeof(this->ll_schema));
176+
this->ll_schema[0] = 0;
177+
this->ll_schema[1] = 0;
170178
}
171179

172180
/** @return The offset of the line in the file. */
@@ -192,10 +200,10 @@ public:
192200

193201
void to_exttm(struct exttm& tm_out) const
194202
{
195-
auto secs = static_cast<time_t>(
203+
const auto secs = static_cast<time_t>(
196204
this->get_time<std::chrono::seconds>().count());
197205

198-
tm_out.et_tm = *gmtime(&secs);
206+
gmtime_r(&secs, &tm_out.et_tm);
199207
tm_out.et_nsec
200208
= this->get_subsecond_time<std::chrono::nanoseconds>().count();
201209
}
@@ -216,10 +224,7 @@ public:
216224
};
217225
}
218226

219-
void set_time(const timeval& tv)
220-
{
221-
this->ll_time = to_us(tv);
222-
}
227+
void set_time(const timeval& tv) { this->ll_time = to_us(tv); }
223228

224229
template<typename T>
225230
void set_subsecond_time(T sub)
@@ -340,8 +345,6 @@ public:
340345
memcpy(this->ll_schema, ba.in(), sizeof(this->ll_schema));
341346
}
342347

343-
char get_schema() const { return this->ll_schema[0]; }
344-
345348
/**
346349
* Perform a partial match of the given schema against this log line.
347350
* Storing the full schema is not practical, so we just keep the first four

src/pcrepp/pcre2pp.cc

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@
3434
#include "config.h"
3535
#include "ww898/cp_utf8.hpp"
3636

37-
namespace lnav {
38-
namespace pcre2pp {
37+
namespace lnav::pcre2pp {
3938

4039
std::string
4140
match_data::to_string() const
@@ -395,6 +394,53 @@ code::named_captures::end() const
395394
};
396395
}
397396

397+
bool
398+
matcher::found_p(uint32_t options)
399+
{
400+
this->mb_input.i_offset = this->mb_input.i_next_offset;
401+
402+
if (this->mb_input.i_offset == -1) {
403+
return false;
404+
}
405+
406+
auto rc = pcre2_match(this->mb_code.p_code.in(),
407+
this->mb_input.i_string.udata(),
408+
this->mb_input.i_string.length(),
409+
this->mb_input.i_offset,
410+
options,
411+
this->mb_match_data.md_data.in(),
412+
nullptr);
413+
414+
if (rc > 0) {
415+
this->mb_match_data.md_input = this->mb_input;
416+
this->mb_match_data.md_code = &this->mb_code;
417+
this->mb_match_data.md_capture_end = rc;
418+
if (this->mb_match_data[0]->empty()
419+
&& this->mb_match_data[0]->sf_end >= this->mb_input.i_string.sf_end)
420+
{
421+
this->mb_input.i_next_offset = -1;
422+
} else if (this->mb_match_data[0]->empty()) {
423+
this->mb_input.i_next_offset
424+
= this->mb_match_data.md_ovector[1] + 1;
425+
} else {
426+
this->mb_input.i_next_offset = this->mb_match_data.md_ovector[1];
427+
}
428+
this->mb_match_data.md_input.i_next_offset
429+
= this->mb_input.i_next_offset;
430+
return true;
431+
}
432+
433+
this->mb_match_data.md_input = this->mb_input;
434+
this->mb_match_data.md_ovector[0] = this->mb_input.i_offset;
435+
this->mb_match_data.md_ovector[1] = this->mb_input.i_offset;
436+
this->mb_match_data.md_capture_end = 1;
437+
if (rc == PCRE2_ERROR_NOMATCH) {
438+
return false;
439+
}
440+
441+
return false;
442+
}
443+
398444
matcher::matches_result
399445
matcher::matches(uint32_t options)
400446
{
@@ -474,5 +520,4 @@ matcher::error::get_message()
474520
return {(const char*) buffer};
475521
}
476522

477-
} // namespace pcre2pp
478-
} // namespace lnav
523+
} // namespace lnav::pcre2pp

src/pcrepp/pcre2pp.hh

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@
4444
#include "base/result.h"
4545
#include "mapbox/variant.hpp"
4646

47-
namespace lnav {
48-
namespace pcre2pp {
47+
namespace lnav::pcre2pp {
4948

5049
class code;
5150
struct capture_builder;
@@ -171,6 +170,8 @@ public:
171170
return *this;
172171
}
173172

173+
bool found_p(uint32_t options = 0);
174+
174175
matches_result matches(uint32_t options = 0);
175176

176177
int get_next_offset() const { return this->mb_input.i_next_offset; }
@@ -380,7 +381,6 @@ capture_builder::for_each(F func) &&
380381
return Err(eret);
381382
}
382383

383-
} // namespace pcre2pp
384-
} // namespace lnav
384+
} // namespace lnav::pcre2pp
385385

386386
#endif

src/relative_time.cc

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,10 +1002,7 @@ relative_time::window_start(const struct exttm& tm) const
10021002
tv.tv_sec = us / 1000000ULL;
10031003
tv.tv_usec = us % 1000000ULL;
10041004

1005-
retval.et_tm = *gmtime(&tv.tv_sec);
1006-
retval.et_nsec = tv.tv_usec * 1000ULL;
1007-
1008-
return retval;
1005+
return exttm::from_tv(tv);
10091006
}
10101007

10111008
bool clear = false;

src/relative_time.hh

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
#ifndef LNAV_RELATIVE_TIME_HH
3131
#define LNAV_RELATIVE_TIME_HH
3232

33-
#include <sys/time.h>
33+
#include <time.h>
3434

3535
#define __STDC_FORMAT_MACROS
3636
#include <array>
@@ -187,43 +187,39 @@ public:
187187
return true;
188188
}
189189

190-
struct exttm adjust_now() const
190+
exttm adjust_now() const
191191
{
192-
struct exttm tm;
192+
exttm tm;
193193
time_t now;
194194

195195
time(&now);
196-
tm.et_tm = *gmtime(&now);
196+
gmtime_r(&now, &tm.et_tm);
197197
return this->adjust(tm);
198198
}
199199

200-
struct exttm adjust(const struct timeval& tv) const
200+
exttm adjust(const struct timeval& tv) const
201201
{
202-
struct exttm tm;
203-
204-
tm.et_tm = *gmtime(&tv.tv_sec);
205-
tm.et_nsec = tv.tv_usec * 1000;
206-
return this->adjust(tm);
202+
return this->adjust(exttm::from_tv(tv));
207203
}
208204

209-
struct exttm adjust(const struct exttm& tm) const;
205+
exttm adjust(const struct exttm& tm) const;
210206

211-
std::optional<exttm> window_start(const struct exttm& tm) const;
207+
std::optional<exttm> window_start(const exttm& tm) const;
212208

213209
int64_t to_microseconds() const;
214210

215-
void to_timeval(struct timeval& tv_out) const
211+
void to_timeval(timeval& tv_out) const
216212
{
217-
int64_t us = this->to_microseconds();
213+
const auto us = this->to_microseconds();
218214

219215
tv_out.tv_sec = us / (1000 * 1000);
220216
tv_out.tv_usec = us % (1000 * 1000);
221217
}
222218

223-
struct timeval to_timeval() const
219+
timeval to_timeval() const
224220
{
225-
int64_t us = this->to_microseconds();
226-
struct timeval retval;
221+
const auto us = this->to_microseconds();
222+
timeval retval;
227223

228224
retval.tv_sec = us / (1000 * 1000);
229225
retval.tv_usec = us % (1000 * 1000);

src/time-extension-functions.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ timeslice(sqlite3_value* time_in, std::optional<const char*> slice_in_opt)
111111

112112
tv.tv_sec = std::chrono::duration_cast<std::chrono::seconds>(msecs)
113113
.count();
114-
tm.et_tm = *gmtime(&tv.tv_sec);
114+
gmtime_r(&tv.tv_sec, &tm.et_tm);
115115
tm.et_nsec = std::chrono::duration_cast<std::chrono::nanoseconds>(
116116
msecs % 1000)
117117
.count();
@@ -123,7 +123,7 @@ timeslice(sqlite3_value* time_in, std::optional<const char*> slice_in_opt)
123123
auto fract = modf(secs, &integ);
124124

125125
tv.tv_sec = integ;
126-
tm.et_tm = *gmtime(&tv.tv_sec);
126+
gmtime_r(&tv.tv_sec, &tm.et_tm);
127127
tm.et_nsec = floor(fract * 1000000000.0);
128128
break;
129129
}

0 commit comments

Comments
 (0)