diff --git a/include/bitsery/adapter/buffer.h b/include/bitsery/adapter/buffer.h index c392f53..47a8678 100644 --- a/include/bitsery/adapter/buffer.h +++ b/include/bitsery/adapter/buffer.h @@ -269,12 +269,11 @@ class OutputBufferAdapter void maybeResize(size_t newOffset, std::true_type) { - if (newOffset > _bufferSize) { - traits::BufferAdapterTraits::increaseBufferSize( - *_buffer, _currOffset, newOffset); - _beginIt = std::begin(*_buffer); - _bufferSize = traits::ContainerTraits::size(*_buffer); - } + if (newOffset > _bufferSize) + BITSERY_UNLIKELY + { + doResize(newOffset); + } } void maybeResize(BITSERY_MAYBE_UNUSED size_t newOffset, std::false_type) @@ -289,6 +288,14 @@ class OutputBufferAdapter std::copy_n(data, size, _beginIt + static_cast(_currOffset)); _currOffset = newOffset; } + + BITSERY_NOINLINE void doResize(size_t newOffset) + { + traits::BufferAdapterTraits::increaseBufferSize( + *_buffer, _currOffset, newOffset); + _beginIt = std::begin(*_buffer); + _bufferSize = traits::ContainerTraits::size(*_buffer); + } }; } diff --git a/include/bitsery/bitsery.h b/include/bitsery/bitsery.h index 3d38bce..378b3ea 100644 --- a/include/bitsery/bitsery.h +++ b/include/bitsery/bitsery.h @@ -36,12 +36,73 @@ BITSERY_BUILD_VERSION_STR( \ BITSERY_MAJOR_VERSION, BITSERY_MINOR_VERSION, BITSERY_PATCH_VERSION) -#if __cplusplus > 201402L -#define BITSERY_MAYBE_UNUSED [[maybe_unused]] +#define BITSERY_DO_PRAGMA(x) _Pragma(#x) +#ifdef __GNUC__ +#define BITSERY_DISABLE_WARNINGS(...) \ + BITSERY_DO_PRAGMA(GCC diagnostic push) \ + BITSERY_DO_PRAGMA(GCC diagnostic ignored __VA_ARGS__) +#define BITSERY_ENABLE_WARNINGS() BITSERY_DO_PRAGMA(GCC diagnostic pop) +#elif defined(_MSC_VER) +#define BITSERY_DISABLE_WARNINGS(...) \ + BITSERY_DO_PRAGMA(GCC diagnostic push) \ + BITSERY_DO_PRAGMA(GCC diagnostic ignored __VA_ARGS__) \ + BITSERY_DO_PRAGMA(GCC diagnostic pop) +#define BITSERY_ENABLE_WARNINGS() BITSERY_DO_PRAGMA(GCC diagnostic pop) +#else +#define BITSERY_DISABLE_WARNINGS(...) +#define BITSERY_ENABLE_WARNINGS() +#endif + +#ifdef __clang__ +#define BITSERY_ATTRIBUTE(...) \ + BITSERY_DISABLE_WARNINGS("-Wfuture-attribute-extensions") \ + [[__VA_ARGS__]] BITSERY_ENABLE_WARNINGS() +#elif defined(__GNUC__) +#define BITSERY_ATTRIBUTE(...) [[__VA_ARGS__]] +#elif defined(_MSC_VER) +#define BITSERY_ATTRIBUTE(...) [[__VA_ARGS__]] +#else +#define BITSERY_ATTRIBUTE(...) [[__VA_ARGS__]] +#endif + +#if __has_cpp_attribute(likely) +#define BITSERY_LIKELY BITSERY_ATTRIBUTE(likey) +#else +#define BITSERY_LIKELY +#endif + +#if __has_cpp_attribute(unlikely) +#define BITSERY_UNLIKELY BITSERY_ATTRIBUTE(unlikely) +#else +#define BITSERY_UNLIKELY +#endif + +#if __has_cpp_attribute(maybe_unused) +#define BITSERY_MAYBE_UNUSED BITSERY_ATTRIBUTE(maybe_unused) #else #define BITSERY_MAYBE_UNUSED #endif +#if __GNUC__ +#define BITSERY_NOINLINE __attribute__((noinline)) +#elif defined(_MSC_VER) +#define BITSERY_NOINLINE __declspec(noinline) +#else +#define BITSERY_NOINLINE +#endif + +#if __GNUC__ +#define BITSERY_ASSUME(cond) \ + do { \ + if (!(cond)) \ + __builtin_unreachable(); \ + } while (0) +#elif defined(_MSC_VER) +#define BITSERY_ASSUME(cond) __assume(cond) +#else +#define BITSERY_ASSUME(cond) +#endif + #include "deserializer.h" #include "serializer.h" diff --git a/include/bitsery/traits/core/std_defaults.h b/include/bitsery/traits/core/std_defaults.h index 20cd5ef..748cdc3 100644 --- a/include/bitsery/traits/core/std_defaults.h +++ b/include/bitsery/traits/core/std_defaults.h @@ -23,6 +23,7 @@ #ifndef BITSERY_TRAITS_CORE_STD_DEFAULTS_H #define BITSERY_TRAITS_CORE_STD_DEFAULTS_H +#include "../../bitsery.h" #include "../../details/serialization_common.h" #include "traits.h" @@ -103,8 +104,11 @@ struct StdContainerForBufferAdapter static_cast(static_cast(container.size()) * 1.5) + 128; // make data cache friendly newSize -= newSize % 64; // 64 is cache line size - container.resize( - (std::max)(newSize > minSize ? newSize : minSize, container.capacity())); + auto resize = + (std::max)(newSize > minSize ? newSize : minSize, container.capacity()); + BITSERY_ASSUME(resize >= container.size()); + BITSERY_ASSUME(resize >= container.capacity()); + container.resize(resize); } };