Skip to content

CompositeByteBuf tidy-up#8784

Merged
normanmaurer merged 3 commits intonetty:4.1from
njhill:composite-clean
Feb 1, 2019
Merged

CompositeByteBuf tidy-up#8784
normanmaurer merged 3 commits intonetty:4.1from
njhill:composite-clean

Conversation

@njhill
Copy link
Copy Markdown
Member

@njhill njhill commented Jan 25, 2019

Motivation

There's some further miscellaneous cleanup/simplification of CompositeByteBuf which would help make the code a bit clearer.

Modifications

Result

Smaller/clearer code

@netty-bot
Copy link
Copy Markdown

Can one of the admins verify this patch?

@normanmaurer
Copy link
Copy Markdown
Member

@netty-bot test this please

Copy link
Copy Markdown
Member

@ejona86 ejona86 left a comment

Choose a reason for hiding this comment

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

These three changes look good and easy:

  • Rename Component.freeIfNecessary() method to just free(), which is less confusing (see #8641)
  • Make loop in addComponents0(...) method more verbose/readable (see #8437 (comment))
  • Simplify addition/subtraction in setBytes(...) methods

This one I think is currently a step backward:

  • Simplify web of constructors and addComponents methods, reducing duplication of logic

Some addComponents0() do the checkNotNull, and others don't. addComponent0() does not consolidate but addComponents0() does. It makes the code more subtle and harder to audit.

That's not to say I'm against moving stuff around. But I feel like the current changes make it less consistent, not more.

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.

Seems this should be addComponent() (not 0).

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.

@ejona86 this wasn't changed in this PR, but I think it was intended to be the 0 version as written. The buffer b is certain to be non-null, and we're avoiding consolidation after each addition so that it can be done once at the end of the enclosing method.

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.

Oh, I missed the (b = it.next()) != null). I thought it was the normal b = it.next(). It doesn't seem the loop is any better with this change and I might argue it was more clear before. (It's not clear why it would choose to stop early if b == null, but it was obvious it was explicitly handling that case.)

@ejona86
Copy link
Copy Markdown
Member

ejona86 commented Jan 29, 2019

I gave up trying to figure out if various addComponents() vs addComponents0() were the correct version.

Motivation

There's some miscellaneous cleanup/simplification of CompositeByteBuf
which would help make the code a bit clearer.

Modifications

- Simplify web of constructors and addComponents methods, reducing
duplication of logic
- Rename `Component.freeIfNecessary()` method to just `free()`, which is
less confusing (see netty#8641)
- Make loop in addComponents0(...) method more verbose/readable (see
netty#8437 (comment))
- Simplify addition/subtraction in setBytes(...) methods

Result

Smaller/clearer code
Now all those with 0-suffix don't include buffer null checks or
consolidation, and those without _do_.
@njhill
Copy link
Copy Markdown
Member Author

njhill commented Jan 29, 2019

Thanks for trying to navigate this @ejona86! I was focused primarily on the de-duplication and you're right that I should have paid more attention to the consistency of the behaviour of the 0-suffix versions. In my defence I think most of the inconsistency was preexisting!

I've added another small commit to make this consistent. Now none of the addComponent0 or addComponents0 methods perform null checks or consolidation, and all of the non-suffixed addComponent and addComponents ones do perform both.

Copy link
Copy Markdown
Member

@ejona86 ejona86 left a comment

Choose a reason for hiding this comment

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

Yeah, there was some minor weirdness with how addComponents0 would do checkNotNull, but that was minor (it wouldn't change the visible behavior) and it didn't seem any callers depended on the behavior. This most recent form has clear responsibilities again.

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.

Oh, I missed the (b = it.next()) != null). I thought it was the normal b = it.next(). It doesn't seem the loop is any better with this change and I might argue it was more clear before. (It's not clear why it would choose to stop early if b == null, but it was obvious it was explicitly handling that case.)

@njhill
Copy link
Copy Markdown
Member Author

njhill commented Jan 29, 2019

Thanks @ejona86. I've reverted that compaction now ... it was silly of me to have done it given one of the other changes in this same PR was to revert a similar occurrence!

Supporting null as a terminator I think was the behaviour all along, maybe for consistency with the array cases where this could be helpful to avoid having to make a copy if populated with an unknown-upfront number of buffers.

@normanmaurer
Copy link
Copy Markdown
Member

@netty-bot test this please

@normanmaurer normanmaurer added this to the 4.1.34.Final milestone Jan 30, 2019
@normanmaurer
Copy link
Copy Markdown
Member

@ejona86 want to take another look now that @njhill did a final commit or happy as it is ?

@ejona86
Copy link
Copy Markdown
Member

ejona86 commented Feb 1, 2019

@normanmaurer, I was happy with it already. Reverting the loop change was just icing on the cake.

@normanmaurer normanmaurer merged commit 98aa5fb into netty:4.1 Feb 1, 2019
normanmaurer pushed a commit that referenced this pull request Feb 1, 2019
Motivation

There's some miscellaneous cleanup/simplification of CompositeByteBuf
which would help make the code a bit clearer.

Modifications

- Simplify web of constructors and addComponents methods, reducing
duplication of logic
- Rename `Component.freeIfNecessary()` method to just `free()`, which is
less confusing (see #8641)
- Make loop in addComponents0(...) method more verbose/readable (see
#8437 (comment))
- Simplify addition/subtraction in setBytes(...) methods

Result

Smaller/clearer code
@njhill njhill deleted the composite-clean branch February 1, 2019 18:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants