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

Use zval storage for Stream instances created from strings #230

Merged
merged 1 commit into from
Apr 20, 2023

Conversation

nicolas-grekas
Copy link
Collaborator

Alternative to #227 so that we can compare both approaches.

@nicolas-grekas nicolas-grekas force-pushed the zval-stream branch 3 times, most recently from 943b525 to f22ea02 Compare April 10, 2023 15:53
@nicolas-grekas
Copy link
Collaborator Author

PR is ready, I confirm this prevents the memcopy from happening.

{
static $wrapper;

$wrapper ?? \stream_wrapper_register('Nyholm-Psr7-Zval', $wrapper = \get_class(new class() {

Choose a reason for hiding this comment

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

Why the wrapper is registered twice - stream_wrapper_register?

Also any reason to not use ??= here / or regular if to improve the code readibility?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's not registered twice, where do you read that? About ??=, PHP 7.1 rulez. For the "if", I find the current style more readable, because less indentation.

Choose a reason for hiding this comment

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

on l318 and then l407, has the 2nd register on l407 any coverage and how it is guaranteed the wrapped is not registered when the 1st fopen fails? I would say the if on l406 is not needed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That "if" guards against unwanted calls to stream_wrapper_unregister('Nyholm-Psr7-Zval').

Choose a reason for hiding this comment

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

I would personally prefer to not do it as any unrelated code MUST never touch wrappers that are not owned by the code.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I prefer keeping it, adding some guards against global scope manipulations.


public function stream_truncate(int $new_size): bool
{
if ($new_size) {

Choose a reason for hiding this comment

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

the if condition and else should not be needed

Copy link
Collaborator Author

@nicolas-grekas nicolas-grekas Apr 11, 2023

Choose a reason for hiding this comment

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

it's a small optimization

Choose a reason for hiding this comment

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

I belive substr(expr, 0, 0) is optimized internally to return interned ''.

Copy link
Collaborator Author

@nicolas-grekas nicolas-grekas Apr 11, 2023

Choose a reason for hiding this comment

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

the optimization is about removing the need to call any functions :)

@nicolas-grekas nicolas-grekas force-pushed the zval-stream branch 3 times, most recently from c949d76 to d978d61 Compare April 17, 2023 17:05
@nicolas-grekas
Copy link
Collaborator Author

PR rebased, still green. I updated the branch-alias also.

Copy link
Owner

@Nyholm Nyholm left a comment

Choose a reason for hiding this comment

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

Thank you

@Nyholm Nyholm merged commit 083ce36 into Nyholm:master Apr 20, 2023
@nicolas-grekas nicolas-grekas deleted the zval-stream branch April 20, 2023 08:15
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.

3 participants