Use Two-Way for finding the delimiter in DelimiterBasedFrameDecoder by chrisvest · Pull Request #13451 · netty/netty · GitHub
Skip to content

Use Two-Way for finding the delimiter in DelimiterBasedFrameDecoder - #13451

Merged
normanmaurer merged 1 commit into
netty:4.1from
chrisvest:41-fast-delimiter
Jun 16, 2023
Merged

normanmaurer merged 1 commit into
netty:4.1from
chrisvest:41-fast-delimiter

Conversation

@chrisvest

Copy link
Copy Markdown
Member

Motivation:
We were using the naive and inefficient O(nm) algorithm here.

Modification:
Remove the naive algorithm and instead call out to the optimised Two-Way search we already have in ByteBufUtil.

Result:
The DelimiterBasedFrameDecoder now searches for delimiters using the fastest algorithm available to us.

Fixes #13313

Motivation:
We were using the naive and inefficient O(nm) algorithm here.

Modification:
Remove the naive algorithm and instead call out to the optimised Two-Way search we already have in ByteBufUtil.

Result:
The DelimiterBasedFrameDecoder now searches for delimiters using the fastest algorithm available to us.

Fixes netty#13313
@chrisvest chrisvest added this to the 4.1.94.Final milestone Jun 15, 2023
@chrisvest
chrisvest requested a review from normanmaurer June 15, 2023 17:25

@franz1981 franz1981 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM I don't remember if we are smart on the util method and check if the readable bytes is just one and it uses the swar index of one instead

@franz1981

Copy link
Copy Markdown
Contributor

@chrisvest

Copy link
Copy Markdown
Member Author

I suspect single-byte delimiters are fairly common, e.g. a single newline character.

@franz1981

franz1981 commented Jun 15, 2023 •

Copy link
Copy Markdown
Contributor

Nice, so let's wait the user feedback...
What's good about SWAR wasn't the batchy behaviour but that It amortizes mispredictions reducing the branches...I am super curious 🧐

@franz1981

franz1981 commented Jun 15, 2023 •

Copy link
Copy Markdown
Contributor

Just some before I forgot about them...
Given that the delimiter shouldn't change, maybe would be worthy to preprocess the needle as required by the algorithm and change the current two way impl to skip bound checks when possible

In the case of multi bytes delimiters, this should further increase performance for such a frequent op

@normanmaurer

Copy link
Copy Markdown
Member

Nice one @chrisvest

@normanmaurer
normanmaurer merged commit dd2c2bb into netty:4.1 Jun 16, 2023
@normanmaurer

Copy link
Copy Markdown
Member

@chrisvest can you also port this to main if needed ?

@chrisvest
chrisvest deleted the 41-fast-delimiter branch June 16, 2023 14:10
chrisvest added a commit that referenced this pull request Jun 16, 2023
…13451)

Motivation:
We were using the naive and inefficient O(nm) algorithm here.

Modification:
Remove the naive algorithm and instead call out to the optimised Two-Way
search we already have in ByteBufUtil.

Result:
The DelimiterBasedFrameDecoder now searches for delimiters using the
fastest algorithm available to us.

Fixes #13313
@rovarga

rovarga commented Jun 21, 2023

Copy link
Copy Markdown
Contributor

For what it's worth, downstream we have ended up doing https://git.opendaylight.org/gerrit/c/netconf/+/101892/1/netconf/netconf-netty-util/src/main/java/org/opendaylight/netconf/nettyutil/handler/NetconfEOMAggregator.java

The point is that the base class is not @sharable and therefore has context. While the two-way is a nice optimization, I think remembering previously-checked ranges is more beneficial. Our use case entails relatively small message chunks (couple of KiB) being aggregated to large messages (10s of MiB).

EOM marker is multi-byte in this case.

@normanmaurer

Copy link
Copy Markdown
Member

@rovarga we love PRs... If you have some ideas how to improve things just open a PR :)

@franz1981

franz1981 commented Jun 22, 2023 •

Copy link
Copy Markdown
Contributor

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.

the cpu usage is too high(80%) when run netty server thread named NioEventLoopGroup-3-1

4 participants