GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Replace chunk extension Match[] state machine with flat transition table by franz1981 · Pull Request #16542 · netty/netty

Conversation

@franz1981

@franz1981 franz1981 commented Mar 25, 2026

Copy link
Copy Markdown
Contributor

Motivation:
The existing enum State with Match[] (BitSet) arrays uses multiple
pointer-chasing loads per byte. It also has two bugs: ';' was not
excluded from ChunkExtValToken causing valid multi-extension lines
to be rejected, and finish() did not accept CHUNK_EXT_VAL_TOKEN as
a valid final state.

Modification:
Replace with a flat byte[7*256] transition table populated at class
load time. Fix both missing cases. Add regression tests and benchmarks.

Result:
Fixes #16540

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@franz1981
franz1981 requested review from chrisvest and normanmaurer March 25, 2026 11:20
@franz1981

franz1981 commented Mar 25, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

Benchmark Results

CPU: see lscpu, NUMA node 0 pinned via numactl --localalloc -N 0
JDK: Oracle 25.0.2 (runtime), Temurin 21.0.8 (build)

1. End-to-end HTTP chunked request/response

HttpChunkedRequestResponseBenchmark — 16 chunks with randomized extensions (name-only, token, quoted, mixed).

Metric Baseline Optimized Delta
Throughput (ops/s) 10,916,175 11,063,757 +1.4%
Instructions/op 1,171.5 1,156.9 -1.2%
Branches/op 248.3 244.2 -1.7%

Validation is ~3% of total work - you were right @chrisvest (it is a drop in the ocean ^^).
Hot path is channel pipeline machinery instead...

2. Isolated process() loop

HttpChunkExtValidationBenchmark — no allocation, reuses processor instance, 64 pre-generated inputs.

Length enumBitSet (ops/μs) flatTable (ops/μs) Speedup
64 2.416 9.094 3.76x
256 0.587 2.115 3.60x
1024 0.193 0.520 2.69x

Per-byte breakdown at length=256:

Metric enumBitSet flatTable Ratio
cycles/byte 28.6 8.0 3.6x fewer
instructions/byte 67.1 10.7 6.3x fewer
branches/byte 11.3 2.3 4.9x fewer
branch-misses/op 4.0 0.2 17x fewer
IPC 2.35 1.34 —
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

franz1981
franz1981 commented Mar 25, 2026
View reviewed changes
Comment thread codec-http/src/main/java/io/netty/handler/codec/http/HttpChunkLineValidatingByteProcessor.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@franz1981
franz1981 force-pushed the 4.2_16540 branch from 066d537 to 610ce16 Compare March 27, 2026 07:24
@franz1981

franz1981 commented Mar 30, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

@chrisvest @normanmaurer any chance you had taken a look at the additional test?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@chrisvest

chrisvest commented Mar 30, 2026

Copy link
Copy Markdown
Member

I think it'd be better to separate bug fixes and performance improvements into separate PRs.

I like the execution speed of the table approach, but I prefer describing the grammar in terms of the enum and match clauses. Can we keep the enum and have it compile into the table? Perhaps we can make a small internal DSL out of this, for the various small-scale parsing needs we have.

franz1981 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

chrisvest added a commit to chrisvest/netty that referenced this pull request Mar 30, 2026
@chrisvest
Fix parsing HTTP chunks with multiple extensions
e833dd2
Motivation:
The chunk extension parsing/validation logic did not correctly account for extensions with multiple key-value pairs.

Modification:
- Adapt parsing logic to accept the repeatability of extension key-value pairs, and that chunk extensions can end on unquoted value tokens.
- Add tests to capture these cases.

Result:
More correct HTTP chunk extension parsing.

The tests are lifted from netty#16542
@chrisvest chrisvest mentioned this pull request Mar 30, 2026
Fix parsing HTTP chunks with multiple extensions #16579
Merged
@chrisvest

chrisvest commented Mar 30, 2026

Copy link
Copy Markdown
Member

@franz1981 I extract the parsing fix to a separate PR: #16579

franz1981 reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981

franz1981 commented Mar 31, 2026

Copy link
Copy Markdown
Contributor Author

Can we keep the enum and have it compile into the table?

I can see why; my concern is to make the static init to be intensive (GC and CPU wise) for no apparent reason. But the benefit of a declarative API, for us humans, is undeniable, is agree

Let me check if I can make this some sort of builder, without any GC root so, once CDS or Layden Is used, the table is the only thing which would be retained at runtime

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

