-
Notifications
You must be signed in to change notification settings - Fork 91
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
get writeable buffer #120
get writeable buffer #120
Conversation
const size_t newOffset = _currOffset + size; | ||
maybeResize(newOffset, TResizable{}); | ||
_currOffset = newOffset; | ||
return _beginIt + static_cast<diff_t>(_currOffset); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Either I miss something, or this is a bug.
Instead of
_currOffset = newOffset;
return _beginIt + static_cast<diff_t>(_currOffset);
You should probably do like this:
auto res = _beginIt + static_cast<diff_t>(_currOffset);
_currOffset = newOffset;
return res;
In general I like the idea, but it's is not fully executed, and a lot of things are still missing, in order to land it as part as core functionality.
In the end, I think it would make more sense to write new input/output adapter together with extension. There's already example of one in #124. |
For alignment, I think it's definitely should be guranteed by user, user must know what they are doing before they use uncopy deserialize. And in-action, I don't think all the buffer data all requires the alignment in memory, for instances : flac, mp3 audio data, they can just be bitstream and pass into a decoder way to play it, only for wav, directly PCM data is required for 4b alignment. |
And I would like to know, if we serialize a 4byte data to drive, will it gurantee the alignment in file buffer side? Doesn't mean directly copy whole 32 bits to buffer, I mean it can gurantee the start address inside buffer of this 4 byte data can be multiple of 4? |
Okay I seen, there is no gurantee for the serialized buffer, only way to keep the alignment is add paddings, but it's still implementable! As the way I said, we need input and output adpater work together, it's simple, user tell us the alignment requirement of non copying buffer, and we add, give me a sec, I'm testing and will post result here soon. |
Thanks for working on it :) template <size_t ALIGNMENT>
TValue* allocateForDirectWrite(size_t size)
{
// first make sure that initial buffer has sufficient alignment ( >= than user type needs)
assert(is_sufficiently_aligned<ALIGNMENT>(_beginIt));
auto padding = (_currOffset + ALIGNMENT - _currOffset % ALIGNMENT) % ALIGNMENT;
// todo write 0s as padding bytes
auto res = _beginIt + static_cast<diff_t>(_currOffset + padding)
_currOffset = _currOffset + size + padding
return res
} On the call side you would probably do something like auto ptrTmp = writer.allocateForDirectWrite<alignof(T)>(size);
auto ptr = std::assume_aligned<alignof(T)>(ptrTmp); // to enable more efficient code generation |
Okay, I'm late, your solution is more straightforward, but can be optimize, we only need one modular operation and output adapter could only provide single noncopying write function. This is the extension code, with caution : There is only be single alignment value available for all the noncopyable buffer(otherwise alignment value is multiple of others buffer like 32, 16, 8, 4), this might need some safety improvements(currently it's allow each buffer to say a wanted alignment)
|
Well, actually with consider of branch predition, double modular is probably fast than per modular + if... But I think the predit sucessful chance is great, because the chance of no need padding is low I think(depends on previous serialization fields) |
i'm writing a custom extension that might invoke a 3rd party serialization method that takes a buffer to serialize into, and need to avoid an unnecessary copy (aka by first serializing into a temporary buffer then copying into the bitsery generate buffer) as these object might be many, many bytes... so i added 2 methods to extract the buffer pointer to write into.