tweak db performance

- leveldb cache pinned to spiram
 - actually use it during indexing lol
 - all up, saves about 10ms per file (amortised) for an incremental reindex
custom
jacqueline 1 year ago
parent 313c021c3b
commit 1e278d55c4
  1. 21
      lib/leveldb/util/cache.cc
  2. 8
      src/database/database.cpp

@ -8,6 +8,7 @@
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include "esp_heap_caps.h"
#include "port/port.h" #include "port/port.h"
#include "port/thread_annotations.h" #include "port/thread_annotations.h"
#include "util/hash.h" #include "util/hash.h"
@ -157,7 +158,9 @@ class LRUCache {
void SetCapacity(size_t capacity) { capacity_ = capacity; } void SetCapacity(size_t capacity) { capacity_ = capacity; }
// Like Cache methods, but with an extra "hash" parameter. // Like Cache methods, but with an extra "hash" parameter.
Cache::Handle* Insert(const Slice& key, uint32_t hash, void* value, Cache::Handle* Insert(const Slice& key,
uint32_t hash,
void* value,
size_t charge, size_t charge,
void (*deleter)(const Slice& key, void* value)); void (*deleter)(const Slice& key, void* value));
Cache::Handle* Lookup(const Slice& key, uint32_t hash); Cache::Handle* Lookup(const Slice& key, uint32_t hash);
@ -264,14 +267,16 @@ void LRUCache::Release(Cache::Handle* handle) {
Unref(reinterpret_cast<LRUHandle*>(handle)); Unref(reinterpret_cast<LRUHandle*>(handle));
} }
Cache::Handle* LRUCache::Insert(const Slice& key, uint32_t hash, void* value, Cache::Handle* LRUCache::Insert(const Slice& key,
uint32_t hash,
void* value,
size_t charge, size_t charge,
void (*deleter)(const Slice& key, void (*deleter)(const Slice& key,
void* value)) { void* value)) {
MutexLock l(&mutex_); MutexLock l(&mutex_);
LRUHandle* e = LRUHandle* e = reinterpret_cast<LRUHandle*>(
reinterpret_cast<LRUHandle*>(malloc(sizeof(LRUHandle) - 1 + key.size())); heap_caps_malloc(sizeof(LRUHandle) - 1 + key.size(), MALLOC_CAP_SPIRAM));
e->value = value; e->value = value;
e->deleter = deleter; e->deleter = deleter;
e->charge = charge; e->charge = charge;
@ -356,7 +361,9 @@ class ShardedLRUCache : public Cache {
} }
} }
~ShardedLRUCache() override {} ~ShardedLRUCache() override {}
Handle* Insert(const Slice& key, void* value, size_t charge, Handle* Insert(const Slice& key,
void* value,
size_t charge,
void (*deleter)(const Slice& key, void* value)) override { void (*deleter)(const Slice& key, void* value)) override {
const uint32_t hash = HashSlice(key); const uint32_t hash = HashSlice(key);
return shard_[Shard(hash)].Insert(key, hash, value, charge, deleter); return shard_[Shard(hash)].Insert(key, hash, value, charge, deleter);
@ -396,6 +403,8 @@ class ShardedLRUCache : public Cache {
} // end anonymous namespace } // end anonymous namespace
Cache* NewLRUCache(size_t capacity) { return new ShardedLRUCache(capacity); } Cache* NewLRUCache(size_t capacity) {
return new ShardedLRUCache(capacity);
}
} // namespace leveldb } // namespace leveldb

@ -139,14 +139,14 @@ auto Database::Open(IFileGatherer& gatherer,
[&]() -> cpp::result<Database*, DatabaseError> { [&]() -> cpp::result<Database*, DatabaseError> {
leveldb::DB* db; leveldb::DB* db;
std::unique_ptr<leveldb::Cache> cache{ std::unique_ptr<leveldb::Cache> cache{
leveldb::NewLRUCache(4 * 1024)}; leveldb::NewLRUCache(256 * 1024)};
leveldb::Options options; leveldb::Options options;
options.env = sEnv.env(); options.env = sEnv.env();
options.write_buffer_size = 4 * 1024; options.write_buffer_size = 4 * 1024;
options.max_file_size = 32; options.max_file_size = 16 * 1024;
options.block_cache = cache.get(); options.block_cache = cache.get();
options.block_size = 512; options.block_size = 2048;
auto status = leveldb::DB::Open(options, kDbPath, &db); auto status = leveldb::DB::Open(options, kDbPath, &db);
if (!status.ok()) { if (!status.ok()) {
@ -299,7 +299,7 @@ auto Database::updateIndexes() -> void {
UpdateNotifier notifier{is_updating_}; UpdateNotifier notifier{is_updating_};
leveldb::ReadOptions read_options; leveldb::ReadOptions read_options;
read_options.fill_cache = false; read_options.fill_cache = true;
// Stage 1: verify all existing tracks are still valid. // Stage 1: verify all existing tracks are still valid.
ESP_LOGI(kTag, "verifying existing tracks"); ESP_LOGI(kTag, "verifying existing tracks");

Loading…
Cancel
Save