perf(portable_binary): skip byte-swap for DataSize==1 - #888
Open
type-name-T wants to merge 1 commit into
Open
Conversation
Single-byte types (e.g. std::vector<std::uint8_t>, std::vector<char>, std::vector<std::byte>) have nothing to swap, but still entered the per-byte slow path in saveBinary/loadBinary when itsConvertEndianness was set. - saveBinary: avoided O(n) single-byte sputn calls; now uses the bulk sputn branch directly. - loadBinary: skipped the empty swap_bytes<1> loop iteration overhead. DataSize is a non-type template parameter, so `DataSize > 1` is a compile-time constant. The dead branch is eliminated by the optimizer, leaving multi-byte types (uint16/32/64, float, double) with zero added runtime cost and byte-identical behavior.
|
您的邮件我已收到,谢谢合作!
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Problem
When serializing a
std::vectorof a single-byte arithmetic type (e.g.std::vector<std::uint8_t>,std::vector<char>,std::vector<unsigned char>) throughPortableBinaryArchivewith endianness conversion enabled, the data still enters the byte-swapping slow path even though swapping a single byte is a no-op.In
vector.hpp, arithmetic vectors use thebinary_datafast path:ar( binary_data( vector.data(), vector.size() * sizeof(T) ) );This dispatches to
saveBinary<sizeof(T)>/loadBinary<sizeof(T)>. Forsizeof(T) == 1,DataSizeis1, so:swap_bytes<1>iterates0times — a no-op the compiler can eliminatesaveBinary: O(n) single-bytesputn(..., 1)calls instead of one bulksputnloadBinary: O(n) empty swap-loop iterationsFix
Gate the byte-swap path behind
DataSize > 1in both functions:Why this is safe
swap_bytes<1>iterates0times), so skipping it produces byte-identical output forDataSize == 1.DataSizeis a non-type template parameter, soDataSize > 1is a compile-time constant. Compilers eliminate the dead branch, leaving theDataSize > 1path identical to before.uint16/32/64,float,double, etc.).Performance impact
For
std::vector<std::uint8_t>(or any 1-byte vector) serialized with endianness conversion enabled:sputncalls into one bulksputnof the whole buffer.Files changed
include/cereal/archives/portable_binary.hpp— 2 lines modified (bothifconditions).