Repository navigation
feat(cpp): IStreamInto decodes into a message the caller owns - #197
Merged
Merged
Conversation
IStreamObject holds the message it decodes into, so a caller that already has one -- which every generated try_decode on this profile does -- can only decode into the stream's instance and copy it across afterwards. On a heap-free target that is a second whole message on the stack of every decode plus a copy of it: 1096 B on the arena's example message (sofa-buffers/generator#744). IStreamInto is IStreamObject with the instance taken out: it binds the message handed to its constructor, through the same C-ABI field callback and the same two assignments, and holds a pointer where IStreamObject holds a MessageType. There is no std::function anywhere near it -- that is what kept the generator staging on this profile, since IStreamInline has one and freestanding has no std::function at all -- so the decoder that writes into the caller's object costs no more dispatch than the one that owns it. What it buys is paid for in the contract, and the doc says so: the bound object is written as the bytes arrive, so a rejected input leaves the fields decoded before the error in it, and a field the bytes do not carry keeps what the object already held (MESSAGE_SPEC §2 omits an all-default field). Both are why generated code calls reset() first. corelib-cpp's inline path has documented exactly this all along; the two C++ profiles now say the same thing about the same entry point. Measured on the generator's side of the pair, ARMv6-m -Os, the whole image gc-sectioned (5-field example message, sizeof 504 B): stack, reset() frame 1832 B -> 1288 B (-544 B) .text 3484 B -> 3564 B (+80 B) The flash goes up because the generated reset() is now inlined at the call site where the staged instance used to share one out-of-line constructor. That is the trade this class offers: RAM for a little flash, on the profile where the stack is the scarce one. Tests: the caller's object is the one written (it would stay empty if the decoder bound a copy), a bound object keeps what the next message does not carry, and a rejected message leaves what was decoded before the error. The freestanding TU decodes through it too, where a std::function would not compile.
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.
Prerequisite for sofa-buffers/generator#744.
What
IStreamObject<T>holds the message it decodes into. A caller that already has one — which every generatedtry_decodeon this profile does — can therefore only decode into the stream's instance and copy it across afterwards:On a heap-free target that is a second whole message on the stack of every decode, plus a copy of it — 1096 B on the arena's example message.
IStreamInto<T>isIStreamObject<T>with the instance taken out: it binds the message handed to its constructor and holds a pointer where the other holds aMessageType.Why not
IStreamInlineThat is the shape corelib-cpp uses for the same job, and it is exactly what kept this profile staging-and-copying:
IStreamInlineholds astd::function(hosted) — and freestanding has nostd::functionat all, so the generated lambda shape cannot even compile here.IStreamIntohas none: same C-ABI field callback asIStreamObject, same two assignments in the constructor, same dispatch. The footprint argument no longer buys the copy anything.The contract, stated
Decoding in place costs the strong guarantee, so the doc says it plainly:
Both are why generated code calls
reset()first. corelib-cpp's inline path has documented exactly this all along — the two C++ profiles now say the same thing about the same entry point.Measured
ARMv6-m,
-Os, whole image linked with--gc-sections, 5-field example message (sizeof504 B), generated code from sofa-buffers/generator#744:reset()frame.textIStreamObject+ copyIStreamIntoThe flash goes up because the generated
reset()is now inlined at the call site, where the staged instance used to share one out-of-line constructor. That is the trade: RAM for a little flash, on the profile where the stack is the scarce one..bssis unchanged.Tests
test/cpp/test_istream.cpp— the caller's object is the one written (it would stay empty if the decoder bound a copy), a bound object keeps what the next message does not carry, and a rejected message leaves what was decoded before the error.test/cpp-freestanding/freestanding.cpp— the same bytes decoded throughIStreamIntointo a caller-owned object, in the TU that has nostd::function: a decoder that needed one would not compile there.No existing API changed;
IStreamObjectis untouched and still whatdecode()(which returns by value) uses.