mapleFU commented on code in PR #55449:
URL: https://github.com/apache/arrow/pull/55449#discussion_r4676589753
##########
cpp/src/arrow/util/alp/alp_compression.cc:
##########
@@ -331,7 +323,17 @@ AlpExponentAndFactor
AlpCompression<T>::FindBestExponentAndFactor(
return combinations.front();
}
- const std::vector<T> sample = CreateSample(inputs);
+ // The sample is capped at 256 values. Keep it on the stack to avoid a heap
+ // allocation for every vector parameter search.
+ std::array<T, AlpSamplingConstants::kSamplesPerChunk> sample_storage;
+ const int32_t idx_increments = std::max<int32_t>(
+ 1, static_cast<int32_t>(std::ceil(static_cast<double>(inputs.size()) /
+
AlpSamplingConstants::kSamplesPerChunk)));
+ size_t sample_count = 0;
+ for (size_t i = 0; i < inputs.size(); i += idx_increments) {
+ sample_storage[sample_count++] = inputs[i];
Review Comment:
nit: can we put ++ to another line?
##########
cpp/src/arrow/util/alp/alp_codec.cc:
##########
@@ -153,61 +153,45 @@ Result<int64_t> EncodePage(std::span<const T> inputs,
const AlpEncodingPreset& p
std::span<uint8_t> body = output.subspan(AlpHeader::kSize);
const int64_t element_count = static_cast<int64_t>(inputs.size());
- // Phase 1: compress all vectors and collect them.
- std::vector<AlpEncodedVector<T>> encoded_vectors;
const int64_t num_vectors = ::arrow::bit_util::CeilDiv(element_count,
vector_size);
- encoded_vectors.reserve(num_vectors);
-
- int64_t input_offset = 0;
- for (int64_t remaining_elements = element_count; remaining_elements > 0;) {
- const int64_t elements_to_encode =
- std::min(static_cast<int64_t>(vector_size), remaining_elements);
- encoded_vectors.push_back(AlpCompression<T>::Compress(
- inputs.subspan(static_cast<size_t>(input_offset),
- static_cast<size_t>(elements_to_encode)),
- preset));
- input_offset += elements_to_encode;
- remaining_elements -= elements_to_encode;
- }
-
- // Phase 2: calculate sizes and offsets.
const int64_t per_vector_metadata_size =
AlpInfo::kStoredSize +
GetIntegerEncodingMetadataSize<T>(preset.integer_encoding);
const int64_t offsets_section_size =
num_vectors *
static_cast<int64_t>(sizeof(AlpFormatConstants::OffsetType));
-
- std::vector<AlpFormatConstants::OffsetType> vector_offsets;
- vector_offsets.reserve(num_vectors);
+ if (offsets_section_size > static_cast<int64_t>(body.size())) {
+ return Status::Invalid("ALP output buffer too small for offsets: ",
body.size(),
+ " < ", offsets_section_size);
+ }
int64_t current_offset = offsets_section_size;
- for (const auto& vec : encoded_vectors) {
+ int64_t input_offset = 0;
+ for (int64_t vector_index = 0; vector_index < num_vectors; ++vector_index) {
+ const int64_t remaining_elements = element_count - input_offset;
+ const int64_t elements_to_encode =
+ std::min(static_cast<int64_t>(vector_size), remaining_elements);
+ const auto vec = AlpCompression<T>::Compress(
+ inputs.subspan(static_cast<size_t>(input_offset),
+ static_cast<size_t>(elements_to_encode)),
+ preset);
+ const int64_t stored_size = per_vector_metadata_size +
vec.GetDataStoredSize();
if (current_offset >
static_cast<int64_t>(
std::numeric_limits<AlpFormatConstants::OffsetType>::max())) {
return Status::Invalid("ALP encoded data exceeds the uint32 offset
range");
}
-
vector_offsets.push_back(static_cast<AlpFormatConstants::OffsetType>(current_offset));
- current_offset += per_vector_metadata_size + vec.GetDataStoredSize();
+ if (stored_size > static_cast<int64_t>(body.size()) - current_offset) {
Review Comment:
Would it be useful to marking it as [[unlikely]]?
##########
cpp/src/arrow/util/alp/alp_compression.cc:
##########
@@ -563,17 +573,18 @@ void AlpCompression<T>::Decompress(const
AlpEncodedVectorView<T>& encoded_view,
// Read the exponent/factor once. AlpInfo stores them as uint8_t, which may
// alias the output stores, so reading them inside the loop blocks
vectorization.
const AlpExponentAndFactor exponent_and_factor =
alp_info.GetExponentAndFactor();
+ const auto multipliers = AlpInlines<T>::GetMultipliers(exponent_and_factor);
for (size_t i = 0; i < output.size(); ++i) {
const EncodedUnsigned unfored_value = scratch[i] + frame_of_ref;
const EncodedSigned signed_value =
util::SafeCopy<EncodedSigned>(unfored_value);
- output[i] = AlpInlines<T>::DecodeValue(signed_value, exponent_and_factor);
+ output[i] = AlpInlines<T>::DecodeValue(signed_value, multipliers);
}
Review Comment:
clang x86 can optimize it well in some scenerio
--
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]