Comments (4)
Sounds like a great solution!
from cista.
In general, unaligned memory access is undefined behavior (UB) in C++ and we try to avoid UB as much as possible. Some architectures (e.g. x86) can handle unaligned access for most instructions. However, even with x86 you need to be careful if you want to use SIMD instruction sets like SSE or AVX. You cannot be sure where the compiler tries to apply those instructions if you enable them. So the only safe thing to do, is to make sure you only access memory in an aligned way. This way, you're also future-proof for the ARM or ARM64 instruction sets which are more strict with regard to alignment.
In your case, I would probably think about making sure that
- the buffer you deserialize with cista is aligned to something like
alignof(std::max_align_t)
inside the buffer transmitted over the network - the buffer transmitted over the network starts at an address that's aligned to
alignof(std::max_align_t)
If you cannot make sure that memory access is always aligned, I would probably try to use something like FlatBuffers or Cap'n'Proto because these make sure that scalars are std::memcpy
'ed before access to prevent UB.
If you're only developing for a very specific hardware architecture that does not and will not have any alignment requirements, it would probably make sense to add a flag to cista that allows you to disable only the alignment checks.
from cista.
I see, thanks for the info.
As I understand then, copying the unaligned string before deserializing is a functional workaround because it gives Cista
correctly aligned data to work with. I can't think of a way to achieve the right offset without modifying the networking library or copying the data.
As far as the custom flag goes, it works but like you said it's not future-proof.
I will see if I can find a way to get the networking lib (uWebSockets
) to align the memory properly.
Luckily an extra copy is likely not too big of a slowdown in my case, I will have to profile it to determine the true importance.
from cista.
I ended up implementing a compression layer between Cista
and the networking library, which amounts to a copy anyways.
This allows for minimal overhead and a correct alignment.
from cista.
Related Issues (20)
- Feature request: builtin-in serialize and type_hash support for std::array HOT 6
- Discussion: STL containers can actually be supported ? HOT 3
- Help: Serialization and inheritance (with standard layout classes) HOT 22
- Wiki Typo HOT 1
- Any plans for c++20? HOT 2
- cista_members() approach: serialize tuples properly HOT 2
- nigiri Branch entfernt HOT 1
- Documentation request: how to build and run tests HOT 6
- Silence clang specific #pragma directives for other compilers ? HOT 2
- Static type hashing of std::complex HOT 5
- Create a stable release branch HOT 5
- Missing const& specifier for return type
- Cleanup: static_cast for `adjustment` should be `offset_t` since `start` is that type
- Support storing trailing \0 byte at the end of string HOT 5
- Support for `resize` [perhaps also `capacity` and `reserve`] for cista::generic_string/basic_string HOT 2
- String gives different size at "empty" input for short/non_short HOT 2
- MSVC v14.1x build error: C2296,C2297 HOT 3
- How can I serialize a struct which contains anonymous union member HOT 2
- Bug in std_vector_test.cc HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cista.