normanmaurer added a commit that referenced this pull request Mar 31, 2026
@chrisvest @normanmaurer
Fix parsing HTTP chunks with multiple extensions (#16579)
b2c0dfd
Motivation:
The chunk extension parsing/validation logic did not correctly account
for extensions with multiple key-value pairs.

Modification:
- Adapt parsing logic to accept the repeatability of extension key-value
pairs, and that chunk extensions can end on unquoted value tokens.
- Add tests to capture these cases.

Result:
More correct HTTP chunk extension parsing.

The tests are lifted from #16542

Co-authored-by: Norman Maurer <[email protected]>
netty-project-bot pushed a commit that referenced this pull request Mar 31, 2026
@chrisvest @normanmaurer @netty-project-bot
Fix parsing HTTP chunks with multiple extensions (#16579)
d338e14
Motivation:
The chunk extension parsing/validation logic did not correctly account
for extensions with multiple key-value pairs.

Modification:
- Adapt parsing logic to accept the repeatability of extension key-value
pairs, and that chunk extensions can end on unquoted value tokens.
- Add tests to capture these cases.

Result:
More correct HTTP chunk extension parsing.

The tests are lifted from #16542

Co-authored-by: Norman Maurer <[email protected]>
(cherry picked from commit b2c0dfd)
@netty-project-bot netty-project-bot mentioned this pull request Mar 31, 2026
Auto-port 5.0: Fix parsing HTTP chunks with multiple extensions #16587
Merged
netty-project-bot pushed a commit that referenced this pull request Mar 31, 2026
@chrisvest @normanmaurer @netty-project-bot
Fix parsing HTTP chunks with multiple extensions (#16579)
2870b6f
Motivation:
The chunk extension parsing/validation logic did not correctly account
for extensions with multiple key-value pairs.

Modification:
- Adapt parsing logic to accept the repeatability of extension key-value
pairs, and that chunk extensions can end on unquoted value tokens.
- Add tests to capture these cases.

Result:
More correct HTTP chunk extension parsing.

The tests are lifted from #16542

Co-authored-by: Norman Maurer <[email protected]>
(cherry picked from commit b2c0dfd)
@netty-project-bot netty-project-bot mentioned this pull request Mar 31, 2026
Auto-port 4.1: Fix parsing HTTP chunks with multiple extensions #16588
Merged
@chrisvest

chrisvest commented Mar 31, 2026

Copy link
Copy Markdown
Member

I can see why; my concern is to make the static init to be intensive (GC and CPU wise) for no apparent reason. But the benefit of a declarative API, for us humans, is undeniable, is agree

Yeah, I know the init cost is there, but I think it's worth it in the worst case. I don't know how much a builder would help. It's hard to quantify the early init performance. In the other extreme end, we could code-gen from an annotation processor or something, but that would also be a lot of work.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@normanmaurer

normanmaurer commented Apr 1, 2026

Copy link
Copy Markdown
Member

I can see why; my concern is to make the static init to be intensive (GC and CPU wise) for no apparent reason. But the benefit of a declarative API, for us humans, is undeniable, is agree

Yeah, I know the init cost is there, but I think it's worth it in the worst case. I don't know how much a builder would help. It's hard to quantify the early init performance. In the other extreme end, we could code-gen from an annotation processor or something, but that would also be a lot of work.

Please don't over-optimize. I am strongly in favor of not adding even more complexity that would be a nightmare to maintain (like code-gen)

franz1981 and leviYX reacted with laugh emoji franz1981 and leviYX reacted with heart emoji
All reactions
  • 😄 2 reactions
  • ❤️ 2 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

chrisvest added a commit that referenced this pull request Apr 3, 2026
@netty-project-bot @chrisvest @normanmaurer
Auto-port 4.1: Fix parsing HTTP chunks with multiple extensions (#16588)
0841072
Auto-port of #16579 to 4.1
Cherry-picked commit: b2c0dfd

---
Motivation:
The chunk extension parsing/validation logic did not correctly account
for extensions with multiple key-value pairs.

Modification:
- Adapt parsing logic to accept the repeatability of extension key-value
pairs, and that chunk extensions can end on unquoted value tokens.
- Add tests to capture these cases.

Result:
More correct HTTP chunk extension parsing.

The tests are lifted from #16542

Co-authored-by: Chris Vest <[email protected]>
Co-authored-by: Norman Maurer <[email protected]>
@franz1981

franz1981 commented Apr 5, 2026

Copy link
Copy Markdown
Contributor Author

As soon as I am back from holidays I will finish this one 😄

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

chrisvest added a commit that referenced this pull request Apr 9, 2026
@netty-project-bot @chrisvest @normanmaurer
Auto-port 5.0: Fix parsing HTTP chunks with multiple extensions (#16587)
d83abb5
Auto-port of #16579 to 5.0
Cherry-picked commit: b2c0dfd

---
Motivation:
The chunk extension parsing/validation logic did not correctly account
for extensions with multiple key-value pairs.

Modification:
- Adapt parsing logic to accept the repeatability of extension key-value
pairs, and that chunk extensions can end on unquoted value tokens.
- Add tests to capture these cases.

Result:
More correct HTTP chunk extension parsing.

The tests are lifted from #16542

Co-authored-by: Chris Vest <[email protected]>
Co-authored-by: Norman Maurer <[email protected]>
franz1981 added 2 commits April 23, 2026 20:33
@franz1981
Replace chunk extension Match[] state machine with flat transition table
97a0b79
Motivation:
The existing enum State with Match[] (BitSet) arrays uses multiple
pointer-chasing loads per byte in the process() hot path.

Modification:
Compile the declarative enum + Match specification into a flat
byte[7*256] transition table at class load time. Reorder state
constants so that invalid-at-finish states are contiguous, enabling
finish() to use a single range check instead of a switch - this
benefits native image AOT compilation which has no branch profiling.

Result:
Fixes netty#16540
@franz1981
Add isolated benchmark for chunk extension validation loop
dc6516e
@franz1981
franz1981 force-pushed the 4.2_16540 branch from 4c6c6ad to dc6516e Compare April 23, 2026 18:36
@franz1981

franz1981 commented Apr 23, 2026

Copy link
Copy Markdown
Contributor Author

PTAL @chrisvest and @normanmaurer
I have slightly changed the finish logic to become a single comparison ;)
Nothing wow, but since often we parse 6-8 chars...

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@franz1981

franz1981 commented May 12, 2026

Copy link
Copy Markdown
Contributor Author

Ptal again @chrisvest @normanmaurer

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

franz1981 and others added 2 commits May 12, 2026 12:02
@franz1981
Merge branch '4.2' into 4.2_16540
b49729a
@normanmaurer
Merge branch '4.2' into 4.2_16540
70e5cb6
normanmaurer
normanmaurer reviewed Jun 15, 2026
View reviewed changes
Comment thread codec-http/src/main/java/io/netty/handler/codec/http/HttpChunkLineValidatingByteProcessor.java
return STATES_BY_ORDINAL[match.then];
private static final byte[] TRANSITIONS = buildTable();

private static byte[] buildTable() {

@normanmaurer normanmaurer Jun 15, 2026

Copy link
Copy Markdown
Member

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.

@franz1981 can you add some comments to better explain how all this works ?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
normanmaurer
normanmaurer reviewed Jun 15, 2026
View reviewed changes
Comment thread codec-http/src/main/java/io/netty/handler/codec/http/HttpChunkLineValidatingByteProcessor.java
@Override
public boolean process(byte value) {
state = state.match(value);
int next = TRANSITIONS[state << 8 | (value & 0xFF)];

@normanmaurer normanmaurer Jun 15, 2026

Copy link
Copy Markdown
Member

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.

Can you add some comments related to the bitshifting etc ?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
normanmaurer
normanmaurer reviewed Oct 29, 2026
View reviewed changes

@normanmaurer normanmaurer left a comment

Copy link
Copy Markdown
Member

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.

Thanks for working on this. A few correctness / safety concerns, since this class is part of the chunk-extension hardening.

1. Behavior change for bytes >= 0x80

The old Match.get(byte) was called with a negative index for bytes >= 0x80, so BitSet.get threw IndexOutOfBoundsException and those bytes were rejected in every state. With value & 0xFF the table is now consulted for 0x80-0xFF, and ChunkExtValQuoted / ChunkExtValQuotedEscape have range(0x80, 0xFF) entries. A line like 5;a="café" (obs-text in a quoted-string) therefore used to fail and is now accepted. That may well be the RFC-correct behavior, but it changes accept/reject behavior in a security-sensitive path, and the exception type for other states changes as well (InvalidChunkExtensionException / NumberFormatException instead of IndexOutOfBoundsException). Can we make this an explicit decision and cover it with a test? Alternatively, keep the previous behavior and reject >= 0x80 for now.

2. No unit tests

The PR only adds benchmarks. For a refactor of the validation core I would expect a test that runs every (state, byte 0..255) pair through the old and new implementations (or a table of expected outcomes) and asserts the same result and exception type, apart from the intentional change in 1. The benchmark's OldProcessor could serve as the reference there. At the moment setup() runs both processors but never compares their results.

3. Implicit coupling of constants and ordinals

  • TRANSITIONS is indexed by State.ordinal(), while the transitions Match.then use the int constants. Moving ChunkExtValQuotedEnd / ChunkExtValToken up in the enum only works because the constants were renumbered by hand to match. Nothing enforces this, so a mismatch would silently produce wrong transitions. An assertion in buildTable() (e.g. that the ordinals line up with the constants) would help.
  • finish() is now state >= FIRST_INVALID_FINISH_STATE, and the old exhaustiveness assert is gone. A state inserted on the wrong side of that boundary would silently change what is accepted at end of line, e.g. a dangling ;a="unterminated. FIRST_INVALID_FINISH_STATE also duplicates the value of CHUNK_EXT_VAL_START; defining it as = CHUNK_EXT_VAL_START would remove one way to drift.
  • buildTable() is last-writer-wins, whereas the old match() was first-match-wins. The Match sets are disjoint today so it makes no difference, but it would silently resolve a future overlap the opposite way. Asserting that an entry is still -1 before writing it would keep the semantics safe.

4. Package-private mutable state

state went from private State to package-private int so the benchmark can reset it. This exposes parser state to the whole package and hard-codes the SIZE value into the benchmark. If a reset hook is needed, a small package-private method would be safer.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@normanmaurer normanmaurer normanmaurer left review comments
@chrisvest chrisvest Awaiting requested review from chrisvest

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Improve stricter HTTP/1.1 chunk extension parsing

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@franz1981 @chrisvest @normanmaurer
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.