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

refactor: faster $_SERVER variables creation #303

Merged
merged 1 commit into from
Nov 16, 2023
Merged

Conversation

dunglas
Copy link
Owner

@dunglas dunglas commented Nov 15, 2023

This patch changes how $_SERVER variables are populated to minimize allocations.
Also, reduce the number of switches between C and Go.

According to the benchmark introduced in #298, this change improves memory usage by 38% compared to main, and from 78% compared to RC3.

Benchmark:

goos: darwin
goarch: arm64
pkg: github.com/dunglas/frankenphp
BenchmarkEcho
BenchmarkEcho-8   	    3571	    330508 ns/op	    3404 B/op	      24 allocs/op
PASS
ok  	github.com/dunglas/frankenphp	1.916s

Some string data are still copied from C to Go. I tried to avoid that by using unsafe.StringData)( but didn't manage to get something that works because it looks not possible to pin pointers returned by unsafe.StringData()

Copy link
Collaborator

@withinboredom withinboredom left a comment

Choose a reason for hiding this comment

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

Can't wait to test this...

allocServerVariable(&cArr, fc.env, gatewayInterface, "GATEWAY_INTERFACE", "CGI/1.1")
allocServerVariable(&cArr, fc.env, serverProtocol, "SERVER_PROTOCOL", request.Proto)
allocServerVariable(&cArr, fc.env, serverSoftware, "SERVER_SOFTWARE", "FrankenPHP")
allocServerVariable(&cArr, fc.env, httpHost, "HTTP_HOST", request.Host) // added here, since not always part of headers
Copy link
Collaborator

Choose a reason for hiding this comment

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

Does request.Host come from the headers, if it is set there? I know many WordPress installs typically set the site name from this variable.

Copy link
Owner Author

Choose a reason for hiding this comment

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

frankenphp_register_known_variable("REQUEST_URI", SG(request_info).request_uri, track_vars_array,false);

/* Known variables */
frankenphp_register_known_variable("CONTENT_LENGTH", known_variables[0], track_vars_array, true);
Copy link
Collaborator

Choose a reason for hiding this comment

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

It might be worth using consts for the indices of known_variables and then reusing them in allocServerVariable() on the Go side. It'd help with maintaining and reviewing, but other than that, wouldn't do anything.

Copy link
Owner Author

Choose a reason for hiding this comment

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

I'm not sure it will help maintenance to add macros, to be honest, but PR is welcome!

@dunglas dunglas merged commit aa1d968 into main Nov 16, 2023
26 checks passed
@dunglas dunglas deleted the refactor/populate-env branch November 16, 2023 13:40
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