leaves12138 commented on code in PR #40:
URL: https://github.com/apache/paimon-cpp/pull/40#discussion_r3339426430


##########
src/paimon/common/memory/memory_slice_output.cpp:
##########
@@ -0,0 +1,118 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "paimon/common/memory/memory_slice_output.h"
+
+#include "fmt/format.h"
+#include "paimon/common/utils/math.h"
+#include "paimon/common/utils/var_length_int_utils.h"
+namespace paimon {
+
+MemorySliceOutput::MemorySliceOutput(int32_t estimated_size, MemoryPool* pool) 
{
+    size_ = 0;
+    pool_ = pool;
+    segment_ = MemorySegment::Wrap(Bytes::AllocateBytes(estimated_size, pool));
+}
+
+int32_t MemorySliceOutput::Size() const {
+    return size_;
+}
+
+void MemorySliceOutput::Reset() {
+    size_ = 0;
+}
+
+MemorySlice MemorySliceOutput::ToSlice() {
+    return MemorySlice(segment_, 0, size_);
+}
+
+template <typename T>
+void MemorySliceOutput::WriteValue(T value) {
+    int32_t write_length = sizeof(T);
+    EnsureSize(size_ + write_length);
+    T write_value = value;
+    if (NeedSwap()) {
+        write_value = EndianSwapValue(value);
+    }
+    segment_.PutValue(size_, write_value);
+    size_ += write_length;
+}
+
+Status MemorySliceOutput::WriteVarLenInt(int32_t value) {
+    EnsureSize(size_ + VarLengthIntUtils::kMaxVarIntSize);
+    PAIMON_ASSIGN_OR_RAISE(int32_t bytes_written,
+                           VarLengthIntUtils::EncodeInt(value, 
segment_.MutableData() + size_));
+    size_ += bytes_written;
+    return Status::OK();
+}
+
+Status MemorySliceOutput::WriteVarLenLong(int64_t value) {
+    EnsureSize(size_ + VarLengthIntUtils::kMaxVarLongSize);
+    PAIMON_ASSIGN_OR_RAISE(int32_t bytes_written,
+                           VarLengthIntUtils::EncodeLong(value, 
segment_.MutableData() + size_));
+    size_ += bytes_written;
+    return Status::OK();
+}
+
+void MemorySliceOutput::WriteBytes(const std::shared_ptr<Bytes>& source) {
+    WriteBytes(source, 0, source->size());
+}
+
+void MemorySliceOutput::WriteBytes(const std::shared_ptr<Bytes>& source, 
int32_t source_index,
+                                   int32_t length) {
+    EnsureSize(size_ + length);
+    std::string_view sv{source->data(), source->size()};
+    segment_.Put(size_, sv, source_index, length);
+    size_ += length;
+}
+
+void MemorySliceOutput::SetOrder(ByteOrder order) {
+    byte_order_ = order;
+}
+
+bool MemorySliceOutput::NeedSwap() const {
+    return SystemByteOrder() != byte_order_;
+}
+
+void MemorySliceOutput::EnsureSize(int32_t size) {
+    if (size <= segment_.Size()) {
+        return;
+    }

Review Comment:
   `size` is already the required total capacity passed by callers (`size_ + 
bytes_to_write`). Adding the current segment size again makes growth much 
larger than needed; for example, appending one byte to a full power-of-two 
buffer grows from N to 4N. More importantly, if `MemorySliceOutput(0, pool)` is 
used, `capacity` remains 0 and `capacity <<= 1` loops forever. Please use 
`min_capacity = size` and handle `capacity == 0` explicitly (or 
reject/normalize zero initial capacity), and add coverage for the zero-capacity 
case.



-- 
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]

Reply via email to