Skip to content

Commit 82ba44f

Browse files
committed
[perf] move small string map tweaks
1 parent 351d14c commit 82ba44f

3 files changed

Lines changed: 12 additions & 52 deletions

File tree

src/base/small_string_map.cc

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
namespace lnav {
3333

3434
std::optional<uint32_t>
35-
small_string_map::lookup(const string_fragment& in) const
35+
small_string_map::lookup(const string_fragment& in)
3636
{
3737
if (in.length() > MAX_KEY_SIZE) {
3838
return std::nullopt;
@@ -41,26 +41,16 @@ small_string_map::lookup(const string_fragment& in) const
4141
alignas(8) char in_key[MAX_KEY_SIZE]{};
4242
memcpy(in_key, in.data(), in.length());
4343

44+
auto index = this->ssm_start_index;
4445
for (int lpc = 0; lpc < MAP_SIZE; ++lpc) {
45-
auto match = true;
46-
#if 0
47-
for (int index = 0; index < MAX_KEY_SIZE; ++index) {
48-
if (this->ssm_keys[lpc * MAX_KEY_SIZE + index] != in_key[index]) {
49-
match = false;
50-
}
51-
}
52-
#else
53-
if (memcmp(&this->ssm_keys[lpc * MAX_KEY_SIZE], in_key, MAX_KEY_SIZE)
54-
!= 0)
46+
if (memcmp(&this->ssm_keys[index * MAX_KEY_SIZE], in_key, MAX_KEY_SIZE)
47+
== 0)
5548
{
56-
match = false;
57-
}
58-
#endif
59-
if (match) {
60-
return this->ssm_values[lpc];
49+
this->ssm_start_index = index;
50+
return index;
6151
}
52+
index = (index + 1) % MAP_SIZE;
6253
}
63-
6454
return std::nullopt;
6555
}
6656

src/base/small_string_map.hh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ struct small_string_map {
4343
char ssm_keys[MAP_SIZE * MAX_KEY_SIZE]{};
4444
uint32_t ssm_values[MAP_SIZE]{};
4545
uint32_t ssm_used_keys{0};
46+
uint32_t ssm_start_index{0};
4647

47-
std::optional<uint32_t> lookup(const string_fragment& in) const;
48+
std::optional<uint32_t> lookup(const string_fragment& in);
4849
void insert(const string_fragment& key, uint32_t value);
4950
};
5051

src/log_format.cc

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4494,33 +4494,12 @@ external_log_format::convert_level(string_fragment sf,
44944494
auto retval = LEVEL_INFO;
44954495

44964496
if (sf.is_valid()) {
4497-
// std::optional<uint32_t> ssm_res;
44984497
if (sbc != nullptr) {
44994498
auto ssm_res = sbc->sbc_level_cache.lookup(sf);
45004499
if (ssm_res.has_value()) {
45014500
return static_cast<log_level_t>(ssm_res.value());
45024501
}
45034502
}
4504-
#if 0
4505-
if (sbc != nullptr && sbc->sbc_cached_level_count > 0) {
4506-
const auto level_end = std::begin(sbc->sbc_cached_level_strings)
4507-
+ sbc->sbc_cached_level_count;
4508-
const auto cached_level_iter = std::find(
4509-
std::begin(sbc->sbc_cached_level_strings), level_end, sf);
4510-
if (cached_level_iter != level_end) {
4511-
const auto cache_index
4512-
= std::distance(std::begin(sbc->sbc_cached_level_strings),
4513-
cached_level_iter);
4514-
if (cache_index != 0) {
4515-
std::swap(sbc->sbc_cached_level_strings[cache_index],
4516-
sbc->sbc_cached_level_strings[0]);
4517-
std::swap(sbc->sbc_cached_level_values[cache_index],
4518-
sbc->sbc_cached_level_values[0]);
4519-
}
4520-
return sbc->sbc_cached_level_values[0];
4521-
}
4522-
}
4523-
#endif
45244503

45254504
if (this->elf_level_patterns.empty()) {
45264505
retval = string2level(sf.data(), sf.length());
@@ -4537,19 +4516,9 @@ external_log_format::convert_level(string_fragment sf,
45374516
}
45384517
}
45394518

4540-
if (sbc != nullptr && sf.length() < 8) {
4541-
#if 0
4542-
size_t cache_index;
4543-
4544-
if (sbc->sbc_cached_level_count == 4) {
4545-
cache_index = sbc->sbc_cached_level_count - 1;
4546-
} else {
4547-
cache_index = sbc->sbc_cached_level_count;
4548-
sbc->sbc_cached_level_count += 1;
4549-
}
4550-
sbc->sbc_cached_level_strings[cache_index] = sf.to_string();
4551-
sbc->sbc_cached_level_values[cache_index] = retval;
4552-
#endif
4519+
if (sbc != nullptr
4520+
&& sf.length() <= lnav::small_string_map::MAX_KEY_SIZE)
4521+
{
45534522
sbc->sbc_level_cache.insert(sf, retval);
45544523
}
45554524
}

0 commit comments

Comments
 (0)