Giter Club home page Giter Club logo

Comments (4)

felixguendling avatar felixguendling commented on June 9, 2024 1

Sounds like a great solution!

from cista.

felixguendling avatar felixguendling commented on June 9, 2024

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.

Eshnek avatar Eshnek commented on June 9, 2024

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.

Eshnek avatar Eshnek commented on June 9, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.