-
Notifications
You must be signed in to change notification settings - Fork 82
Introduce count_min_sketch::serialize_to
#509
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
cv4g
wants to merge
6
commits into
apache:master
Choose a base branch
from
cv4g:countmin-serialize-to
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+240
−49
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6029803
Add micro-benchmark for count_min_sketch serialization
cv4g 4e66c11
Add serialize_to sink API for count-min sketch
cv4g 764cdf1
Extend micro-benchmark for count_min_sketch serialization
cv4g 63e7f75
Serialize count_min_sketch sketch array in a single write call
cv4g 91f6b19
Merge branch 'master'
cv4g 163e75e
Merge branch 'master'
cv4g File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
161 changes: 161 additions & 0 deletions
161
benchmarks/benchmark_count_min_sketch_serialization.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| /* | ||
| * 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 <benchmark/benchmark.h> | ||
| #include <count_min.hpp> | ||
|
|
||
| #include <algorithm> | ||
| #include <cstddef> | ||
| #include <cstdint> | ||
| #include <cstring> | ||
| #include <ostream> | ||
| #include <streambuf> | ||
| #include <vector> | ||
|
|
||
| namespace | ||
| { | ||
|
|
||
| using Sketch = datasketches::count_min_sketch<uint64_t>; | ||
|
|
||
| // Roughly 99.9% confidence. Serialization cost then scales with the bucket count. | ||
| constexpr uint8_t NUM_HASHES = 7; | ||
|
|
||
| Sketch makeSketch(uint32_t num_buckets) | ||
| { | ||
| Sketch sketch(NUM_HASHES, num_buckets); | ||
| for (uint64_t i = 0; i < NUM_HASHES; ++i) | ||
| sketch.update(i, i + 1); | ||
| return sketch; | ||
| } | ||
|
|
||
| class fixed_buffer_streambuf final: public std::streambuf | ||
| { | ||
| public: | ||
| explicit fixed_buffer_streambuf(std::vector<uint8_t>& buffer): buffer_(buffer), position_(0) {} | ||
|
|
||
| void reset() | ||
| { | ||
| position_ = 0; | ||
| } | ||
|
|
||
| size_t bytes_written() const | ||
| { | ||
| return position_; | ||
| } | ||
|
|
||
| protected: | ||
| std::streamsize xsputn(const char* data, std::streamsize size) override | ||
| { | ||
| const size_t requested = static_cast<size_t>(size); | ||
| const size_t available = buffer_.size() - position_; | ||
| const size_t bytes_to_write = std::min(requested, available); | ||
| if (bytes_to_write > 0) { | ||
| std::memcpy(buffer_.data() + position_, data, bytes_to_write); | ||
| position_ += bytes_to_write; | ||
| } | ||
| return static_cast<std::streamsize>(bytes_to_write); | ||
| } | ||
|
|
||
| int_type overflow(int_type ch) override | ||
| { | ||
| if (traits_type::eq_int_type(ch, traits_type::eof())) | ||
| return traits_type::not_eof(ch); | ||
| if (position_ == buffer_.size()) | ||
| return traits_type::eof(); | ||
| buffer_[position_++] = static_cast<uint8_t>(traits_type::to_char_type(ch)); | ||
| return ch; | ||
| } | ||
|
|
||
| private: | ||
| std::vector<uint8_t>& buffer_; | ||
| size_t position_; | ||
| }; | ||
|
|
||
| void BM_CountMinGetSerializedSizeBytes(benchmark::State& state) | ||
| { | ||
| const auto sketch = makeSketch(static_cast<uint32_t>(state.range(0))); | ||
|
|
||
| for (auto _ : state) { | ||
| const auto size = sketch.get_serialized_size_bytes(); | ||
| benchmark::DoNotOptimize(size); | ||
| } | ||
|
|
||
| state.SetItemsProcessed(state.iterations()); | ||
| } | ||
|
|
||
| void BM_CountMinSerializeVector(benchmark::State& state) | ||
| { | ||
| const auto sketch = makeSketch(static_cast<uint32_t>(state.range(0))); | ||
| const auto serialized_size = sketch.get_serialized_size_bytes(); | ||
|
|
||
| for (auto _ : state) { | ||
| auto bytes = sketch.serialize(); | ||
| benchmark::DoNotOptimize(bytes.data()); | ||
| benchmark::DoNotOptimize(bytes.size()); | ||
| benchmark::ClobberMemory(); | ||
| } | ||
|
|
||
| state.SetBytesProcessed(state.iterations() * static_cast<int64_t>(serialized_size)); | ||
| } | ||
|
|
||
| void BM_CountMinSerializeOStream(benchmark::State& state) | ||
| { | ||
| const auto sketch = makeSketch(static_cast<uint32_t>(state.range(0))); | ||
| const auto serialized_size = sketch.get_serialized_size_bytes(); | ||
| std::vector<uint8_t> bytes(serialized_size); | ||
| fixed_buffer_streambuf stream_buffer(bytes); | ||
| std::ostream os(&stream_buffer); | ||
|
|
||
| for (auto _ : state) { | ||
| stream_buffer.reset(); | ||
| os.clear(); | ||
| sketch.serialize(os); | ||
| benchmark::DoNotOptimize(stream_buffer.bytes_written()); | ||
| benchmark::ClobberMemory(); | ||
| } | ||
|
|
||
| state.SetBytesProcessed(state.iterations() * static_cast<int64_t>(serialized_size)); | ||
| } | ||
|
|
||
| void BM_CountMinSerializeToSink(benchmark::State& state) | ||
| { | ||
| const auto sketch = makeSketch(static_cast<uint32_t>(state.range(0))); | ||
| const auto serialized_size = sketch.get_serialized_size_bytes(); | ||
| std::vector<uint8_t> bytes(serialized_size); | ||
|
|
||
| for (auto _ : state) { | ||
| uint8_t* ptr = bytes.data(); | ||
| const auto bytes_written = sketch.serialize_to([&ptr](const void* data, size_t size) { | ||
| std::memcpy(ptr, data, size); | ||
| ptr += size; | ||
| }); | ||
| benchmark::DoNotOptimize(ptr); | ||
| benchmark::DoNotOptimize(bytes_written); | ||
| benchmark::ClobberMemory(); | ||
| } | ||
|
|
||
| state.SetBytesProcessed(state.iterations() * static_cast<int64_t>(serialized_size)); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| BENCHMARK(BM_CountMinGetSerializedSizeBytes)->RangeMultiplier(8)->Range(1024, 65536); | ||
| BENCHMARK(BM_CountMinSerializeVector)->RangeMultiplier(8)->Range(1024, 65536); | ||
| BENCHMARK(BM_CountMinSerializeOStream)->RangeMultiplier(8)->Range(1024, 65536); | ||
| BENCHMARK(BM_CountMinSerializeToSink)->RangeMultiplier(8)->Range(1024, 65536); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -300,4 +300,33 @@ TEST_CASE("CountMin sketch: bytes serialize-deserialize non-empty", "[cm_sketch] | |
|
|
||
| } | ||
|
|
||
| TEST_CASE("CountMin sketch: sink serialize matches bytes", "[cm_sketch]") { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you change the test code round-trip way? serialize - deserialize - compare |
||
| auto check_sink_serialize = [](const count_min_sketch<uint64_t>& c, size_t expected_fragments, size_t expected_last_fragment_size = 0) { | ||
| auto bytes = c.serialize(); | ||
| std::vector<uint8_t> sink_bytes; | ||
| sink_bytes.reserve(c.get_serialized_size_bytes()); | ||
| std::vector<size_t> fragment_sizes; | ||
|
|
||
| const size_t bytes_written = c.serialize_to([&sink_bytes, &fragment_sizes](const void* data, size_t size) { | ||
| const auto* begin = static_cast<const uint8_t*>(data); | ||
| sink_bytes.insert(sink_bytes.end(), begin, begin + size); | ||
| fragment_sizes.push_back(size); | ||
| }); | ||
|
|
||
| REQUIRE(bytes_written == bytes.size()); | ||
| REQUIRE(sink_bytes == bytes); | ||
| REQUIRE(fragment_sizes.size() == expected_fragments); | ||
| if (expected_last_fragment_size > 0) { | ||
| REQUIRE(fragment_sizes.back() == expected_last_fragment_size); | ||
| } | ||
| }; | ||
|
|
||
| count_min_sketch<uint64_t> empty(3, 32); | ||
| check_sink_serialize(empty, 9); | ||
|
|
||
| count_min_sketch<uint64_t> non_empty(5, 64); | ||
| for (uint64_t i=0; i < 10; ++i) non_empty.update(i, 10 * i * i); | ||
| check_sink_serialize(non_empty, 11, sizeof(uint64_t) * non_empty.get_num_hashes() * non_empty.get_num_buckets()); | ||
| } | ||
|
|
||
| } /* namespace datasketches */ | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit; how about
Sink?