lxy-9602 commented on code in PR #588:
URL: https://github.com/apache/paimon-cpp/pull/588#discussion_r4666150131
##########
include/paimon/scan_context.h:
##########
@@ -251,6 +251,8 @@ class PAIMON_EXPORT ScanContextBuilder {
/// @param memory_pool The memory pool to use.
/// @return Reference to this builder for method chaining.
/// @note if not set, memory_pool is default pool
+ /// With a custom pool, native data scans load numbered snapshot files
through FileSystem::Open
+ /// into a buffer allocated from that pool.
/// A custom pool controls only allocations routed through it, not all
planning memory.
Review Comment:
It seems unnecessary to update the interface comments.
##########
src/paimon/core/snapshot.cpp:
##########
@@ -323,11 +332,42 @@ void Snapshot::FromJson(const rapidjson::Value& obj)
noexcept(false) {
RapidJsonUtil::DeserializeKeyValue<std::optional<int64_t>>(obj,
FIELD_NEXT_ROW_ID);
}
-Result<Snapshot> Snapshot::FromPath(const std::shared_ptr<FileSystem>& fs,
- const std::string& path) {
- std::string json_str;
- PAIMON_RETURN_NOT_OK(fs->ReadFile(path, &json_str));
- PAIMON_ASSIGN_OR_RAISE(Snapshot snapshot,
Snapshot::FromJsonString(json_str));
+Result<Snapshot> Snapshot::FromPath(const std::shared_ptr<FileSystem>& fs,
const std::string& path,
+ MemoryPool* pool) {
+ if (pool == nullptr || pool == GetDefaultPool().get()) {
+ std::string json_str;
+ PAIMON_RETURN_NOT_OK(fs->ReadFile(path, &json_str));
+ return Snapshot::FromJsonString(json_str);
+ }
+
+ Bytes json;
+ {
+ PAIMON_ASSIGN_OR_RAISE(std::unique_ptr<InputStream> input,
fs->Open(path));
+ if (input == nullptr) {
+ return Status::Invalid("input stream is not suppose to be a null
pointer, path: ",
+ path);
+ }
+ ScopeGuard close([&input]() { (void)input->Close(); });
+ PAIMON_ASSIGN_OR_RAISE(int64_t length, input->Length());
+ PAIMON_RETURN_NOT_OK(ValidateValueNonNegative(length, "read length"));
+ const uint64_t allocation_size = static_cast<uint64_t>(length) + 1;
+ PAIMON_RETURN_NOT_OK(ValidateValueInRange<size_t>(allocation_size,
"snapshot buffer size"));
+ try {
+ json = Bytes(static_cast<size_t>(allocation_size), pool);
+ } catch (const std::bad_alloc&) {
+ return Status::OutOfMemory("cannot allocate ", allocation_size, "
bytes for snapshot ",
+ path);
Review Comment:
Could you clarify why we need the additional `try-catch` here? If we follow
this pattern, it seems like we might end up wrapping the entire end-to-end flow
with `try-catch` for any memory allocation, which may be overkill—so I’m
wondering about the rationale.
##########
src/paimon/core/utils/snapshot_manager.h:
##########
@@ -126,6 +126,7 @@ class SnapshotManager {
std::string root_path_;
std::string branch_;
SnapshotLoader snapshot_loader_;
+ std::shared_ptr<MemoryPool> pool_;
};
Review Comment:
I think if we’re going to overhaul this part thoroughly, the `SchemaManager`
likely has similar issues too. Is there any plan to adjust it as well? Also, do
the various operations—read/write/commit/scan—follow the same logic? Will these
be adjusted together as part of the future plan?
##########
src/paimon/common/utils/rapidjson_util.h:
##########
@@ -75,27 +75,34 @@ class RapidJsonUtil {
// if T is custom type, T must have FromJson()
template <typename T>
static inline Status FromJsonString(const std::string& json_str, T* obj) {
+ return FromNullTerminatedJson(json_str, obj, json_str);
+ }
+
+ // The view excludes the final NUL, which must follow it in the backing
buffer.
+ template <typename T>
+ static inline Status FromNullTerminatedJson(std::string_view json_str, T*
obj,
+ std::string_view diagnostic) {
Review Comment:
For output parameters, could you please place them at the end ? i.e., please
move the `diagnostic` and `obj` parameters to their correct positions.
##########
src/paimon/core/snapshot.cpp:
##########
@@ -323,11 +332,42 @@ void Snapshot::FromJson(const rapidjson::Value& obj)
noexcept(false) {
RapidJsonUtil::DeserializeKeyValue<std::optional<int64_t>>(obj,
FIELD_NEXT_ROW_ID);
}
-Result<Snapshot> Snapshot::FromPath(const std::shared_ptr<FileSystem>& fs,
- const std::string& path) {
- std::string json_str;
- PAIMON_RETURN_NOT_OK(fs->ReadFile(path, &json_str));
- PAIMON_ASSIGN_OR_RAISE(Snapshot snapshot,
Snapshot::FromJsonString(json_str));
+Result<Snapshot> Snapshot::FromPath(const std::shared_ptr<FileSystem>& fs,
const std::string& path,
+ MemoryPool* pool) {
+ if (pool == nullptr || pool == GetDefaultPool().get()) {
+ std::string json_str;
+ PAIMON_RETURN_NOT_OK(fs->ReadFile(path, &json_str));
+ return Snapshot::FromJsonString(json_str);
+ }
+
+ Bytes json;
+ {
+ PAIMON_ASSIGN_OR_RAISE(std::unique_ptr<InputStream> input,
fs->Open(path));
+ if (input == nullptr) {
+ return Status::Invalid("input stream is not suppose to be a null
pointer, path: ",
+ path);
+ }
+ ScopeGuard close([&input]() { (void)input->Close(); });
+ PAIMON_ASSIGN_OR_RAISE(int64_t length, input->Length());
+ PAIMON_RETURN_NOT_OK(ValidateValueNonNegative(length, "read length"));
+ const uint64_t allocation_size = static_cast<uint64_t>(length) + 1;
+ PAIMON_RETURN_NOT_OK(ValidateValueInRange<size_t>(allocation_size,
"snapshot buffer size"));
+ try {
+ json = Bytes(static_cast<size_t>(allocation_size), pool);
+ } catch (const std::bad_alloc&) {
+ return Status::OutOfMemory("cannot allocate ", allocation_size, "
bytes for snapshot ",
+ path);
+ }
+ PAIMON_ASSIGN_OR_RAISE(int64_t read_length, input->Read(json.data(),
length));
+ if (read_length != length) {
+ return Status::IOError(fmt::format("path {}, expect read len {},
actual read len {}",
+ path, length, read_length));
+ }
+ json.data()[length] = '\0';
+ }
+ Snapshot snapshot;
+ PAIMON_RETURN_NOT_OK(RapidJsonUtil::FromNullTerminatedJson(
+ std::string_view(json.data(), json.size() - 1), &snapshot, path));
return snapshot;
Review Comment:
Hi, @realAaronWu In PR #584, your measurements showed schema JSON sizes of
roughly 0.7–7.7 KB. Snapshot JSON appears even smaller—all 372 snapshots in
this repository are under 1 KB—and its input buffer is short-lived.
Propagating the memory pool consistently through all snapshot-loading paths
would touch several components. Could you share the motivating workload,
expected snapshot sizes, concurrency, and memory budgets? It would help us
understand whether this addresses a measurable memory issue or a requirement to
account for every allocation, and assess the implementation cost accordingly.
##########
src/paimon/core/snapshot.h:
##########
@@ -242,8 +242,8 @@ class Snapshot : public Jsonizable<Snapshot> {
void FromJson(const rapidjson::Value& obj) noexcept(false) override;
- static Result<Snapshot> FromPath(const std::shared_ptr<FileSystem>& fs,
- const std::string& path);
+ static Result<Snapshot> FromPath(const std::shared_ptr<FileSystem>& fs,
const std::string& path,
+ MemoryPool* pool = nullptr);
Review Comment:
We added default parameters in the official code, so other places can
compile even if they don’t call the updated APIs—it's easy to miss necessary
changes. However, if we remove the default parameters, we’d likely need to
update most areas related to snapshots. @lucasfang What do you think?
##########
src/paimon/core/table/file_store_table.h:
##########
@@ -76,8 +76,9 @@ class FileStoreTable : public Table {
return schema_manager_;
}
- std::shared_ptr<SnapshotManager> GetSnapshotManager() const {
- return std::make_shared<SnapshotManager>(file_system_, root_path_,
CurrentBranch());
+ std::shared_ptr<SnapshotManager> GetSnapshotManager(
+ const std::shared_ptr<MemoryPool>& pool = nullptr) const {
+ return std::make_shared<SnapshotManager>(file_system_, root_path_,
CurrentBranch(), pool);
Review Comment:
Please avoid default parameters in production code.
##########
src/paimon/core/snapshot.cpp:
##########
@@ -323,11 +332,42 @@ void Snapshot::FromJson(const rapidjson::Value& obj)
noexcept(false) {
RapidJsonUtil::DeserializeKeyValue<std::optional<int64_t>>(obj,
FIELD_NEXT_ROW_ID);
}
-Result<Snapshot> Snapshot::FromPath(const std::shared_ptr<FileSystem>& fs,
- const std::string& path) {
- std::string json_str;
- PAIMON_RETURN_NOT_OK(fs->ReadFile(path, &json_str));
- PAIMON_ASSIGN_OR_RAISE(Snapshot snapshot,
Snapshot::FromJsonString(json_str));
+Result<Snapshot> Snapshot::FromPath(const std::shared_ptr<FileSystem>& fs,
const std::string& path,
+ MemoryPool* pool) {
+ if (pool == nullptr || pool == GetDefaultPool().get()) {
+ std::string json_str;
+ PAIMON_RETURN_NOT_OK(fs->ReadFile(path, &json_str));
+ return Snapshot::FromJsonString(json_str);
+ }
+
+ Bytes json;
+ {
+ PAIMON_ASSIGN_OR_RAISE(std::unique_ptr<InputStream> input,
fs->Open(path));
+ if (input == nullptr) {
+ return Status::Invalid("input stream is not suppose to be a null
pointer, path: ",
+ path);
+ }
+ ScopeGuard close([&input]() { (void)input->Close(); });
+ PAIMON_ASSIGN_OR_RAISE(int64_t length, input->Length());
+ PAIMON_RETURN_NOT_OK(ValidateValueNonNegative(length, "read length"));
+ const uint64_t allocation_size = static_cast<uint64_t>(length) + 1;
+ PAIMON_RETURN_NOT_OK(ValidateValueInRange<size_t>(allocation_size,
"snapshot buffer size"));
+ try {
+ json = Bytes(static_cast<size_t>(allocation_size), pool);
+ } catch (const std::bad_alloc&) {
+ return Status::OutOfMemory("cannot allocate ", allocation_size, "
bytes for snapshot ",
+ path);
+ }
+ PAIMON_ASSIGN_OR_RAISE(int64_t read_length, input->Read(json.data(),
length));
+ if (read_length != length) {
+ return Status::IOError(fmt::format("path {}, expect read len {},
actual read len {}",
+ path, length, read_length));
+ }
+ json.data()[length] = '\0';
+ }
+ Snapshot snapshot;
Review Comment:
Could we use `doc.Parse(json.data(), json.size())` and rename the helper to
`FromJsonView()`? This would remove the extra byte allocation, manual NUL
terminator, and `size() - 1`, while keeping the embedded-NUL validation.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]