chore: switch json object to pmr allocator (#2483)

* chore: switch json object to pmr allocator

1. Move json object creation code to the shard-thread inside rdb_load
2. Now json_family never references "json" type, always dfly::JsonType
3. Switch JsonType to pmr::json.
4. Make sure we pass the correct memory_resource when creating json object from string.

Signed-off-by: Roman Gershman <roman@dragonflydb.io>
---------

Signed-off-by: Roman Gershman <roman@dragonflydb.io>
This commit is contained in:
Roman Gershman 2024-01-26 12:47:46 +02:00 committed by GitHub
parent a5b9401449
commit ea5955962e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 96 additions and 153 deletions

View file

@ -377,7 +377,6 @@ class RdbLoaderBase::OpaqueObjLoader {
void operator()(const base::PODArray<char>& str);
void operator()(const LzfString& lzfstr);
void operator()(const unique_ptr<LoadTrace>& ptr);
void operator()(const JsonType& jt);
std::error_code ec() const {
return ec_;
@ -462,10 +461,6 @@ void RdbLoaderBase::OpaqueObjLoader::operator()(const unique_ptr<LoadTrace>& ptr
}
}
void RdbLoaderBase::OpaqueObjLoader::operator()(const JsonType& json) {
pv_->SetJson(JsonType{json});
}
void RdbLoaderBase::OpaqueObjLoader::CreateSet(const LoadTrace* ltrace) {
size_t len = ltrace->blob_count();
@ -1080,6 +1075,12 @@ void RdbLoaderBase::OpaqueObjLoader::HandleBlob(string_view blob) {
unsigned char* lp = (uint8_t*)zmalloc(bytes);
std::memcpy(lp, src_lp, bytes);
pv_->InitRobj(OBJ_ZSET, OBJ_ENCODING_LISTPACK, lp);
} else if (rdb_type_ == RDB_TYPE_JSON) {
auto json = JsonFromString(blob);
if (!json) {
ec_ = RdbError(errc::bad_json_string);
}
pv_->SetJson(std::move(*json));
} else {
LOG(FATAL) << "Unsupported rdb type " << rdb_type_;
}
@ -1400,20 +1401,18 @@ error_code RdbLoaderBase::ReadObj(int rdbtype, OpaqueObj* dest) {
iores = ReadStreams();
break;
case RDB_TYPE_JSON:
iores = ReadJson();
break;
case RDB_TYPE_SET_LISTPACK:
// We need to deal with protocol versions 9 and older because in these
// RDB_TYPE_JSON == 20. On newer versions > 9 we bumped up RDB_TYPE_JSON to 30
// because it overlapped with the new type RDB_TYPE_SET_LISTPACK
if (rdb_version_ < 10 && rdbtype == RDB_TYPE_JSON_OLD) {
if (rdb_version_ < 10) {
// consider it RDB_TYPE_JSON_OLD
iores = ReadJson();
break;
} else {
iores = ReadGeneric(rdbtype);
}
if (rdbtype == RDB_TYPE_JSON) {
iores = ReadJson();
break;
}
iores = ReadGeneric(rdbtype);
break;
default:
LOG(ERROR) << "Unsupported rdb type " << rdbtype;
@ -1829,14 +1828,12 @@ auto RdbLoaderBase::ReadStreams() -> io::Result<OpaqueObj> {
}
auto RdbLoaderBase::ReadJson() -> io::Result<OpaqueObj> {
string json_str;
SET_OR_UNEXPECT(FetchGenericString(), json_str);
RdbVariant dest;
error_code ec = ReadStringObj(&dest);
if (ec)
return make_unexpected(ec);
auto json = JsonFromString(json_str);
if (!json)
return Unexpected(errc::bad_json_string);
return OpaqueObj{std::move(*json), RDB_TYPE_JSON};
return OpaqueObj{std::move(dest), RDB_TYPE_JSON};
}
template <typename T> io::Result<T> RdbLoaderBase::FetchInt() {