Deduplicate static strings

This commit is contained in:
Benoit Blanchon
2025-02-27 13:42:04 +01:00
parent 08b2400592
commit ee144b89a2
5 changed files with 61 additions and 3 deletions
+18
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") {
@@ -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;