Deduplicate static strings

This commit is contained in:
Benoit Blanchon
2025-02-26 10:26:04 +01:00
parent 08b2400592
commit ee144b89a2
5 changed files with 61 additions and 3 deletions

View File

@ -382,6 +382,24 @@ TEST_CASE("Deduplicate keys") {
Allocate(sizeofString("example")),
});
}
SECTION("string literals") {
doc[0]["example"] = 1;
doc[1]["example"] = 2;
doc.shrinkToFit();
const char* key1 = doc[0].as<JsonObject>().begin()->key().c_str();
const char* key2 = doc[1].as<JsonObject>().begin()->key().c_str();
CHECK(key1 == key2);
REQUIRE(spy.log() ==
AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofStaticStringPool()),
Reallocate(sizeofPool(), sizeofPool(6)),
Reallocate(sizeofStaticStringPool(), sizeofStaticStringPool(1)),
});
}
}
TEST_CASE("MemberProxy under memory constraints") {

View File

@ -19,6 +19,25 @@ static StringNode* saveString(ResourceManager& resources, const char* s,
return resources.saveString(adaptString(s, n));
}
TEST_CASE("ResourceManager::saveStaticString()") {
SpyingAllocator spy;
ResourceManager resources(&spy);
auto a = resources.saveStaticString("hello");
auto b = resources.saveStaticString("world");
REQUIRE(a != b);
auto c = resources.saveStaticString("hello");
REQUIRE(a == c);
resources.shrinkToFit();
REQUIRE(spy.log() ==
AllocatorLog{
Allocate(sizeofStaticStringPool()),
Reallocate(sizeofStaticStringPool(), sizeofStaticStringPool(2)),
});
}
TEST_CASE("ResourceManager::saveString()") {
ResourceManager resources;

View File

@ -81,6 +81,14 @@ class MemoryPool {
return slots_ + id;
}
SlotId find(const T& value) const {
for (SlotId i = 0; i < usage_; i++) {
if (slots_[i] == value)
return i;
}
return NULL_SLOT;
}
void clear() {
usage_ = 0;
}

View File

@ -114,6 +114,15 @@ class MemoryPoolList {
return pools_[poolIndex].getSlot(indexInPool);
}
SlotId find(const T& value) const {
for (PoolCount i = 0; i < count_; i++) {
SlotId id = pools_[i].find(value);
if (id != NULL_SLOT)
return SlotId(i * ARDUINOJSON_POOL_CAPACITY + id);
}
return NULL_SLOT;
}
void clear(Allocator* allocator) {
for (PoolCount i = 0; i < count_; i++)
pools_[i].destroy(allocator);

View File

@ -114,10 +114,14 @@ class ResourceManager {
}
SlotId saveStaticString(const char* s) {
auto existingSlotId = staticStringsPools_.find(s);
if (existingSlotId != NULL_SLOT)
return existingSlotId;
auto slot = staticStringsPools_.allocSlot(allocator_);
if (!slot)
return NULL_SLOT;
*slot = s;
if (slot)
*slot = s;
return slot.id();
}