GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing github.com — Replace chunk extension Match[] state machine with flat transition table by franz1981 · Pull Request #16542 · netty/netty Conversation
Contributor
Motivation: Modification: Result:
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions
|
There was an error while loading. Please reload this page.
Benchmark ResultsCPU: see 1. End-to-end HTTP chunked request/response
Validation is ~3% of total work - you were right @chrisvest (it is a drop in the ocean ^^). 2. Isolated process() loop
Per-byte breakdown at length=256:
|
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
@chrisvest @normanmaurer any chance you had taken a look at the additional test? |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
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. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
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
|
@franz1981 I extract the parsing fix to a separate PR: #16579 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
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 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
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]>
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)
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)
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. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
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) |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
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]>
|
As soon as I am back from holidays I will finish this one 😄 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
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]>
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
|
PTAL @chrisvest and @normanmaurer |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Ptal again @chrisvest @normanmaurer |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
| return STATES_BY_ORDINAL[match.then]; | ||
| private static final byte[] TRANSITIONS = buildTable(); | ||
|
|
||
| private static byte[] buildTable() { |
There was a problem 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.
There was an error while loading. Please reload this page.
| @Override | ||
| public boolean process(byte value) { | ||
| state = state.match(value); | ||
| int next = TRANSITIONS[state << 8 | (value & 0xFF)]; |
There was a problem 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.
There was an error while loading. Please reload this page.
There was a problem 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.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
Improve stricter HTTP/1.1 chunk extension parsing
There was an error while loading. Please reload this page.