-
Notifications
You must be signed in to change notification settings - Fork 41
Optimize message memmoves #501
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
Merged
anarthal
merged 16 commits into
boostorg:develop
from
vsoulgard:feature/462-optimize-packet-read
Mar 29, 2026
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
554b9b1
Optimize message memmoving by predicting next message size
vsoulgard c807970
Fix calculator initial size
vsoulgard 2f096bd
Fix comparison
vsoulgard e498045
Fix comparasion
vsoulgard 7f1d198
Fix bug
vsoulgard 5c7f6cb
New solution
vsoulgard 09cb308
Grow read buffer to next power of two
vsoulgard ec8ba88
Add unit tests
vsoulgard 84a0556
Fix types
vsoulgard 3f931e3
Fix typo
vsoulgard 045ae72
Fix type
vsoulgard 42a504d
Fix msvc type warning
vsoulgard 04131b4
Add test to Jamfile
vsoulgard bb3120e
Fix another type comparison
vsoulgard c22bed3
Improve tests
vsoulgard 4a6028c
Merge branch 'boostorg:develop' into feature/462-optimize-packet-read
vsoulgard 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| // | ||
| // Copyright (c) 2026 Vladislav Soulgard (vsoulgard at gmail dot com) | ||
| // | ||
| // Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
| // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
| // | ||
|
|
||
| #ifndef BOOST_MYSQL_IMPL_INTERNAL_NEXT_POWER_OF_TWO_HPP | ||
| #define BOOST_MYSQL_IMPL_INTERNAL_NEXT_POWER_OF_TWO_HPP | ||
|
|
||
| #include <boost/assert.hpp> | ||
|
|
||
| #include <type_traits> | ||
| #include <limits> | ||
|
|
||
| namespace boost { | ||
| namespace mysql { | ||
| namespace detail { | ||
|
|
||
| template<int Shift, class UnsignedInt, bool Continue> | ||
| struct recursive_shift_or; | ||
|
|
||
| // Apply shift and continue to the next power of 2 | ||
| template<int Shift, class UnsignedInt> | ||
| struct recursive_shift_or<Shift, UnsignedInt, true> | ||
| { | ||
| static void apply(UnsignedInt& n) | ||
| { | ||
| n |= n >> Shift; | ||
| recursive_shift_or<Shift * 2, UnsignedInt, (Shift * 2 < sizeof(UnsignedInt) * 8)>::apply(n); | ||
| } | ||
| }; | ||
|
|
||
| // Stop recursion when shift exceeds type width | ||
| template<int Shift, class UnsignedInt> | ||
| struct recursive_shift_or<Shift, UnsignedInt, false> | ||
| { | ||
| static void apply(UnsignedInt&) {} | ||
| }; | ||
|
|
||
| // Returns the smallest power of two greater than or equal to n. | ||
| // Precondition: n must not exceed the largest power of two that fits | ||
| // in UnsignedInt. For example: | ||
| // - uint8_t: n <= 128 (2^7) | ||
| // - uint16_t: n <= 32768 (2^15) | ||
| // - uint32_t: n <= 2147483648 (2^31) | ||
| // - uint64_t: n <= 9223372036854775808 (2^63) | ||
| // | ||
| // Passing a larger value results in undefined behavior (overflow). | ||
| // In debug builds, this is caught by BOOST_ASSERT. | ||
| template<class UnsignedInt> | ||
| UnsignedInt next_power_of_two(UnsignedInt n) noexcept | ||
| { | ||
| static_assert(std::is_unsigned<UnsignedInt>::value, ""); | ||
| // Assert overflow (if value is bigger than maximum power) | ||
| BOOST_ASSERT(!(n > (std::numeric_limits<UnsignedInt>::max() >> 1) + 1)); | ||
| if (n == 0) return 1; | ||
| n--; | ||
| // Fill all lower bits | ||
| recursive_shift_or<1, UnsignedInt, (1 < sizeof(UnsignedInt) * 8)>::apply(n); | ||
| return n + 1; | ||
| } | ||
|
|
||
| } // namespace detail | ||
| } // namespace mysql | ||
| } // namespace boost | ||
|
|
||
| #endif |
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
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 |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| // | ||
| // Copyright (c) 2026 Vladislav Soulgard (vsoulgard at gmail dot com) | ||
| // | ||
| // Distributed under the Boost Software License, Version 1.0. (See accompanying | ||
| // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | ||
| // | ||
|
|
||
| #include <boost/mysql/impl/internal/next_power_of_two.hpp> | ||
|
|
||
| #include <boost/test/unit_test.hpp> | ||
|
|
||
| #include <cstdint> | ||
|
|
||
| using namespace boost::mysql::detail; | ||
|
|
||
| BOOST_AUTO_TEST_SUITE(test_next_power_of_two) | ||
|
|
||
| BOOST_AUTO_TEST_CASE(basic) | ||
| { | ||
| // n = 0 (special case) | ||
| BOOST_TEST(next_power_of_two<std::size_t>(0u) == 1u); | ||
|
|
||
| // n is already power of two | ||
| BOOST_TEST(next_power_of_two<std::size_t>(1u) == 1u); | ||
| BOOST_TEST(next_power_of_two<std::size_t>(2u) == 2u); | ||
| BOOST_TEST(next_power_of_two<std::size_t>(4u) == 4u); | ||
| BOOST_TEST(next_power_of_two<std::size_t>(8u) == 8u); | ||
| BOOST_TEST(next_power_of_two<std::size_t>(16u) == 16u); | ||
| BOOST_TEST(next_power_of_two<std::size_t>(32u) == 32u); | ||
| BOOST_TEST(next_power_of_two<std::size_t>(64u) == 64u); | ||
| BOOST_TEST(next_power_of_two<std::size_t>(128u) == 128u); | ||
|
|
||
| // n just below power of two | ||
| BOOST_TEST(next_power_of_two<std::size_t>(3u) == 4u); | ||
| BOOST_TEST(next_power_of_two<std::size_t>(7u) == 8u); | ||
| BOOST_TEST(next_power_of_two<std::size_t>(15u) == 16u); | ||
| BOOST_TEST(next_power_of_two<std::size_t>(31u) == 32u); | ||
| BOOST_TEST(next_power_of_two<std::size_t>(63u) == 64u); | ||
| BOOST_TEST(next_power_of_two<std::size_t>(127u) == 128u); | ||
|
|
||
| // n just above power of two | ||
| BOOST_TEST(next_power_of_two<std::size_t>(5u) == 8u); | ||
| BOOST_TEST(next_power_of_two<std::size_t>(9u) == 16u); | ||
| BOOST_TEST(next_power_of_two<std::size_t>(17u) == 32u); | ||
| BOOST_TEST(next_power_of_two<std::size_t>(33u) == 64u); | ||
| BOOST_TEST(next_power_of_two<std::size_t>(65u) == 128u); | ||
| BOOST_TEST(next_power_of_two<std::size_t>(129u) == 256u); | ||
|
|
||
| // n is random value | ||
| BOOST_TEST(next_power_of_two<std::size_t>(6u) == 8u); | ||
| BOOST_TEST(next_power_of_two<std::size_t>(13u) == 16u); | ||
| BOOST_TEST(next_power_of_two<std::size_t>(21u) == 32u); | ||
| BOOST_TEST(next_power_of_two<std::size_t>(45u) == 64u); | ||
| BOOST_TEST(next_power_of_two<std::size_t>(89u) == 128u); | ||
| BOOST_TEST(next_power_of_two<std::size_t>(200u) == 256u); | ||
| BOOST_TEST(next_power_of_two<std::size_t>(300u) == 512u); | ||
| BOOST_TEST(next_power_of_two<std::size_t>(400u) == 512u); | ||
| BOOST_TEST(next_power_of_two<std::size_t>(505u) == 512u); | ||
| BOOST_TEST(next_power_of_two<std::size_t>(888u) == 1024u); | ||
| } | ||
|
anarthal marked this conversation as resolved.
|
||
|
|
||
| BOOST_AUTO_TEST_CASE(different_types) | ||
| { | ||
| // uint8_t | ||
| BOOST_TEST(next_power_of_two<std::uint8_t>(0u) == 1u); | ||
| BOOST_TEST(next_power_of_two<std::uint8_t>(62u) == 64u); | ||
| BOOST_TEST(next_power_of_two<std::uint8_t>(100u) == 128u); | ||
| BOOST_TEST(next_power_of_two<std::uint8_t>(128u) == 128u); | ||
|
|
||
| // uint16_t | ||
| BOOST_TEST(next_power_of_two<std::uint16_t>(0u) == 1u); | ||
| BOOST_TEST(next_power_of_two<std::uint16_t>(1000u) == 1024u); | ||
| BOOST_TEST(next_power_of_two<std::uint16_t>(16383u) == 16384u); | ||
| BOOST_TEST(next_power_of_two<std::uint16_t>(32768u) == 32768u); | ||
|
|
||
| // uint32_t | ||
| BOOST_TEST(next_power_of_two<std::uint32_t>(0u) == 1u); | ||
| BOOST_TEST(next_power_of_two<std::uint32_t>(100000u) == 131072u); | ||
| BOOST_TEST(next_power_of_two<std::uint32_t>(1u << 30) == 1u << 30); | ||
| BOOST_TEST(next_power_of_two<std::uint32_t>((1u << 30) + 1) == 1u << 31); | ||
|
|
||
| // uint64_t | ||
| BOOST_TEST(next_power_of_two<std::uint64_t>(0u) == 1u); | ||
| BOOST_TEST(next_power_of_two<std::uint64_t>(1ull << 40) == 1ull << 40); | ||
| BOOST_TEST(next_power_of_two<std::uint64_t>((1ull << 40) + 1) == 1ull << 41); | ||
| BOOST_TEST(next_power_of_two<std::uint64_t>(1ull << 62) == 1ull << 62); | ||
| BOOST_TEST(next_power_of_two<std::uint64_t>((1ull << 62) + 1) == 1ull << 63); | ||
| } | ||
|
|
||
| BOOST_AUTO_TEST_SUITE_END() | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.