Skip to content

Conversation

dunglas
Copy link
Member

@dunglas dunglas commented Feb 11, 2018

Q A
Branch? 2.7
Bug fix? no
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets n/a
License MIT
Doc PR n/a

Tiny optimizations and small code cleanup. The opcode triggering is especially useful in the hot path (normalizers and encoders) because it's a recursive process.

return $this->appendCData($node, $val);
} elseif (is_string($val)) {
} elseif ($isString) {
Copy link
Member

Choose a reason for hiding this comment

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

i'm not sure using a variable is faster at all (same below)

Copy link
Member Author

Choose a reason for hiding this comment

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

You mean the call is cheap and it's better to duplicate the call to is_string?

Copy link
Member

Choose a reason for hiding this comment

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

yep: the call is not an call anymore, it's an opcode

Copy link
Contributor

Choose a reason for hiding this comment

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

This could be written like this maybe:

} elseif (\is_string($val)) {
    if ($this->needsCdataWrapping($val)) {
        return $this->appendCData($node, $val);
    } else {
        return $this->appendText($node, $val);
    }
} elseif {

Copy link
Member

@nicolas-grekas nicolas-grekas Feb 11, 2018

Choose a reason for hiding this comment

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

this wouldn't bring anything, the check is almost free

@nicolas-grekas nicolas-grekas added this to the 2.7 milestone Feb 11, 2018
@@ -108,13 +108,13 @@ public function denormalize($data, $class, $format = null, array $context = arra
$attribute = $this->nameConverter->denormalize($attribute);
}

$allowed = false === $allowedAttributes || in_array($attribute, $allowedAttributes);
$ignored = in_array($attribute, $this->ignoredAttributes);
$allowed = false === $allowedAttributes || \in_array($attribute, $allowedAttributes, true);
Copy link
Member

Choose a reason for hiding this comment

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

Could setting the strict argument to true create some BC break in some obscure edge case?

Copy link
Member

Choose a reason for hiding this comment

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

I agree, let's remove all "true" added to in_array checks (the 3rd arg is not needed to benefit from the optim)

Copy link
Member Author

Choose a reason for hiding this comment

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

Removed, but both are strings so it cannot have a BC break.

@dunglas
Copy link
Member Author

dunglas commented Feb 13, 2018

Status: needs review

$allowed = false === $allowedAttributes || in_array($paramName, $allowedAttributes);
$ignored = in_array($paramName, $this->ignoredAttributes);
$allowed = false === $allowedAttributes || \in_array($paramName, $allowedAttributes, true);
$ignored = \in_array($paramName, $this->ignoredAttributes, true);
Copy link
Member

Choose a reason for hiding this comment

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

s/, true//

Copy link
Member Author

Choose a reason for hiding this comment

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

done

@nicolas-grekas
Copy link
Member

Thank you @dunglas.

nicolas-grekas added a commit that referenced this pull request Feb 14, 2018
This PR was squashed before being merged into the 2.7 branch (closes #26141).

Discussion
----------

[Serializer] optims and cleanup

| Q             | A
| ------------- | ---
| Branch?       | 2.7 <!-- see below -->
| Bug fix?      | no
| New feature?  | no <!-- don't forget to update src/**/CHANGELOG.md files -->
| BC breaks?    | no
| Deprecations? | no <!-- don't forget to update UPGRADE-*.md files -->
| Tests pass?   | yes
| Fixed tickets | n/a <!-- #-prefixed issue number(s), if any -->
| License       | MIT
| Doc PR        | n/a

Tiny optimizations and small code cleanup. The opcode triggering is especially useful in the hot path (normalizers and encoders) because it's a recursive process.

Commits
-------

8ee8387 [Serializer] optims and cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants