Skip to content

Commit c245460

Browse files
committed
[ptimec] trying some optimizations
1 parent a8acf56 commit c245460

File tree

11 files changed

+456
-211
lines changed

11 files changed

+456
-211
lines changed

configure.ac

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ AC_PREFIX_DEFAULT(/usr)
88

99
AC_CANONICAL_HOST
1010
AX_PROG_CC_FOR_BUILD
11+
AX_PROG_CXX_FOR_BUILD
1112

1213
AX_PTHREAD()
1314
LIBS="$PTHREAD_LIBS $LIBS"

m4/ax_prog_cxx_for_build.m4

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# ===========================================================================
2+
# https://www.gnu.org/software/autoconf-archive/ax_prog_cxx_for_build.html
3+
# ===========================================================================
4+
#
5+
# SYNOPSIS
6+
#
7+
# AX_PROG_CXX_FOR_BUILD
8+
#
9+
# DESCRIPTION
10+
#
11+
# This macro searches for a C++ compiler that generates native
12+
# executables, that is a C++ compiler that surely is not a cross-compiler.
13+
# This can be useful if you have to generate source code at compile-time
14+
# like for example GCC does.
15+
#
16+
# The macro sets the CXX_FOR_BUILD and CXXCPP_FOR_BUILD macros to anything
17+
# needed to compile or link (CXX_FOR_BUILD) and preprocess
18+
# (CXXCPP_FOR_BUILD). The value of these variables can be overridden by
19+
# the user by specifying a compiler with an environment variable (like you
20+
# do for standard CXX).
21+
#
22+
# LICENSE
23+
#
24+
# Copyright (c) 2008 Paolo Bonzini <bonzini@gnu.org>
25+
# Copyright (c) 2012 Avionic Design GmbH
26+
#
27+
# Based on the AX_PROG_CC_FOR_BUILD macro by Paolo Bonzini.
28+
#
29+
# Copying and distribution of this file, with or without modification, are
30+
# permitted in any medium without royalty provided the copyright notice
31+
# and this notice are preserved. This file is offered as-is, without any
32+
# warranty.
33+
34+
#serial 5
35+
36+
AU_ALIAS([AC_PROG_CXX_FOR_BUILD], [AX_PROG_CXX_FOR_BUILD])
37+
AC_DEFUN([AX_PROG_CXX_FOR_BUILD], [dnl
38+
AC_REQUIRE([AX_PROG_CC_FOR_BUILD])dnl
39+
AC_REQUIRE([AC_PROG_CXX])dnl
40+
AC_REQUIRE([AC_PROG_CXXCPP])dnl
41+
AC_REQUIRE([AC_CANONICAL_HOST])dnl
42+
43+
dnl Use the standard macros, but make them use other variable names
44+
dnl
45+
pushdef([ac_cv_prog_CXXCPP], ac_cv_build_prog_CXXCPP)dnl
46+
pushdef([ac_cv_prog_gxx], ac_cv_build_prog_gxx)dnl
47+
pushdef([ac_cv_prog_cxx_works], ac_cv_build_prog_cxx_works)dnl
48+
pushdef([ac_cv_prog_cxx_cross], ac_cv_build_prog_cxx_cross)dnl
49+
pushdef([ac_cv_prog_cxx_g], ac_cv_build_prog_cxx_g)dnl
50+
pushdef([CXX], CXX_FOR_BUILD)dnl
51+
pushdef([CXXCPP], CXXCPP_FOR_BUILD)dnl
52+
pushdef([GXX], GXX_FOR_BUILD)dnl
53+
pushdef([CXXFLAGS], CXXFLAGS_FOR_BUILD)dnl
54+
pushdef([CPPFLAGS], CPPFLAGS_FOR_BUILD)dnl
55+
pushdef([CXXCPPFLAGS], CXXCPPFLAGS_FOR_BUILD)dnl
56+
pushdef([host], build)dnl
57+
pushdef([host_alias], build_alias)dnl
58+
pushdef([host_cpu], build_cpu)dnl
59+
pushdef([host_vendor], build_vendor)dnl
60+
pushdef([host_os], build_os)dnl
61+
pushdef([ac_cv_host], ac_cv_build)dnl
62+
pushdef([ac_cv_host_alias], ac_cv_build_alias)dnl
63+
pushdef([ac_cv_host_cpu], ac_cv_build_cpu)dnl
64+
pushdef([ac_cv_host_vendor], ac_cv_build_vendor)dnl
65+
pushdef([ac_cv_host_os], ac_cv_build_os)dnl
66+
pushdef([ac_tool_prefix], ac_build_tool_prefix)dnl
67+
pushdef([am_cv_CXX_dependencies_compiler_type], am_cv_build_CXX_dependencies_compiler_type)dnl
68+
pushdef([cross_compiling], cross_compiling_build)dnl
69+
70+
cross_compiling_build=no
71+
72+
ac_build_tool_prefix=
73+
AS_IF([test -n "$build"], [ac_build_tool_prefix="$build-"],
74+
[test -n "$build_alias"],[ac_build_tool_prefix="$build_alias-"])
75+
76+
AC_LANG_PUSH([C++])
77+
AC_PROG_CXX
78+
AC_PROG_CXXCPP
79+
80+
dnl Restore the old definitions
81+
dnl
82+
popdef([cross_compiling])dnl
83+
popdef([am_cv_CXX_dependencies_compiler_type])dnl
84+
popdef([ac_tool_prefix])dnl
85+
popdef([ac_cv_host_os])dnl
86+
popdef([ac_cv_host_vendor])dnl
87+
popdef([ac_cv_host_cpu])dnl
88+
popdef([ac_cv_host_alias])dnl
89+
popdef([ac_cv_host])dnl
90+
popdef([host_os])dnl
91+
popdef([host_vendor])dnl
92+
popdef([host_cpu])dnl
93+
popdef([host_alias])dnl
94+
popdef([host])dnl
95+
popdef([CXXCPPFLAGS])dnl
96+
popdef([CPPFLAGS])dnl
97+
popdef([CXXFLAGS])dnl
98+
popdef([CXXCPP])dnl
99+
popdef([CXX])dnl
100+
popdef([ac_cv_prog_cxx_g])dnl
101+
popdef([ac_cv_prog_cxx_cross])dnl
102+
popdef([ac_cv_prog_cxx_works])dnl
103+
popdef([ac_cv_prog_gxx])dnl
104+
popdef([ac_cv_prog_CXXCPP])dnl
105+
106+
dnl restore global variables (dependent on the current
107+
dnl language after popping):
108+
AC_LANG_POP([C++])
109+
110+
dnl Finally, set Makefile variables
111+
dnl
112+
AC_SUBST([CXXFLAGS_FOR_BUILD])dnl
113+
AC_SUBST([CXXCPPFLAGS_FOR_BUILD])dnl
114+
])

src/CMakeLists.txt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ add_subdirectory(yajlpp)
2626
add_executable(bin2c bin2c.hh ../tools/bin2c.c)
2727
target_link_libraries(bin2c ZLIB::ZLIB)
2828

29-
add_executable(ptimec ptimec.hh ptimec.c)
29+
add_executable(ptimec ptimec.hh ptimec.cc)
3030

3131
set(TIME_FORMATS
3232
"@%@"
@@ -133,8 +133,10 @@ set(TIME_FORMATS
133133

134134
set(GEN_SRCS "")
135135

136-
add_custom_command(OUTPUT time_fmts.cc COMMAND ptimec ${TIME_FORMATS} >
137-
time_fmts.cc)
136+
add_custom_command(
137+
OUTPUT time_fmts.cc
138+
DEPENDS ptimec
139+
COMMAND ptimec ${TIME_FORMATS} > time_fmts.cc)
138140

139141
add_library(lnavdt STATIC config.h.in ptimec.hh ptimec_rt.cc time_fmts.cc)
140142
target_include_directories(lnavdt PUBLIC . ${CMAKE_CURRENT_BINARY_DIR} third-party/date/include)
@@ -334,7 +336,10 @@ add_library(
334336
third-party/scnlib/include/scn/istream.h
335337
third-party/scnlib/include/scn/scan.h
336338
)
337-
target_include_directories(cppscnlib PRIVATE third-party/scnlib/src/deps/fast_float/single_include)
339+
target_include_directories(
340+
cppscnlib PRIVATE
341+
third-party/scnlib/src
342+
third-party/scnlib/src/deps/fast_float/single_include)
338343
target_include_directories(cppscnlib PUBLIC third-party/scnlib/include)
339344

340345
add_library(

src/Makefile.am

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ builtin-sh-scripts.cc: $(BIN2C_PATH) $(BUILTIN_SHSCRIPTS)
9090

9191
include time_formats.am
9292

93-
time_fmts.cc: ptimec$(BUILD_EXEEXT)
93+
time_fmts.cc: ptimec$(BUILD_EXEEXT) time_formats.am
9494
$(PTIME_V)./ptimec$(BUILD_EXEEXT) $(TIME_FORMATS) > $@
9595

9696
if HAVE_RE2C
@@ -556,11 +556,11 @@ lnav_test_SOURCES = \
556556
$(PLUGIN_SRCS)
557557

558558
if CROSS_COMPILING
559-
ptimec$(BUILD_EXEEXT): ptimec.c
560-
$(AM_V_CC) $(CC_FOR_BUILD) $(CPPFLAGS_FOR_BUILD) $(LDFLAGS_FOR_BUILD) -g3 -o $@ $? -fPIE
559+
ptimec$(BUILD_EXEEXT): ptimec.cc
560+
$(AM_V_CXX) $(CXX_FOR_BUILD) $(CPPFLAGS_FOR_BUILD) $(LDFLAGS_FOR_BUILD) -g3 -o $@ $? -fPIE
561561
else
562-
ptimec$(BUILD_EXEEXT): ptimec.c
563-
$(AM_V_CC) $(CC) $(CPPFLAGS) $(LDFLAGS) -g3 -o $@ $? -fPIE
562+
ptimec$(BUILD_EXEEXT): ptimec.cc
563+
$(AM_V_CXX) $(CXX) $(CPPFLAGS) $(LDFLAGS) -g3 -o $@ $? -fPIE
564564
endif
565565

566566
if HAVE_RE2C
@@ -572,7 +572,7 @@ EXTRA_DIST = \
572572
third-party/prqlc-c/Cargo.toml \
573573
third-party/prqlc-c/cbindgen.toml \
574574
third-party/prqlc-c/src/lib.rs \
575-
ptimec.c
575+
ptimec.cc
576576

577577
CLEANFILES = \
578578
ptimec$(BUILD_EXEEXT)

src/base/is_utf8.cc

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,6 @@ is_utf8(string_fragment str, std::optional<unsigned char> terminator)
6767
ssize_t i = 0, valid_end = 0;
6868

6969
while (i < str.length()) {
70-
if (ustr[i] == '\x1b') {
71-
retval.usr_has_ansi = true;
72-
}
73-
7470
if (terminator && ustr[i] == terminator.value()) {
7571
retval.usr_remaining = str.substr(i + 1);
7672
break;
@@ -86,6 +82,8 @@ is_utf8(string_fragment str, std::optional<unsigned char> terminator)
8682
if (ustr[i] <= 0x7F) /* 00..7F */ {
8783
if (ustr[i] == '\t') {
8884
retval.usr_column_width_guess += 7;
85+
} else if (ustr[i] == '\x1b') {
86+
retval.usr_has_ansi = true;
8987
}
9088
i += 1;
9189
} else if (ustr[i] >= 0xC2 && ustr[i] <= 0xDF) /* C2..DF 80..BF */ {

src/line_buffer.cc

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@ class lock_hack {
135135

136136
#define Z_BUFSIZE 65536U
137137
#define SYNCPOINT_SIZE (1024 * 1024)
138-
line_buffer::gz_indexed::
139-
gz_indexed()
138+
line_buffer::gz_indexed::gz_indexed()
140139
{
141140
if ((this->inbuf = auto_mem<Bytef>::malloc(Z_BUFSIZE)) == nullptr) {
142141
throw std::bad_alloc();
@@ -368,14 +367,12 @@ line_buffer::gz_indexed::read(void* buf, size_t offset, size_t size)
368367
return bytes;
369368
}
370369

371-
line_buffer::
372-
line_buffer()
370+
line_buffer::line_buffer()
373371
{
374372
ensure(this->invariant());
375373
}
376374

377-
line_buffer::~
378-
line_buffer()
375+
line_buffer::~line_buffer()
379376
{
380377
if (this->lb_loader_future.valid()) {
381378
this->lb_loader_future.wait();
@@ -804,6 +801,8 @@ line_buffer::fill_range(file_off_t start, ssize_t max_length)
804801
this->lb_line_has_ansi = std::move(this->lb_alt_line_has_ansi);
805802
this->lb_alt_line_has_ansi.clear();
806803
this->lb_stats.s_used_preloads += 1;
804+
this->lb_next_line_start_index = 0;
805+
this->lb_next_buffer_offset = 0;
807806
}
808807
if (this->in_range(start)
809808
&& (max_length == 0 || this->in_range(start + max_length - 1)))
@@ -1114,22 +1113,43 @@ line_buffer::load_next_line(file_range prev_line)
11141113
if (!this->lb_line_starts.empty()) {
11151114
auto buffer_offset = offset - this->lb_file_offset;
11161115

1117-
auto start_iter = std::lower_bound(this->lb_line_starts.begin(),
1118-
this->lb_line_starts.end(),
1119-
buffer_offset);
1120-
if (start_iter != this->lb_line_starts.end()) {
1116+
if (this->lb_next_buffer_offset == buffer_offset) {
1117+
auto start_iter = this->lb_line_starts.begin()
1118+
+ this->lb_next_line_start_index;
11211119
auto next_line_iter = start_iter + 1;
1122-
1123-
// log_debug("found offset %d %d", buffer_offset, *start_iter);
11241120
if (next_line_iter != this->lb_line_starts.end()) {
11251121
utf8_end = *next_line_iter - 1 - *start_iter;
11261122
found_in_cache = true;
11271123
lf = line_start + utf8_end;
1124+
1125+
this->lb_next_buffer_offset = *next_line_iter;
1126+
this->lb_next_line_start_index += 1;
11281127
} else {
11291128
// log_debug("no next iter");
11301129
}
11311130
} else {
1132-
// log_debug("no buffer_offset found");
1131+
auto start_iter = std::lower_bound(this->lb_line_starts.begin(),
1132+
this->lb_line_starts.end(),
1133+
buffer_offset);
1134+
if (start_iter != this->lb_line_starts.end()) {
1135+
auto next_line_iter = start_iter + 1;
1136+
1137+
// log_debug("found offset %d %d", buffer_offset,
1138+
// *start_iter);
1139+
if (next_line_iter != this->lb_line_starts.end()) {
1140+
utf8_end = *next_line_iter - 1 - *start_iter;
1141+
found_in_cache = true;
1142+
lf = line_start + utf8_end;
1143+
1144+
this->lb_next_line_start_index = std::distance(
1145+
this->lb_alt_line_starts.begin(), next_line_iter);
1146+
this->lb_next_buffer_offset = *next_line_iter;
1147+
} else {
1148+
// log_debug("no next iter");
1149+
}
1150+
} else {
1151+
// log_debug("no buffer_offset found");
1152+
}
11331153
}
11341154
}
11351155

@@ -1241,8 +1261,7 @@ line_buffer::load_next_line(file_range prev_line)
12411261
(size_t) retval.li_file_range.fr_size,
12421262
};
12431263

1244-
auto scan_res = scn::scan<int64_t, int64_t, char>(sv,
1245-
"{}.{}:{};");
1264+
auto scan_res = scn::scan<int64_t, int64_t, char>(sv, "{}.{}:{};");
12461265
if (scan_res) {
12471266
auto& [tv_sec, tv_usec, level] = scan_res->values();
12481267
retval.li_timestamp.tv_sec = tv_sec;
@@ -1317,8 +1336,8 @@ line_buffer::get_available()
13171336
static_cast<file_ssize_t>(this->lb_buffer.size())};
13181337
}
13191338

1320-
line_buffer::gz_indexed::indexDict::
1321-
indexDict(const z_stream& s, const file_size_t size)
1339+
line_buffer::gz_indexed::indexDict::indexDict(const z_stream& s,
1340+
const file_size_t size)
13221341
{
13231342
assert((s.data_type & GZ_END_OF_BLOCK_MASK));
13241343
assert(!(s.data_type & GZ_END_OF_FILE_MASK));

src/line_buffer.hh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,8 @@ private:
371371
file_off_t lb_last_line_offset{-1}; /*< */
372372

373373
std::vector<uint32_t> lb_line_starts;
374+
file_off_t lb_next_buffer_offset{0};
375+
size_t lb_next_line_start_index{0};
374376
std::vector<bool> lb_line_is_utf;
375377
std::vector<bool> lb_line_has_ansi;
376378
stats lb_stats;

src/log_format.cc

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,7 @@ std::optional<std::string>
228228
log_format::opid_descriptor::matches(const string_fragment& sf) const
229229
{
230230
if (this->od_extractor.pp_value) {
231-
static thread_local auto desc_md
232-
= lnav::pcre2pp::match_data::unitialized();
231+
thread_local auto desc_md = lnav::pcre2pp::match_data::unitialized();
233232

234233
auto desc_match_res = this->od_extractor.pp_value->capture_from(sf)
235234
.into(desc_md)
@@ -684,7 +683,7 @@ log_format::log_scanf(uint32_t line_number,
684683
int pat_index = this->last_pattern_index();
685684

686685
while (!done && next_format(fmt, curr_fmt, pat_index)) {
687-
static thread_local auto md = lnav::pcre2pp::match_data::unitialized();
686+
thread_local auto md = lnav::pcre2pp::match_data::unitialized();
688687

689688
auto match_res = fmt[curr_fmt]
690689
.pcre->capture_from(line)
@@ -1387,10 +1386,9 @@ external_log_format::scan(logfile& lf,
13871386
int curr_fmt = -1, orig_lock = this->last_pattern_index();
13881387
int pat_index = orig_lock;
13891388
auto line_sf = sbr.to_string_fragment();
1389+
thread_local auto md = lnav::pcre2pp::match_data::unitialized();
13901390

13911391
while (::next_format(this->elf_pattern_order, curr_fmt, pat_index)) {
1392-
thread_local auto md = lnav::pcre2pp::match_data::unitialized();
1393-
13941392
auto* fpat = this->elf_pattern_order[curr_fmt].get();
13951393
auto* pat = fpat->p_pcre.pp_value.get();
13961394

@@ -1631,8 +1629,8 @@ external_log_format::scan(logfile& lf,
16311629
std::optional<double> dvalue_opt;
16321630
switch (vd.vd_meta.lvm_kind) {
16331631
case value_kind_t::VALUE_INTEGER: {
1634-
auto scan_res = scn::scan_int<int64_t>(
1635-
num_cap->to_string_view());
1632+
auto scan_res
1633+
= scn::scan_int<int64_t>(num_cap->to_string_view());
16361634
if (scan_res) {
16371635
dvalue_opt = scan_res->value();
16381636
}

0 commit comments

Comments
 (0)