Repository navigation
feat(clp-s): Add support for losslessly compressing floating-point numbers; Bump archive format version to v0.4.1. #1176
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
Changes from 15 commits
2c0a7e3
91b499c
41eee1a
1a3ef60
e4bca46
1c2d47e
32a5375
f6f58be
6bc94cc
8724e11
1f33f98
15e4346
e5717ef
a4cbd89
30c9b08
c56275e
0090052
38d5ae4
87a131f
287b43e
283ce61
b0d083d
3e18f00
bee4fca
71f2fb6
e6ac492
42347c1
284027c
3870058
eff80ce
03506bc
4da57f3
e8db268
5a385c6
c43884d
b6b364a
e72ea13
c0982e7
464f3b6
5425f84
cfc3740
98e0390
b5c1987
091a3fc
731bf83
1d3fd9f
bf9cfb0
18e5aaf
af19cc6
ab38a34
73da7f3
fcea117
7ed2c91
bf9b6cb
0197735
c1bf459
eb9a711
ef79776
05fd52c
1c2a209
d4ef78b
86ed676
c1b2960
c38d22a
0d4b607
0e15df7
6813e1e
200a320
dafb471
f9725db
7c0d698
a2a31b0
b997d17
b8e2d50
2f70218
3894a4d
b37df69
89e9a1f
903e6c2
46983ae
0aca1e2
7da0c1d
5fbdd07
cda2121
f6e1024
2f25beb
66a7e8e
1504898
c0b7e84
c0d6f4c
29fcbf2
777f126
bb46862
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,6 +50,11 @@ void FloatColumnReader::load(BufferViewReader& reader, uint64_t num_messages) { | |
| m_values = reader.read_unaligned_span<double>(num_messages); | ||
| } | ||
|
|
||
| void FormattedFloatColumnReader::load(BufferViewReader& reader, uint64_t num_messages) { | ||
| m_values = reader.read_unaligned_span<double>(num_messages); | ||
| m_format = reader.read_unaligned_span<uint16_t>(num_messages); | ||
| } | ||
|
anlowee marked this conversation as resolved.
Resolved
|
||
|
|
||
| void | ||
| Int64ColumnReader::extract_string_value_into_buffer(uint64_t cur_message, std::string& buffer) { | ||
| buffer.append(std::to_string(m_values[cur_message])); | ||
|
|
@@ -68,6 +73,12 @@ std::variant<int64_t, double, std::string, uint8_t> FloatColumnReader::extract_v | |
| return m_values[cur_message]; | ||
| } | ||
|
|
||
| std::variant<int64_t, double, std::string, uint8_t> FormattedFloatColumnReader::extract_value( | ||
| uint64_t cur_message | ||
| ) { | ||
| return m_values[cur_message]; | ||
| } | ||
|
|
||
| void BooleanColumnReader::load(BufferViewReader& reader, uint64_t num_messages) { | ||
| m_values = reader.read_unaligned_span<uint8_t>(num_messages); | ||
| } | ||
|
|
@@ -77,6 +88,154 @@ FloatColumnReader::extract_string_value_into_buffer(uint64_t cur_message, std::s | |
| buffer.append(std::to_string(m_values[cur_message])); | ||
| } | ||
|
|
||
| void FormattedFloatColumnReader::extract_string_value_into_buffer( | ||
| uint64_t cur_message, | ||
| std::string& buffer | ||
| ) { | ||
| buffer.append(restore_format(cur_message)); | ||
| } | ||
|
Contributor
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. 🧹 Nitpick (assertive) Optional: avoid intermediate allocation in string extraction
🤖 Prompt for AI Agents |
||
|
|
||
| std::string FormattedFloatColumnReader::restore_format(uint64_t cur_message) { | ||
| std::ostringstream oss; | ||
| uint16_t const significant_digits | ||
| = (m_format[cur_message] >> float_format_encoding::cSignificantDigitsPos & 0x0F) + 1; | ||
| oss << std::scientific << std::setprecision(significant_digits - 1); | ||
| if (m_format[cur_message] & (1 << float_format_encoding::cScientificExponentNotePos)) { | ||
| if (m_format[cur_message] & (1 << (float_format_encoding::cScientificExponentNotePos + 1))) | ||
| { | ||
| oss << std::uppercase; | ||
| } | ||
| oss << m_values[cur_message]; | ||
| auto formatted_double_str = oss.str(); | ||
| auto const exp_pos = formatted_double_str.find_first_of("Ee"); | ||
| char const maybe_sign = formatted_double_str[exp_pos + 1]; | ||
| uint16_t const exp_digits | ||
| = (m_format[cur_message] >> float_format_encoding::cScientificExponentDigitsPos | ||
| & 0x03) | ||
| + 1; | ||
| if (0b00 << float_format_encoding::cScientificExponentSignPos | ||
| == (m_format[cur_message] & 0b11 << float_format_encoding::cScientificExponentSignPos)) | ||
| { | ||
| if ('+' == maybe_sign || '-' == maybe_sign) { | ||
| formatted_double_str.erase(exp_pos + 1, 1); | ||
| } | ||
| if (exp_digits < (formatted_double_str.length() - exp_pos - 1)) { | ||
| formatted_double_str = trim_leading_zeros( | ||
| formatted_double_str, | ||
| exp_pos + 1, | ||
| formatted_double_str.length() - exp_pos - 1 - exp_digits | ||
| ); | ||
| } else { | ||
| formatted_double_str.insert( | ||
| exp_pos + 1, | ||
| exp_digits - (formatted_double_str.length() - exp_pos - 1), | ||
| '0' | ||
| ); | ||
| } | ||
| } else { | ||
| if (exp_digits < (formatted_double_str.length() - exp_pos - 2)) { | ||
| formatted_double_str = trim_leading_zeros( | ||
| formatted_double_str, | ||
| exp_pos + 2, | ||
| formatted_double_str.length() - exp_pos - 2 - exp_digits | ||
| ); | ||
| } else { | ||
| formatted_double_str.insert( | ||
| exp_pos + 2, | ||
| exp_digits - (formatted_double_str.length() - exp_pos - 2), | ||
| '0' | ||
| ); | ||
| } | ||
| if (0b01 << float_format_encoding::cScientificExponentSignPos | ||
| == (m_format[cur_message] | ||
| & 0b11 << float_format_encoding::cScientificExponentSignPos)) | ||
| { | ||
| if (std::isdigit(maybe_sign)) { | ||
| formatted_double_str.insert(exp_pos + 1, "+"); | ||
| } else { | ||
| formatted_double_str[exp_pos + 1] = '+'; | ||
| } | ||
| } else if (0b10 << float_format_encoding::cScientificExponentSignPos | ||
| == (m_format[cur_message] | ||
| & 0b11 << float_format_encoding::cScientificExponentSignPos)) | ||
| { | ||
| if (std::isdigit(maybe_sign)) { | ||
| formatted_double_str.insert(exp_pos + 1, "-"); | ||
| } else { | ||
| formatted_double_str[exp_pos + 1] = '-'; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return formatted_double_str; | ||
| } | ||
|
|
||
| // Convert the scientific notation to the standard decimal | ||
| oss << m_values[cur_message]; | ||
| return scientific_to_decimal(oss.str()); | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| std::string FormattedFloatColumnReader::trim_leading_zeros( | ||
| std::string_view scientific_notation, | ||
| size_t start, | ||
| size_t number_of_zeros_to_trim | ||
| ) { | ||
| auto sci_str = std::string(scientific_notation); | ||
| size_t actual_number_of_zeros_to_trim{0}; | ||
| for (size_t i{start}; i < start + number_of_zeros_to_trim; ++i) { | ||
| if ('0' == sci_str[i]) { | ||
| actual_number_of_zeros_to_trim++; | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
| sci_str.erase(start, actual_number_of_zeros_to_trim); | ||
| return sci_str; | ||
| } | ||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||
|
|
||
| std::string FormattedFloatColumnReader::scientific_to_decimal( | ||
| std::string_view scientific_notation | ||
| ) { | ||
| auto sci_str = std::string(scientific_notation); | ||
| bool isNegative = false; | ||
| if (false == std::isdigit(sci_str[0])) { | ||
| isNegative = true; | ||
| sci_str.erase(0, 1); | ||
| } | ||
| size_t const exp_pos = sci_str.find_first_of("e"); | ||
| assert(std::string::npos != exp_pos); | ||
|
|
||
| // Split into mantissa and exponent parts | ||
| std::string mantissa_str = sci_str.substr(0, exp_pos); | ||
| int const exponent = std::stoi(sci_str.substr(exp_pos + 1)); | ||
|
|
||
| // Remove the decimal point from the mantissa | ||
| size_t const dot_pos = mantissa_str.find('.'); | ||
| std::string digits; | ||
| if (dot_pos != std::string::npos) { | ||
| digits = mantissa_str.substr(0, dot_pos) + mantissa_str.substr(dot_pos + 1); | ||
| } else { | ||
| digits = mantissa_str; | ||
| } | ||
|
|
||
| // Adjust position of decimal point based on exponent | ||
| int const decimal_pos = static_cast<int>(dot_pos) + exponent; | ||
|
|
||
| std::string result{""}; | ||
| if (isNegative) { | ||
| result = "-"; | ||
| } | ||
| if (decimal_pos <= 0) { | ||
| result += "0." + std::string(-decimal_pos, '0') + digits; | ||
| } else if (decimal_pos < static_cast<int>(digits.size())) { | ||
| result += digits.substr(0, decimal_pos) + "." + digits.substr(decimal_pos); | ||
| } else { | ||
| result += digits + std::string(decimal_pos - digits.size(), '0'); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
anlowee marked this conversation as resolved.
Resolved
anlowee marked this conversation as resolved.
Resolved
|
||
|
|
||
| std::variant<int64_t, double, std::string, uint8_t> BooleanColumnReader::extract_value( | ||
| uint64_t cur_message | ||
| ) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -147,6 +147,66 @@ class FloatColumnReader : public BaseColumnReader { | |||||
| UnalignedMemSpan<double> m_values; | ||||||
| }; | ||||||
|
|
||||||
| class FormattedFloatColumnReader : public BaseColumnReader { | ||||||
| public: | ||||||
| // Constructor | ||||||
| explicit FormattedFloatColumnReader(int32_t id) : BaseColumnReader(id) {} | ||||||
|
|
||||||
| // Destructor | ||||||
| ~FormattedFloatColumnReader() override = default; | ||||||
|
|
||||||
| // Methods inherited from BaseColumnReader | ||||||
| void load(BufferViewReader& reader, uint64_t num_messages) override; | ||||||
|
|
||||||
| NodeType get_type() override { return NodeType::FormattedFloat; } | ||||||
|
|
||||||
| std::variant<int64_t, double, std::string, uint8_t> extract_value( | ||||||
| uint64_t cur_message | ||||||
| ) override; | ||||||
|
|
||||||
| void extract_string_value_into_buffer(uint64_t cur_message, std::string& buffer) override; | ||||||
|
|
||||||
| /** | ||||||
| * Decodes the format information and generates the string of the stored double value with the | ||||||
| * decoded format. | ||||||
| * | ||||||
| * @param cur_message | ||||||
| * @return The double value string formatted by the format information. | ||||||
| */ | ||||||
| std::string restore_format(uint64_t cur_message); | ||||||
|
|
||||||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||||||
| private: | ||||||
| UnalignedMemSpan<double> m_values; | ||||||
| UnalignedMemSpan<uint16_t> m_format; | ||||||
|
Contributor
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.
Suggested change
|
||||||
|
|
||||||
|
anlowee marked this conversation as resolved.
Resolved
|
||||||
| /** | ||||||
| * Trims the leading zeros until the number of exponent digits match the value stored in the | ||||||
| * format. However, if it encounters non-zero digit when trimming, the trim will be aborted to | ||||||
| * avoid incorrect double value string. | ||||||
| * | ||||||
| * @param scientific_notation The scientific notation string generated by std::scientific. | ||||||
| * @param start The start position of trimming. It could be either right after the exponent | ||||||
| * note (E or e), or the second char after the exponent note if the exponent has a sign. | ||||||
| * @param number_of_zeros_to_trim The difference between current number of exponent digits and | ||||||
| * the target value stored in the format. | ||||||
| * @return The scientific notation string with the exponent being trimmed leading zeros. | ||||||
| */ | ||||||
| static std::string trim_leading_zeros( | ||||||
| std::string_view scientific_notation, | ||||||
| size_t start, | ||||||
| size_t number_of_zeros_to_trim | ||||||
| ); | ||||||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||||||
|
|
||||||
| /** | ||||||
| * Convert the scientific notation string to a double value string formatted by the encoded | ||||||
| * format information. | ||||||
| * | ||||||
| * @param scientific_notation The scientific notation string generated by std::scientific. | ||||||
| * @return The double value string formatted by the format information. | ||||||
| */ | ||||||
| static std::string scientific_to_decimal(std::string_view scientific_notation); | ||||||
| }; | ||||||
|
anlowee marked this conversation as resolved.
Resolved
|
||||||
|
|
||||||
| class BooleanColumnReader : public BaseColumnReader { | ||||||
| public: | ||||||
| // Constructor | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -46,6 +46,94 @@ void FloatColumnWriter::store(ZstdCompressor& compressor) { | |||||
| compressor.write(reinterpret_cast<char const*>(m_values.data()), size); | ||||||
| } | ||||||
|
|
||||||
| size_t FormattedFloatColumnWriter::add_value(ParsedMessage::variable_t& value) { | ||||||
|
anlowee marked this conversation as resolved.
Resolved
|
||||||
| auto float_str = std::get<std::string>(value); | ||||||
|
|
||||||
| // Trim the raw string | ||||||
| float_str.erase( | ||||||
|
anlowee marked this conversation as resolved.
Resolved
|
||||||
| std::remove_if( | ||||||
| float_str.begin(), | ||||||
| float_str.end(), | ||||||
| [](char c) { | ||||||
| return !std::isdigit(static_cast<unsigned char>(c)) && '.' != c && '+' != c | ||||||
| && '-' != c && 'E' != c && 'e' != c; | ||||||
| } | ||||||
| ), | ||||||
| float_str.end() | ||||||
| ); | ||||||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||||||
|
|
||||||
| // If the raw JSON token is an illegal double number then here will throw exception | ||||||
| m_values.push_back(std::stod(float_str)); | ||||||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
Contributor
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. This is fine as defensive programming, but just to confirm this shouldn't ever throw given the way we're currently parsing number values right? I would think that if simdjson claims something is a double it is guaranteed to be valid, but maybe that's not right.
Contributor
Author
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. true |
||||||
|
|
||||||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||||||
| auto const dot_pos = float_str.find('.'); | ||||||
| uint16_t format{0}; | ||||||
|
|
||||||
| // Check whether it is the scientific; if so, if the exponent is E or e | ||||||
|
Contributor
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.
Suggested change
|
||||||
| size_t exp_pos = float_str.find_first_of("Ee"); | ||||||
| if (std::string::npos != exp_pos) { | ||||||
| format |= 1 << float_format_encoding::cScientificExponentNotePos; | ||||||
| format |= ('E' == float_str[exp_pos]) | ||||||
| << (float_format_encoding::cScientificExponentNotePos + 1); | ||||||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||||||
|
|
||||||
| // Check whether there is a sign for the exponent | ||||||
| if ('+' == float_str[exp_pos + 1]) { | ||||||
| format |= 1 << float_format_encoding::cScientificExponentSignPos; | ||||||
| } else if ('-' == float_str[exp_pos + 1]) { | ||||||
| format |= 1 << (float_format_encoding::cScientificExponentSignPos + 1); | ||||||
| } | ||||||
|
|
||||||
| // Set the number of exponent digits | ||||||
| int exp_digits = float_str.length() - exp_pos - 1; | ||||||
| if (false == std::isdigit(float_str[exp_pos + 1])) { | ||||||
| exp_digits--; | ||||||
| } | ||||||
| format |= (static_cast<uint16_t>(std::min(exp_digits - 1, 3)) & 0x03) | ||||||
| << float_format_encoding::cScientificExponentDigitsPos; | ||||||
|
anlowee marked this conversation as resolved.
Resolved
|
||||||
| } else { | ||||||
| exp_pos = float_str.length(); | ||||||
| } | ||||||
|
|
||||||
| // According to the JSON grammar, there is no leading zeros for the integer part of a number, | ||||||
| // so we can check whether the first or the second (if the there is a sign) digit is 0 to know | ||||||
| // if the first non-zero number is in integer part. | ||||||
| size_t first_non_zero_frac_digit_pos = std::isdigit(float_str[0]) ? 0 : 1; | ||||||
| if (std::isdigit(float_str[0]) && '0' != float_str[0]) { | ||||||
| first_non_zero_frac_digit_pos = 0; | ||||||
| } else if (std::isdigit(float_str[1]) && '0' != float_str[1]) { | ||||||
| first_non_zero_frac_digit_pos = 1; | ||||||
| } else if (std::string::npos != dot_pos) { | ||||||
| for (size_t i = dot_pos + 1; i < float_str.length(); ++i) { | ||||||
| if (false == std::isdigit(float_str[i])) { | ||||||
| break; | ||||||
| } else if ('0' != float_str[i]) { | ||||||
| first_non_zero_frac_digit_pos = i; | ||||||
| break; | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
|
||||||
|
|
||||||
| int significant_digits = exp_pos - first_non_zero_frac_digit_pos; | ||||||
| if (std::string::npos != dot_pos && first_non_zero_frac_digit_pos < dot_pos) { | ||||||
| significant_digits--; | ||||||
| } | ||||||
| assert(significant_digits > 0); | ||||||
| uint16_t const compressed_significant_digits | ||||||
| = static_cast<uint16_t>(std::min(significant_digits - 1, 15)) & 0x0F; | ||||||
|
|
||||||
| format |= compressed_significant_digits << float_format_encoding::cSignificantDigitsPos; | ||||||
|
|
||||||
| m_format.push_back(format); | ||||||
| return sizeof(double) + sizeof(uint16_t); | ||||||
| } | ||||||
|
coderabbitai[bot] marked this conversation as resolved.
Resolved
anlowee marked this conversation as resolved.
Resolved
|
||||||
|
|
||||||
| void FormattedFloatColumnWriter::store(ZstdCompressor& compressor) { | ||||||
| assert(m_format.size() == m_values.size()); | ||||||
| auto const values_size = m_values.size() * sizeof(double); | ||||||
| auto const format_size = m_format.size() * sizeof(uint16_t); | ||||||
| compressor.write(reinterpret_cast<char const*>(m_values.data()), values_size); | ||||||
| compressor.write(reinterpret_cast<char const*>(m_format.data()), format_size); | ||||||
| } | ||||||
|
|
||||||
| size_t BooleanColumnWriter::add_value(ParsedMessage::variable_t& value) { | ||||||
| m_values.push_back(std::get<bool>(value) ? 1 : 0); | ||||||
| return sizeof(uint8_t); | ||||||
|
|
||||||
Uh oh!
There was an error while loading. Please reload this page.