Skip to content
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

Allowing construction of NoAllocSliceSegments with buffer and NoAllocBufferSegmentType #488

Closed
hs-vc opened this issue Mar 26, 2024 · 7 comments

Comments

@hs-vc
Copy link

hs-vc commented Mar 26, 2024

It would be advantageous to enable the creation of NoAllocSliceSegments along with a buffer and NoAllocBufferSegmentType, particularly when the message has already undergone parsing. While this capability was present until version 0.17, it has since become unavailable, resulting in inefficiencies as parsing must be repeated during the construction of NoAllocSliceSegments.

The necessity for this functionality stems from the asynchronous reading of messages in the Cap'n Proto protocol format from sockets. In such scenarios, clients must parse the header (segments) to ascertain the message's length, thus obviating the need for re-parsing within NoAllocSliceSegments.

Is there any alternative approach available that can overcome this issue?

@dwrensha
Copy link
Member

dwrensha commented Mar 26, 2024

What if we add public visibility to ReadSegmentTableResult, read_segment_table(), and NoAllocBufferSegments::from_segment_table()? (These things are currently at private or crate-only visibility.)

Then I think you would be able get the message length from ReadSegmentTableResult before constructing the BufferSegments.

@hs-vc
Copy link
Author

hs-vc commented Mar 27, 2024

What if we add public visibility to ReadSegmentTableResult, read_segment_table(), and NoAllocBufferSegments::from_segment_table()? (These things are currently at private or crate-only visibility.)

Then I think you would be able get the message length from ReadSegmentTableResult before constructing the BufferSegments.

In my case, I don't need to use read_segment_table() which requires a complete slice because I need to parse the header while reading from the socket to get complete messages.
So I am reading bytes partially from the socket and parsing the header directly to determine the total message length. In fact, exposing NoAllocBufferSegments::from_segment_table() to public seems sufficient.

BTW, is there a reason why NoAllocBufferSegmentType is defined as:

enum NoAllocBufferSegmentType {
    SingleSegment(usize, usize),
    MultipleSegments,
}

Instead of:

enum NoAllocBufferSegmentType {
    SingleSegment,
    MultipleSegments,
}

In the case of SingleSegment, segment_table_length_bytes must always be 8, and the message length is also inferred from the buffer. In fact, in version 0.17, NoAllocSliceSegments requires buffer only for both of SingleSegment and MultipleSegments

@dwrensha
Copy link
Member

In the case of SingleSegment, segment_table_length_bytes must always be 8, and the message length is also inferred from the buffer.

Hm... I agree that it looks like the first parameter is not needed. The second parameter however might point to before the end of the buffer if from_buffer() is called on something that's longer than the message:

pub fn from_buffer(buffer: T, options: ReaderOptions) -> Result<Self> {
let segment_table_info = read_segment_table(buffer.as_ref(), options)?;
Ok(Self::from_segment_table(buffer, segment_table_info))
}

@dwrensha
Copy link
Member

I removed the first parameter in 9738fed.

@dwrensha
Copy link
Member

dwrensha commented Mar 28, 2024

I opened a PR for adding public visibility tot he relevant items: #489.

@hs-vc
Copy link
Author

hs-vc commented Mar 29, 2024

@dwrensha Thanks for your prompt help. I really appreciate it!

@dwrensha
Copy link
Member

Closed by #489.

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

No branches or pull requests

2 participants