Skip to content

Add functions to serialize to and deserialize from memory buffers#18

Merged
dime10 merged 21 commits into
mainfrom
improve-cmake
May 5, 2026
Merged

Add functions to serialize to and deserialize from memory buffers#18
dime10 merged 21 commits into
mainfrom
improve-cmake

Conversation

@denialhaag
Copy link
Copy Markdown
Collaborator

@denialhaag denialhaag commented Apr 30, 2026

Fixes #6
Fixes #8

@denialhaag denialhaag self-assigned this Apr 30, 2026
@denialhaag denialhaag changed the title Improve CMake setup Add functions to serialize to and deserialize from memory buffers Apr 30, 2026
@denialhaag denialhaag marked this pull request as ready for review April 30, 2026 17:44
@denialhaag denialhaag requested review from burgholzer and dime10 April 30, 2026 17:45
Comment thread cmake/ExternalDependencies.cmake Outdated
@denialhaag
Copy link
Copy Markdown
Collaborator Author

denialhaag commented Apr 30, 2026

Some context for @dime10: Our round-trip tests in MQT Core didn't serialize and deserialize the converted jeff programs so far. I'm changing this in munich-quantum-toolkit/core#1676 (the PR is +11/-6 🎉). This PR contains all the necessary changes to enable this, but the changes should not be MQT-specific.

Comment thread lib/Translation/CMakeLists.txt Outdated
Comment thread lib/Translation/Serialize.cpp Outdated
writeMessage(module, message);
capnp::writeMessage(output, message);
auto bytes = serialize(module);
output.write(reinterpret_cast<const char*>(bytes.data()), bytes.size());
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The split is definitely good, but I think we are introducing 2 copies here to write to file? The messageToFlatArray and then again to SmallVector, before then going to file.

Some references that might be relevant:
https://groups.google.com/g/capnproto/c/NBayPDP0_84/m/1meBteW2CAAJ
https://capnproto.org/cxx.html#messages-and-io

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@burgholzer I might be interesting if you could take a quick look at these links and contrast to your approach. If I understand your current code correctly, we are making one copy to serialize, and no copies to deserialize? (not including file i/o)

I suppose in theory one could use the memory buffer of the message builder directly for a zero copy approach, or could that pose issues?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did another iteration on the serialization and deserialization code.
I think both serialization and deserialization should now be zero copy.
Maybe you could have another look 👀

Comment thread lib/Translation/Serialize.cpp Outdated
Comment thread lib/Translation/Serialize.cpp Outdated
@burgholzer
Copy link
Copy Markdown
Collaborator

Sorry @dime10 for wasting some of your time. I was in the middle of working on the PR and did not notice your review in the middle of it.
I think all of your comments should be addressed in the new version though.

@dime10
Copy link
Copy Markdown
Collaborator

dime10 commented May 4, 2026

Sorry @dime10 for wasting some of your time. I was in the middle of working on the PR and did not notice your review in the middle of it. I think all of your comments should be addressed in the new version though.

No worries, I think we just happened to have worked on it at the same time 😅 it's all good

Copy link
Copy Markdown
Collaborator

@dime10 dime10 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot @burgholzer for those improvements!

Comment thread cmake/CTestCustom.cmake.in Outdated
Comment thread cmake/ExternalDependencies.cmake
Comment thread lib/Translation/Deserialize.cpp
Comment thread lib/Translation/Deserialize.cpp Outdated
Comment thread lib/Translation/Serialize.cpp Outdated
Comment thread cmake/CTestCustom.cmake.in Outdated
@burgholzer burgholzer requested a review from dime10 May 4, 2026 23:57
burgholzer added 4 commits May 5, 2026 02:21
Signed-off-by: Lukas Burgholzer <burgholzer@me.com>
Signed-off-by: Lukas Burgholzer <burgholzer@me.com>
Signed-off-by: Lukas Burgholzer <burgholzer@me.com>
@burgholzer
Copy link
Copy Markdown
Collaborator

Alright, this is now finally also working on Windows in munich-quantum-toolkit/core#1676

At some point, we may want to consider also expanding the CI matrix here to catch such failures earlier.

@@ -1,5 +1,8 @@

+# Disable vendored Cap'n Proto tests when consumed through FetchContent.
+set(BUILD_TESTING OFF)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's just a matter of the flag, can we not set it from our side?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately not. The project itself calls include(CTest), which unconditionally sets that variable again and overrides anything that we set here.
I have probably tried 5 different ways to exclude the tests; this is the one that worked reliably.

Mid- to long-term, one could contribute an upstream fix for this that scopes the testing to the project.
The sensible default is typically to enable the tests by default if running as the top-level CMake project (using PROJECT_IS_TOP_LEVEL) and to provide an additional project-specific option in CMake that defaults to OFF for downstream consumers.
I currently do not have the capacity to drive that through, but maybe @denialhaag has once he is back from vacation.

@dime10 dime10 merged commit 4002596 into main May 5, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve CMake setup Add functions to serialize to and deserialize from memory buffers

3 participants