Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add benchmark showing a 2.5 reduction in memory usage since RC2 #298

Merged
merged 1 commit into from
Nov 10, 2023

Conversation

dunglas
Copy link
Owner

@dunglas dunglas commented Nov 10, 2023

This benchmark shows that #292 and #293 improve memory usage by a factor of 2.5!
The more data read from the request body or written in the response body, the more significant the improvement!

There is still room for similar improvements in:

func go_register_variables(rh C.uintptr_t, trackVarsArray *C.zval) {

It would be nice if the memory wasn't allocated once in Go and a second time in C (and then a third time because of how PHP works internally). A possible way to improve this could be to populate the CGI variables directly in C-compatible strings, to avoid copyinv.

Before #292 and #293.

Memory usage: 14.9 bytes per request.

Running tool: /opt/homebrew/bin/go test -benchmem -run=^$ -bench ^BenchmarkEcho$ github.com/dunglas/frankenphp -v -count=1

goos: darwin
goarch: arm64
pkg: github.com/dunglas/frankenphp
BenchmarkEcho
BenchmarkEcho-8 3514 342450 ns/op 15261 B/op 32 allocs/op
PASS
ok github.com/dunglas/frankenphp 2.122s

After

5.3 bytes per request.

Running tool: /opt/homebrew/bin/go test -benchmem -run=^$ -bench ^BenchmarkEcho$ github.com/dunglas/frankenphp -v -count=1

goos: darwin
goarch: arm64
pkg: github.com/dunglas/frankenphp
BenchmarkEcho
BenchmarkEcho-8 3565 335976 ns/op 5482 B/op 28 allocs/op
PASS
ok github.com/dunglas/frankenphp 2.150s

@dunglas dunglas merged commit 1279163 into main Nov 10, 2023
27 checks passed
@dunglas dunglas deleted the chore/echo-benchmark branch November 10, 2023 16:10
@withinboredom
Copy link
Collaborator

A possible way to improve this could be to populate the CGI variables directly in C-compatible strings

If you add a "\0" onto the end of the go strings (strings in Go are not null terminated), and know the strings won't be mutated ... you could probably try this super hacky (untested) method to get the raw pointer from Go:

char* cstring = (*reflect.StringHeader)(unsafe.Pointer(&gostring)).Data

Maybe?

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