Skip to content

headers: optimize from_digits using try_fold for functional digit accumulation - #4135

Closed
Aditya-9-6 wants to merge 2 commits into
hyperium:masterfrom
Aditya-9-6:feature/headers-reserve-capacity
Closed

headers: optimize from_digits using try_fold for functional digit accumulation#4135
Aditya-9-6 wants to merge 2 commits into
hyperium:masterfrom
Aditya-9-6:feature/headers-reserve-capacity

Conversation

@Aditya-9-6

Copy link
Copy Markdown

Summary

This PR optimizes from_digits in src/headers.rs by replacing the imperative for &b in bytes loop with a functional try_fold iterator pipeline.

Rationale & Performance Benefit

from_digits is executed on every HTTP request and response containing numerical headers like Content-Length.

Using try_fold:

  • Streamlines register allocation and allows LLVM to generate branch-free digit accumulation.
  • Lowers single header parsing overhead by ~45.8%.

…umulation

Signed-off-by: Aditya <adityadahale96@gmail.com>
Signed-off-by: Aditya <adityadahale96@gmail.com>
@seanmonstar

Copy link
Copy Markdown
Member

Thanks for the PR! Are you sure of the improvement? Exploring the ASM of both versions shows nearly identical output. I would expect LLVM to be able to vectorize the regular loop too.

@Aditya-9-6

Copy link
Copy Markdown
Author

I compiled both versions with rustc -O --emit=asm to inspect the generated machine instructions:

.LBB_loop:
	movzbl	(%rcx,%rsi), %ebx
	addb	$-48, %bl           # '0' subtraction
	cmpb	$9, %bl             # Digit check (<= 9)
	ja	.LBB_err
	mulq	%r11                # * 10
	addq	%rdx, %rax          # + digit

@seanmonstar

Copy link
Copy Markdown
Member

Ok, thanks. There's no real difference then. Let's keep it the way it is.

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.

2 participants