Skip to content

Commit 4cd7112

Browse files
committed
[perf] try to improve small_string_map eviction
1 parent 24fe5cf commit 4cd7112

3 files changed

Lines changed: 21 additions & 7 deletions

File tree

src/base/small_string_map.cc

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929

3030
#include "small_string_map.hh"
3131

32+
#include "lnav_log.hh"
33+
3234
namespace lnav {
3335

3436
std::optional<uint32_t>
@@ -47,6 +49,7 @@ small_string_map::lookup(const string_fragment& in)
4749
|| this->ssm_keys[index * MAX_KEY_SIZE + in.length()] == '\0'))
4850
{
4951
this->ssm_start_index = index;
52+
this->ssm_age[index] = true;
5053
return this->ssm_values[index];
5154
}
5255
index = (index + 1) % MAP_SIZE;
@@ -57,18 +60,27 @@ small_string_map::lookup(const string_fragment& in)
5760
void
5861
small_string_map::insert(const string_fragment& key, uint32_t value)
5962
{
60-
if (key.length() >= MAX_KEY_SIZE) {
63+
if (key.empty() || key.length() > MAX_KEY_SIZE) {
6164
return;
6265
}
6366

64-
auto key_index = this->ssm_used_keys < MAP_SIZE ? this->ssm_used_keys++
65-
: key.hash() % MAP_SIZE;
67+
auto key_index = (this->ssm_start_index + 1) % MAP_SIZE;
68+
for (auto lpc = 0; lpc < MAP_SIZE; lpc++) {
69+
if (!this->ssm_age[lpc]) {
70+
key_index = lpc;
71+
} else {
72+
this->ssm_age[lpc] = false;
73+
}
74+
}
75+
this->ssm_age[this->ssm_start_index] = true;
76+
this->ssm_age[key_index] = true;
6677

6778
memset(&this->ssm_keys[key_index * MAX_KEY_SIZE],
6879
0,
6980
MAX_KEY_SIZE * sizeof(char));
7081
memcpy(&this->ssm_keys[key_index * MAX_KEY_SIZE], key.data(), key.length());
7182
this->ssm_values[key_index] = value;
83+
this->ssm_start_index = key_index;
7284
}
7385

7486
} // namespace lnav

src/base/small_string_map.hh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@
3838
namespace lnav {
3939

4040
struct small_string_map {
41-
static constexpr auto MAP_SIZE = 4;
41+
static constexpr auto MAP_SIZE = 8;
4242
static constexpr auto MAX_KEY_SIZE = 8;
4343
char ssm_keys[MAP_SIZE * MAX_KEY_SIZE]{};
4444
uint32_t ssm_values[MAP_SIZE]{};
45-
uint32_t ssm_used_keys{0};
4645
uint32_t ssm_start_index{0};
46+
bool ssm_age[MAP_SIZE];
4747

4848
std::optional<uint32_t> lookup(const string_fragment& in);
4949
void insert(const string_fragment& key, uint32_t value);

src/base/small_string_map.tests.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ TEST_CASE("basic lookup")
4545
{
4646
auto ssm = lnav::small_string_map{};
4747

48-
ssm.insert("test"_frag, 123);
49-
auto res = ssm.lookup("test"_frag);
48+
ssm.insert("info"_frag, 123);
49+
ssm.insert("304"_frag, 123);
50+
ssm.insert("404"_frag, 123);
51+
auto res = ssm.lookup("info"_frag);
5052
CHECK(res.has_value());
5153
CHECK(123 == res.value());
5254

0 commit comments

Comments
 (0)