Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 8 additions & 17 deletions cpp/src/arrow/util/alp/alp_compression.cc
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,11 @@ AlpEncodingPreset AlpCompression<T>::MakePreset(

template <AlpFloatingType T>
std::vector<T> AlpCompression<T>::CreateSample(std::span<const T> inputs) {
// Pick an equidistant stride so the sample count is capped at kSamplesPerChunk.
// Pick an equidistant stride so the sample count is capped at
// kSearchSamplesPerVector.
const int32_t idx_increments = std::max<int32_t>(
1, static_cast<int32_t>(std::ceil(static_cast<double>(inputs.size()) /
AlpSamplingOptions::kSamplesPerChunk)));
AlpSamplingOptions::kSearchSamplesPerVector)));
const size_t increment = static_cast<size_t>(idx_increments);
std::vector<T> sample;
sample.reserve((inputs.size() + increment - 1) / increment);
Expand All @@ -335,9 +336,9 @@ AlpExponentAndFactor AlpCompression<T>::FindBestExponentAndFactor(

AlpExponentAndFactor best_exponent_and_factor;
int64_t best_total_bits = std::numeric_limits<int64_t>::max();
int64_t worse_total_bits_counter = 0;

// Try each candidate combination to find the one which minimizes compression size.
// Try every candidate combination, at most kMaxCombinations, and keep the one
// which minimizes compression size. Ties keep the earlier, more frequent one.
// penalize_exceptions=false because this is second-level sampling: the preset
// already filtered out bad combinations during first-level sampling, so we
// just pick whichever pre-vetted combination compresses this vector best.
Expand All @@ -346,20 +347,10 @@ AlpExponentAndFactor AlpCompression<T>::FindBestExponentAndFactor(
EstimateCompressedSizeBits<T>(sample, exponent_and_factor,
/*penalize_exceptions=*/false)
.value_or(std::numeric_limits<int64_t>::max());

// If current compression size is worse or equal than current best combination.
if (estimated_compression_size >= best_total_bits) {
worse_total_bits_counter += 1;
// Early exit strategy.
if (worse_total_bits_counter == AlpSamplingOptions::kEarlyExitThreshold) {
break;
}
continue;
if (estimated_compression_size < best_total_bits) {
best_total_bits = estimated_compression_size;
best_exponent_and_factor = exponent_and_factor;
}
// Otherwise replace the best and continue trying with next combination.
best_total_bits = estimated_compression_size;
best_exponent_and_factor = exponent_and_factor;
worse_total_bits_counter = 0;
}
return best_exponent_and_factor;
}
Expand Down
2 changes: 1 addition & 1 deletion cpp/src/arrow/util/alp/alp_compression_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class ARROW_EXPORT AlpCompression {

/// Creates an encoding preset from the collected samples.
///
/// Each element is one strided sample from a sampling chunk.
/// Each element is the sample of one vector and casts one vote.
static AlpEncodingPreset MakePreset(const std::vector<std::vector<T>>& samples);

/// Decompresses a view into caller-provided outputs and reusable scratch space.
Expand Down
26 changes: 11 additions & 15 deletions cpp/src/arrow/util/alp/alp_constants_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,22 @@ static_assert(sizeof(AlpFormatConstants::PositionType) == 2);
// Sampling and parameter-search tuning

struct AlpSamplingOptions {
/// Input elements per sampling chunk.
static constexpr int64_t kChunkSize = 4096;
/// Values per sampling window: 128 vectors, the power of two nearest the ALP
/// paper's row group of 100 vectors (section 4, "Sampling Parameters").
static constexpr int64_t kSampleWindowSize = 131072;

/// Elements per sampling interval.
static constexpr int64_t kIntervalSize = 122880;
/// Vectors sampled from each window. The ALP paper samples 8 per row group.
static constexpr int64_t kSampledVectors = 8;

/// Samples collected per chunk.
static constexpr int64_t kSamplesPerChunk = 256;
/// Values sampled from each sampled vector. The ALP paper uses 32.
static constexpr int64_t kSamplesPerVector = 32;

/// Chunks sampled per interval.
static constexpr int64_t kSampleChunksPerInterval = 8;
/// Values sampled from a vector when choosing among preset combinations.
static constexpr int64_t kSearchSamplesPerVector = 256;

/// Consecutive non-improving combinations before an early exit.
static constexpr uint8_t kEarlyExitThreshold = 4;

/// Maximum exponent-factor combinations retained in a preset.
/// Maximum exponent-factor combinations retained in a preset. The ALP paper
/// keeps 5.
static constexpr uint8_t kMaxCombinations = 5;

static_assert(kMaxCombinations > kEarlyExitThreshold,
"early exit requires at least one additional candidate");
};

// Power-of-ten constants
Expand Down
124 changes: 56 additions & 68 deletions cpp/src/arrow/util/alp/alp_sampler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "arrow/util/alp/alp_sampler_internal.h"

#include <algorithm>
#include <cstdint>
#include <span>
#include <utility>

Expand All @@ -30,95 +31,82 @@ namespace arrow::util::alp {

namespace {

struct AlpSamplingParameters {
/// Prefix length examined in the chunk.
int64_t values_to_examine;
/// Stride between sampled values.
int64_t sample_stride;
};

// The interval holds 30 4096-element chunks. Round up the jump so about eight
// chunks per interval are sampled instead of ten under integer truncation.
constexpr int64_t kSampledChunkStride = bit_util::CeilDiv(
bit_util::CeilDiv(AlpSamplingOptions::kIntervalSize, AlpSamplingOptions::kChunkSize),
AlpSamplingOptions::kSampleChunksPerInterval);
static_assert(kSampledChunkStride > 0, "sample jump must be positive");

// Caps the lookup window and chooses a stride that yields at most one sample per
// kSamplesPerChunk values.
AlpSamplingParameters GetSamplingParameters(int64_t chunk_size) {
const int64_t values_to_examine =
std::min(chunk_size, static_cast<int64_t>(AlpFormatConstants::kDefaultVectorSize));
const int64_t sample_stride = std::max<int64_t>(
1, bit_util::CeilDiv(values_to_examine, AlpSamplingOptions::kSamplesPerChunk));
return AlpSamplingParameters{values_to_examine, sample_stride};
}

bool ShouldSkipChunk(const int64_t chunk_index, const int64_t sampled_chunk_count,
const int64_t chunk_size) {
if ((chunk_index % kSampledChunkStride) != 0) {
return true;
// splitmix64, a small fixed-seed generator: the same input always yields the same
// sample.
class SampleRng {
public:
explicit SampleRng(uint64_t seed) : state_(0x9E3779B97F4A7C15ULL ^ seed) {}

uint64_t Next() {
uint64_t z = (state_ += 0x9E3779B97F4A7C15ULL);
z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ULL;
z = (z ^ (z >> 27)) * 0x94D049BB133111EBULL;
return z ^ (z >> 31);
}

// Skip short chunks unless nothing has been sampled yet: for inputs shorter
// than a full sample window, a short chunk is the only sample available.
return chunk_size < AlpSamplingOptions::kSamplesPerChunk && sampled_chunk_count != 0;
}
private:
uint64_t state_;
};

} // namespace

template <AlpFloatingType T>
void AlpSampler<T>::AddSample(std::span<const T> input) {
const int64_t input_size = static_cast<int64_t>(input.size());
for (int64_t i = 0; i < input_size; i += AlpSamplingOptions::kChunkSize) {
const int64_t elements = std::min(input_size - i, AlpSamplingOptions::kChunkSize);
AddSampleChunk({input.data() + i, static_cast<size_t>(elements)});
std::vector<int64_t> GetSampledVectorOffsets(int64_t num_values) {
constexpr int64_t kVectorSize = AlpFormatConstants::kDefaultVectorSize;
constexpr int64_t kWindowSize = AlpSamplingOptions::kSampleWindowSize;
static_assert(kWindowSize % kVectorSize == 0, "windows must hold whole vectors");

std::vector<int64_t> offsets;
for (int64_t window = 0; window < num_values; window += kWindowSize) {
const int64_t num_vectors =
bit_util::CeilDiv(std::min(kWindowSize, num_values - window), kVectorSize);
const int64_t num_sampled =
std::min(num_vectors, AlpSamplingOptions::kSampledVectors);
for (int64_t i = 0; i < num_sampled; ++i) {
offsets.push_back(window + i * num_vectors / num_sampled * kVectorSize);
}
}
return offsets;
}

template <AlpFloatingType T>
void AlpSampler<T>::AddSampleChunk(std::span<const T> input) {
if (input.empty()) {
return;
}

void AlpSampler<T>::AddSample(std::span<const T> input) {
const int64_t input_size = static_cast<int64_t>(input.size());
const bool should_skip =
ShouldSkipChunk(chunks_processed_, chunks_sampled_, input_size);

chunks_processed_ += 1;
values_processed_ += input_size;
if (should_skip) {
return;
for (const int64_t offset : GetSampledVectorOffsets(input_size)) {
const int64_t length =
std::min<int64_t>(AlpFormatConstants::kDefaultVectorSize, input_size - offset);
AddVectorSample(
input.subspan(static_cast<size_t>(offset), static_cast<size_t>(length)), offset);
}
}

const AlpSamplingParameters sampling_params = GetSamplingParameters(input_size);
const int64_t sample_count =
bit_util::CeilDiv(sampling_params.values_to_examine, sampling_params.sample_stride);
ARROW_CHECK_LE(sample_count, AlpSamplingOptions::kSamplesPerChunk)
<< "ALP sampler produced too many values for one chunk: " << sample_count << " > "
<< AlpSamplingOptions::kSamplesPerChunk;

std::vector<T> chunk_sample;
chunk_sample.reserve(static_cast<size_t>(sample_count));
for (int64_t i = 0; i < sampling_params.values_to_examine;
i += sampling_params.sample_stride) {
chunk_sample.push_back(input[static_cast<size_t>(i)]);
template <AlpFloatingType T>
void AlpSampler<T>::AddVectorSample(std::span<const T> vector, int64_t offset) {
const size_t stride = static_cast<size_t>(bit_util::CeilDiv(
static_cast<int64_t>(vector.size()), AlpSamplingOptions::kSamplesPerVector));

// One value from each run of `stride` values, at a pseudo-random position within
// the run, so the sample covers the whole vector without lining up with data
// that repeats with a period dividing the stride.
SampleRng rng(static_cast<uint64_t>(offset) * 0xD1B54A32D192ED03ULL);
std::vector<T> sample;
sample.reserve(static_cast<size_t>(AlpSamplingOptions::kSamplesPerVector));
for (size_t i = 0; i < vector.size(); i += stride) {
const size_t run = std::min(stride, vector.size() - i);
sample.push_back(vector[i + static_cast<size_t>(rng.Next() % run)]);
}
sampled_values_count_ += static_cast<int64_t>(chunk_sample.size());

chunk_samples_.push_back(std::move(chunk_sample));
chunks_sampled_++;
sampled_values_count_ += static_cast<int64_t>(sample.size());
vector_samples_.push_back(std::move(sample));
}

template <AlpFloatingType T>
AlpEncodingPreset AlpSampler<T>::MakePreset() const {
ARROW_LOG(DEBUG) << "AlpSampler create preset: chunksSampled=" << chunks_sampled_ << "/"
<< chunks_processed_ << " total"
ARROW_LOG(DEBUG) << "AlpSampler create preset: vectorsSampled="
<< vector_samples_.size()
<< ", valuesSampled=" << sampled_values_count_ << "/"
<< values_processed_ << " total";

AlpEncodingPreset preset = AlpCompression<T>::MakePreset(chunk_samples_);
AlpEncodingPreset preset = AlpCompression<T>::MakePreset(vector_samples_);

ARROW_LOG(DEBUG) << "AlpSampler preset: " << preset.combinations.size()
<< " exponent/factor combinations"
Expand Down
30 changes: 17 additions & 13 deletions cpp/src/arrow/util/alp/alp_sampler_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#pragma once

#include <cstdint>
#include <span>
#include <vector>

Expand All @@ -27,10 +28,17 @@

namespace arrow::util::alp {

/// Returns the offsets of the vectors AlpSampler samples from an input of
/// `num_values` values. Each window of AlpSamplingOptions::kSampleWindowSize
/// values, or the whole input if shorter, contributes up to
/// AlpSamplingOptions::kSampledVectors vectors of
/// AlpFormatConstants::kDefaultVectorSize values spaced evenly across it.
ARROW_EXPORT std::vector<int64_t> GetSampledVectorOffsets(int64_t num_values);

/// Collects samples and builds an ALP encoding preset.
///
/// Input is split into fixed-size chunks. Each sampled chunk contributes one
/// strided sample. Call AddSample() one or more times, then MakePreset().
/// Each AddSample() call samples the vectors given by GetSampledVectorOffsets().
/// Call AddSample() one or more times, then MakePreset().
template <AlpFloatingType T>
class ARROW_EXPORT AlpSampler {
public:
Expand All @@ -45,20 +53,16 @@ class ARROW_EXPORT AlpSampler {
AlpEncodingPreset MakePreset() const;

private:
/// Adds one sampling chunk. Only the first
/// AlpFormatConstants::kDefaultVectorSize elements are examined.
void AddSampleChunk(std::span<const T> input);
/// Adds a sample of at most AlpSamplingOptions::kSamplesPerVector values of the
/// vector at `offset` in the input.
void AddVectorSample(std::span<const T> vector, int64_t offset);

/// Number of chunks that have contributed a sample.
int64_t chunks_sampled_{0};
/// Number of chunks processed.
int64_t chunks_processed_{0};
/// Number of values processed.
/// Number of values passed to AddSample().
int64_t values_processed_{0};
/// Number of values retained in chunk_samples_.
/// Number of values retained in vector_samples_.
int64_t sampled_values_count_{0};
/// One strided sample per sampled chunk. MakePreset() only reads these.
std::vector<std::vector<T>> chunk_samples_;
/// The sample of each sampled vector. MakePreset() only reads these.
std::vector<std::vector<T>> vector_samples_;
};

} // namespace arrow::util::alp
Loading
Loading