Skip to content

Commit 1eb109d

Browse files
[MINOR] Lazy set the numBytes of ColumnarBatch (#11759)
1 parent c12a678 commit 1eb109d

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

cpp/core/memory/ColumnarBatch.cc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,14 @@ std::string ArrowCStructColumnarBatch::getType() const {
106106
}
107107

108108
int64_t ArrowCStructColumnarBatch::numBytes() {
109-
int64_t bytes = cArray_->n_buffers;
110-
for (int64_t i = 0; i < cArray_->n_children; ++i) {
111-
bytes += cArray_->children[i]->n_buffers;
109+
if (!numBytes_.has_value()) {
110+
int64_t bytes = cArray_->n_buffers;
111+
for (int64_t i = 0; i < cArray_->n_children; ++i) {
112+
bytes += cArray_->children[i]->n_buffers;
113+
}
114+
numBytes_ = bytes;
112115
}
113-
return bytes;
116+
return numBytes_.value();
114117
}
115118

116119
std::shared_ptr<ArrowSchema> ArrowCStructColumnarBatch::exportArrowSchema() {

cpp/core/memory/ColumnarBatch.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#pragma once
1919

2020
#include <memory>
21+
#include <optional>
2122

2223
#include "arrow/c/bridge.h"
2324
#include "arrow/c/helpers.h"
@@ -59,6 +60,8 @@ class ColumnarBatch {
5960

6061
protected:
6162
int64_t exportNanos_;
63+
// If the batch is immutable batch, the numBytes_ should be a fixed value, lazy set it.
64+
std::optional<int64_t> numBytes_;
6265
};
6366

6467
class ArrowColumnarBatch final : public ColumnarBatch {

cpp/velox/memory/GpuBufferColumnarBatch.cc

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,14 @@ std::vector<char> GpuBufferColumnarBatch::toUnsafeRow(int32_t rowId) const {
5151
}
5252

5353
int64_t GpuBufferColumnarBatch::numBytes() {
54-
int64_t numBytes = 0;
55-
for (const auto& buffer : buffers_) {
56-
numBytes += buffer->size();
54+
if (!numBytes_.has_value()) {
55+
int64_t bytes = 0;
56+
for (const auto& buffer : buffers_) {
57+
bytes += buffer->size();
58+
}
59+
numBytes_ = bytes;
5760
}
58-
return numBytes;
61+
return numBytes_.value();
5962
}
6063

6164
// Optimize to release the previous buffer after merge it.

0 commit comments

Comments
 (0)