Skip to content
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

Compressed_Buffer_Improvements #119

Merged
merged 1 commit into from
Dec 16, 2024
Merged
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
91 changes: 35 additions & 56 deletions spectator/compressed_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,74 +18,40 @@ class CompressedBuffer {
static constexpr size_t kDefaultChunkSizeInput = 256 * 1024;
static constexpr size_t kDefaultOutSize = 256 * 1024;
static constexpr size_t kDefaultChunkSizeOutput = 32 * 1024;

explicit CompressedBuffer(size_t chunk_size_input = kDefaultChunkSizeInput,
size_t out_size = kDefaultOutSize,
size_t chunk_size_output = kDefaultChunkSizeOutput);
CompressedBuffer(const CompressedBuffer&) = delete;
CompressedBuffer(CompressedBuffer&&) = default;
auto operator=(const CompressedBuffer&) -> CompressedBuffer& = delete;
auto operator=(CompressedBuffer &&) -> CompressedBuffer& = delete;
auto operator=(CompressedBuffer&&) -> CompressedBuffer& = delete;

~CompressedBuffer();

auto Init() -> void;

auto Append(std::string_view s) -> void {
assert(init_);
std::copy(s.begin(), s.end(), std::back_inserter(cur_));
maybe_compress();
}

auto Append(uint8_t b) -> void {
assert(init_);
cur_.push_back(b);
maybe_compress();
}

auto Append(uint8_t b1, uint8_t b2) -> void {
/**
* Append Templated Types to an internal buffer
*
* A variadic function that accepts a flexible number of arguments of either strings (std::string
* or std::string_view) or (uint8_t). It checks that the types are valid at compile time, and
* forwards them to an internal appending function for processing. We have statically asserted
* that all types in the parameter pack are either string_view or uint8_t
*
* @param args is a parameter pack of forwarding references that can accept a variable number of
* arguments of any type.
*
*/
template <typename... Args>
auto Append(Args&&... args) -> void {
static_assert(
((std::is_convertible_v<std::decay_t<Args>, std::string_view> ||
std::is_same_v<std::decay_t<Args>, uint8_t>) &&
...),
"Invalid argument types. Only std::string, std::string_view, or uint8_t are allowed.");
assert(init_);
cur_.push_back(b1);
cur_.push_back(b2);
maybe_compress();
}

auto Append(uint8_t b1, uint8_t b2, uint8_t b3) -> void {
assert(init_);
cur_.push_back(b1);
cur_.push_back(b2);
cur_.push_back(b3);
maybe_compress();
}

auto Append(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4) -> void {
assert(init_);
cur_.push_back(b1);
cur_.push_back(b2);
cur_.push_back(b3);
cur_.push_back(b4);
maybe_compress();
}

auto Append(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5)
-> void {
assert(init_);
cur_.push_back(b1);
cur_.push_back(b2);
cur_.push_back(b3);
cur_.push_back(b4);
cur_.push_back(b5);
maybe_compress();
}

auto Append(uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5,
uint8_t b6) {
assert(init_);
cur_.push_back(b1);
cur_.push_back(b2);
cur_.push_back(b3);
cur_.push_back(b4);
cur_.push_back(b5);
cur_.push_back(b6);
AppendImpl(std::forward<Args>(args)...);
maybe_compress();
}

Expand All @@ -105,6 +71,19 @@ class CompressedBuffer {
auto maybe_compress() -> void;

auto deflate_chunk(size_t chunk_size, int flush) -> int;

template <typename... Args>
auto AppendImpl(Args&&... args) -> void {
if constexpr ((std::is_convertible_v<std::decay_t<Args>, std::string_view> && ...)) {
static_assert(sizeof...(args) == 1, "Only one string-like argument is allowed.");
(cur_.insert(cur_.end(), std::forward<Args>(args).begin(), std::forward<Args>(args).end()),
...);
} else if constexpr ((std::is_same_v<std::decay_t<Args>, uint8_t> && ...)) {
static_assert(sizeof...(args) >= 1 && sizeof...(args) <= 6,
"The number of uint8_t arguments must be between 1 and 6.");
(cur_.push_back(std::forward<Args>(args)), ...);
}
}
};

} // namespace spectator
32 changes: 29 additions & 3 deletions spectator/compressed_buffer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ TEST(CompressedBuffer, Basic) {
buf.Append(string1);
buf.Append(string2);
buf.Append(string3);
buf.Append('a', 'b', 'c');
auto res = buf.Result();
buf.Append(static_cast<uint8_t>('a'), static_cast<uint8_t>('b'), static_cast<uint8_t>('c'));
auto res{buf.Result()};

char uncompressed[32768];
size_t dest_len = sizeof(uncompressed);
Expand All @@ -36,7 +36,7 @@ TEST(CompressedBuffer, Basic) {
// make sure we can reuse the buffer
buf.Init();
buf.Append(string2);
buf.Append(':', 'd');
buf.Append(static_cast<uint8_t>(':'), static_cast<uint8_t>('d'));
res = buf.Result();

gzip_uncompress(uncompressed, &dest_len, res.data, res.size);
Expand All @@ -45,4 +45,30 @@ TEST(CompressedBuffer, Basic) {
EXPECT_EQ(result2, expected);
}

// This test is commented out intentionally. Uncomment this test to ensure the correct behavior
// of each static assert. This is to ensure specefic behavior for the templated Append
/*
TEST(CompressedBuffer, StaticAssertTest) {

CompressedBuffer buf{1024, 32, 32};

// Assert compile time failure of adding a double
buf.Append(3.4);

// Assert a compile time failure of adding nothing
buf.Append();

// Assert a compile time failure of adding more that one sting type
buf.Append("Hello", "World");

// Assert a compile time failure of passing a Vector to Append
buf.Append(std::vector<int>());

// Assert a compile time failure of adding more than 6 uint8_t types
buf.Append(static_cast<uint8_t>(65), static_cast<uint8_t>(66), static_cast<uint8_t>(67),
static_cast<uint8_t>(68), static_cast<uint8_t>(69), static_cast<uint8_t>(70),
static_cast<uint8_t>(71));
}
*/

} // namespace
2 changes: 1 addition & 1 deletion spectator/smile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void SmilePayload::Append(double value) {
// Then split byte (one that crosses lo/hi int boundary), 7 bits
{
auto mid = static_cast<uint32_t>(l >> 28U);
buffer_.Append(mid & 0x7FU);
buffer_.Append(static_cast<uint8_t>(mid & 0x7FU));
}

// and then last 4 bytes (28 bits)
Expand Down
Loading