Skip to content

Fix for incorrect values from CompositeByteBuf#component(int)#9525

Merged
normanmaurer merged 3 commits intonetty:4.1from
njhill:simpler-cbb-fix
Sep 2, 2019
Merged

Fix for incorrect values from CompositeByteBuf#component(int)#9525
normanmaurer merged 3 commits intonetty:4.1from
njhill:simpler-cbb-fix

Conversation

@njhill
Copy link
Copy Markdown
Member

@njhill njhill commented Aug 30, 2019

Motivation

This is a "simpler" alternative to #9416 which fixes the same CompositeByteBuf bugs described there, originally reported by @jingene in #9398.

Modifications

  • Add fields to Component class for the original buffer along with its adjustment, which may be different to the already-stored unwrapped buffer. Use it in appropriate places to ensure correctness and equivalent behaviour to that prior to the earlier optimizations
  • Add comments explaining purpose of each of the Component fields
  • Unwrap more kinds of buffers in newComponent method to extend scope of the existing indirection-reduction optimization
  • De-duplicate common buffer consolidation logic
  • Unit test for the original bug provided by @jingene

Result

  • Correct behaviour / fixed bugs
  • Some code deduplication / simplification
  • Unwrapping optimization applied to more types of buffers

The downside is increased mem footprint from the two new fields, and additional allocations in some specific cases, though those should be rare.

Co-authored-by: jingene jingene0206@gmail.com

@netty-bot
Copy link
Copy Markdown

Can one of the admins verify this patch?

Motivation

This is a "simpler" alternative to netty#9416 which fixes the same
CompositeByteBuf bugs described there, originally reported by @jingene
in netty#9398.

Modifications
- Add fields to Component class for the original buffer along with its
adjustment, which may be different to the already-stored unwrapped
buffer. Use it in appropriate places to ensure correctness and
equivalent behaviour to that prior to the earlier optimizations
- Add comments explaining purpose of each of the Component fields
- Unwrap more kinds of buffers in newComponent method to extend scope of
the existing indirection-reduction optimization
- De-duplicate common buffer consolidation logic
- Unit test for the original bug provided by @jingene

Result
- Correct behaviour / fixed bugs
- Some code deduplication / simplification
- Unwrapping optimization applied to more types of buffers

The downside is increased mem footprint from the two new fields, and
additional allocations in some specific cases, though those should be
rare.


Co-authored-by: jingene <jingene0206@gmail.com>
@normanmaurer
Copy link
Copy Markdown
Member

@netty-bot test this please

Copy link
Copy Markdown
Contributor

@franz1981 franz1981 left a comment

Choose a reason for hiding this comment

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

LGTM

@jingene
Copy link
Copy Markdown
Contributor

jingene commented Aug 30, 2019

LGTM 👍

@normanmaurer
Copy link
Copy Markdown
Member

@netty-bot test this please

@SuppressWarnings("deprecation")
private Component newComponent(final ByteBuf buf, final int offset) {
final int srcIndex = buf.readerIndex();
final int len = buf.writerIndex() - buf.readerIndex();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: final int len = buf.readableBytes()

return buf.duplicate().setIndex(idx(offset), idx(endOffset));
// We can only use the cached slice safely if it's equal to the source buffer,
// otherwise it might not be fully visible to us
return (slice == srcBuf ? srcBuf : srcBuf.slice()).duplicate();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Why do we need to first slice and then also duplicate ?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@normanmaurer oops, I had meant this to be .slice(srcIdx(offset), length()). But thinking some more and comparing further to the "original" impl I think you are right that srcBuf.duplicate() would be ok and obviously preferable overhead-wise.

I was concerned about the possibility of someone independently modifying the indices of a buffer after it was added, but we make it clear that the CompositeByteBuf takes "ownership" of all added buffers (mainly refcount-wise but I think indices would be assumed too).

@njhill
Copy link
Copy Markdown
Member Author

njhill commented Aug 30, 2019

Thanks @franz1981 @jingene @normanmaurer, updated to address comments

@normanmaurer
Copy link
Copy Markdown
Member

@netty-bot test this please

@normanmaurer normanmaurer merged commit 1039f69 into netty:4.1 Sep 2, 2019
@normanmaurer
Copy link
Copy Markdown
Member

@njhill @jingene thanks a lot!

normanmaurer pushed a commit that referenced this pull request Sep 2, 2019
Motivation

This is a "simpler" alternative to #9416 which fixes the same
CompositeByteBuf bugs described there, originally reported by @jingene
in #9398.

Modifications
- Add fields to Component class for the original buffer along with its
adjustment, which may be different to the already-stored unwrapped
buffer. Use it in appropriate places to ensure correctness and
equivalent behaviour to that prior to the earlier optimizations
- Add comments explaining purpose of each of the Component fields
- Unwrap more kinds of buffers in newComponent method to extend scope of
the existing indirection-reduction optimization
- De-duplicate common buffer consolidation logic
- Unit test for the original bug provided by @jingene

Result
- Correct behaviour / fixed bugs
- Some code deduplication / simplification
- Unwrapping optimization applied to more types of buffers

The downside is increased mem footprint from the two new fields, and
additional allocations in some specific cases, though those should be
rare.


Co-authored-by: jingene <jingene0206@gmail.com>
@normanmaurer normanmaurer added this to the 4.1.40.Final milestone Sep 2, 2019
@njhill njhill deleted the simpler-cbb-fix branch September 3, 2019 17:26
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.

5 participants