forked from bblanchon/ArduinoJson
Optimize storage of tiny strings (up to 3 characters)
This commit is contained in:
@ -6,8 +6,8 @@
|
||||
#include <catch.hpp>
|
||||
|
||||
#include "Allocators.hpp"
|
||||
#include "Literals.hpp"
|
||||
|
||||
using namespace ArduinoJson;
|
||||
using namespace ArduinoJson::detail;
|
||||
|
||||
TEST_CASE("StringBuilder") {
|
||||
@ -22,13 +22,31 @@ TEST_CASE("StringBuilder") {
|
||||
str.startString();
|
||||
str.save(&data);
|
||||
|
||||
REQUIRE(resources.size() == sizeofString(""));
|
||||
REQUIRE(resources.overflowed() == false);
|
||||
REQUIRE(spyingAllocator.log() ==
|
||||
AllocatorLog{
|
||||
Allocate(sizeofStringBuffer()),
|
||||
Reallocate(sizeofStringBuffer(), sizeofString("")),
|
||||
});
|
||||
REQUIRE(spyingAllocator.log() == AllocatorLog{
|
||||
Allocate(sizeofStringBuffer()),
|
||||
});
|
||||
REQUIRE(data.type() == VariantType::TinyString);
|
||||
}
|
||||
|
||||
SECTION("Tiny string") {
|
||||
StringBuilder str(&resources);
|
||||
|
||||
str.startString();
|
||||
str.append("url");
|
||||
|
||||
REQUIRE(str.isValid() == true);
|
||||
REQUIRE(str.str() == "url");
|
||||
REQUIRE(spyingAllocator.log() == AllocatorLog{
|
||||
Allocate(sizeofStringBuffer()),
|
||||
});
|
||||
|
||||
VariantData data;
|
||||
str.save(&data);
|
||||
|
||||
REQUIRE(resources.overflowed() == false);
|
||||
REQUIRE(data.type() == VariantType::TinyString);
|
||||
REQUIRE(data.asString() == "url");
|
||||
}
|
||||
|
||||
SECTION("Short string fits in first allocation") {
|
||||
@ -98,12 +116,12 @@ TEST_CASE("StringBuilder") {
|
||||
}
|
||||
}
|
||||
|
||||
static const char* saveString(StringBuilder& builder, const char* s) {
|
||||
static JsonString saveString(StringBuilder& builder, const char* s) {
|
||||
VariantData data;
|
||||
builder.startString();
|
||||
builder.append(s);
|
||||
builder.save(&data);
|
||||
return data.asString().c_str();
|
||||
return data.asString();
|
||||
}
|
||||
|
||||
TEST_CASE("StringBuilder::save() deduplicates strings") {
|
||||
@ -116,9 +134,9 @@ TEST_CASE("StringBuilder::save() deduplicates strings") {
|
||||
auto s2 = saveString(builder, "world");
|
||||
auto s3 = saveString(builder, "hello");
|
||||
|
||||
REQUIRE(s1 == "hello"_s);
|
||||
REQUIRE(s2 == "world"_s);
|
||||
REQUIRE(+s1 == +s3); // same address
|
||||
REQUIRE(s1 == "hello");
|
||||
REQUIRE(s2 == "world");
|
||||
REQUIRE(+s1.c_str() == +s3.c_str()); // same address
|
||||
|
||||
REQUIRE(spy.log() ==
|
||||
AllocatorLog{
|
||||
@ -134,9 +152,9 @@ TEST_CASE("StringBuilder::save() deduplicates strings") {
|
||||
auto s1 = saveString(builder, "hello world");
|
||||
auto s2 = saveString(builder, "hello");
|
||||
|
||||
REQUIRE(s1 == "hello world"_s);
|
||||
REQUIRE(s2 == "hello"_s);
|
||||
REQUIRE(+s2 != +s1); // different address
|
||||
REQUIRE(s1 == "hello world");
|
||||
REQUIRE(s2 == "hello");
|
||||
REQUIRE(+s2.c_str() != +s1.c_str()); // different address
|
||||
|
||||
REQUIRE(spy.log() ==
|
||||
AllocatorLog{
|
||||
@ -149,18 +167,18 @@ TEST_CASE("StringBuilder::save() deduplicates strings") {
|
||||
|
||||
SECTION("Don't overrun") {
|
||||
auto s1 = saveString(builder, "hello world");
|
||||
auto s2 = saveString(builder, "wor");
|
||||
auto s2 = saveString(builder, "worl");
|
||||
|
||||
REQUIRE(s1 == "hello world"_s);
|
||||
REQUIRE(s2 == "wor"_s);
|
||||
REQUIRE(s2 != s1);
|
||||
REQUIRE(s1 == "hello world");
|
||||
REQUIRE(s2 == "worl");
|
||||
REQUIRE(s2.c_str() != s1.c_str()); // different address
|
||||
|
||||
REQUIRE(spy.log() ==
|
||||
AllocatorLog{
|
||||
Allocate(sizeofStringBuffer()),
|
||||
Reallocate(sizeofStringBuffer(), sizeofString("hello world")),
|
||||
Allocate(sizeofStringBuffer()),
|
||||
Reallocate(sizeofStringBuffer(), sizeofString("wor")),
|
||||
Reallocate(sizeofStringBuffer(), sizeofString("worl")),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user