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

performance improvements #310

Merged
merged 1 commit into from
Oct 10, 2016
Merged

Conversation

digitalkaoz
Copy link
Contributor

@digitalkaoz digitalkaoz commented Sep 21, 2016

  • fixed unnecessary object references
  • avoid unnecessary array_merges
  • speedup travis builds
  • speedup tests

@digitalkaoz digitalkaoz force-pushed the master branch 2 times, most recently from 4f96ab3 to 0a48d3a Compare September 21, 2016 10:40
@digitalkaoz digitalkaoz changed the title avoid unnessary array_merge calls performance improvements Sep 21, 2016
@digitalkaoz digitalkaoz force-pushed the master branch 7 times, most recently from c1cd87c to e428b84 Compare September 21, 2016 12:26
@digitalkaoz
Copy link
Contributor Author

@bighappyface are you willing to review this one?

@digitalkaoz digitalkaoz force-pushed the master branch 7 times, most recently from 3ad7f54 to 0c45504 Compare September 21, 2016 13:56
return array_key_exists($property, $element) ? $element[$property] : $fallback;
} elseif (is_object($element)) {
return property_exists($element, $property) ? $element->$property : $fallback;
if (is_array($element) && array_key_exists($property, $element) /*$this->checkMode == self::CHECK_MODE_TYPE_CAST*/) {
Copy link
Contributor

Choose a reason for hiding this comment

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

what do you think of isset instead of array_key_exists which is a lot faster.

this would mean you also need an additional call to array_key_exists in case it does allow caching of NULL values. In cases were most calls would already be catched by isset($element[property]) this will speedup things a lot.

so the end result would render to something like

if (is_array($element)) {
  if (isset($element[property]) || array_key_exists($property, $element)) {
    return $element[property];
  }
}
// ...
return $fallback;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah good point, did it

@digitalkaoz digitalkaoz force-pushed the master branch 3 times, most recently from b67d90e to ad93364 Compare September 21, 2016 14:17
@staabm
Copy link
Contributor

staabm commented Sep 21, 2016

related tweet which shows the measured improvements:
https://twitter.com/digitalkaoz/status/778573654712061952

@digitalkaoz
Copy link
Contributor Author

digitalkaoz commented Sep 21, 2016

running it in an isolated environment (a single time) doesnt show much difference...but we are running it in a long running process, and there the gain is huge

https://blackfire.io/profiles/compare/5d8fd94c-bb36-4717-b3ad-c24d87bfbf6b/graph

@staabm
Copy link
Contributor

staabm commented Sep 21, 2016

at best you would drop the public blackfire comparison link so we can see exactly whats going on.
see https://blog.blackfire.io/profiles-public-sharing.html

{
$this->uriRetriever = $uriRetriever;
return $this->factory->getCheckMode();
Copy link

Choose a reason for hiding this comment

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

this kind of API tells me that your factory is not really a factory (or not only that)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah well, removed those getters, they are not really needed and not part of any interface

*/
public function validateDefinition($element, $objectDefinition = null, JsonPointer $path = null)
{
$constraint = $this->getFactory()->createInstanceFor('undefined');
Copy link

Choose a reason for hiding this comment

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

I would name it $undefinedConstraint to make the code more readable

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

$this->uriRetriever = $uriRetriever;
$this->factory = $factory;
$this->schemaStorage = $schemaStorage;
public function __construct(Factory $factory = null)
Copy link

Choose a reason for hiding this comment

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

you should consider providing a BC layer for the constructor signature. Otherwise, this force to bump to 4.0 already. Supporting the old signature with a deprecation warning (building a factory from the older arguments) would make the upgrade path easier

Copy link
Contributor Author

Choose a reason for hiding this comment

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

im slightly against supporting BC...i guess nobody is using custom constraints out there so its all part of the internals...im ok with a BC break here

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm injecting a few constraints, at least until this gets merged: #308

That said, changing my client code isn't a big deal. When can we expect these PRs to get merged? What are we waiting on?

@@ -72,33 +47,13 @@ public function getUriRetriever()
public function getFactory()
{
if (!$this->factory) {
$this->factory = new Factory($this->getSchemaStorage(), $this->getUriRetriever(), $this->checkMode);
$this->factory = new Factory();
Copy link

Choose a reason for hiding this comment

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

If the factory is now always created in the constructor, there is no point in doing if (!$this->factory) in the public getFactory method anymore.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah not needed, fixed

@bighappyface
Copy link
Collaborator

Hey @shmax I have merged #308

@digitalkaoz would you please rebase this PR on master and resolve conflicts. I will approve/merge/bump once all is green.

@digitalkaoz
Copy link
Contributor Author

@bighappyface ok rebased, should be all green in a few minutes!

/**
* @dataProvider getInvalidCoerceTests
*/
public function testInvalidCoerceCases($input, $schema, $errors = [])
Copy link
Collaborator

Choose a reason for hiding this comment

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

array for 5.3

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

$validator = new Validator(new Factory($schemaStorage, null, $checkMode));
$validator->check(json_decode($input), $schema);

if ([] !== $errors) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This one, too...

$validator = new Validator(new Factory($schemaStorage, null, $checkMode));
$validator->check(json_decode($input, true), $schema);

if ([] !== $errors) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

one more...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

who is using 5.3 nowadays? anyway, fixed both ;)

Copy link

Choose a reason for hiding this comment

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

@digitalkaoz Composer still supports PHP 5.3 users today, and it uses this library

Copy link
Collaborator

Choose a reason for hiding this comment

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

@stof is there a plan/policy for Composer to adhere to PHP supported version lifespans?

@digitalkaoz digitalkaoz force-pushed the master branch 2 times, most recently from fab3d3c to f452047 Compare October 10, 2016 06:41
@digitalkaoz
Copy link
Contributor Author

@shmax im getting some weird errors in my builds (which werent there before your merged PR) any idea where Undefined property: JsonSchema\Constraints\UndefinedConstraint::$schemaStorage comes from? cant find it anywhere in the code

@staabm
Copy link
Contributor

staabm commented Oct 10, 2016

@digitalkaoz per travis the stack for this warning is

1) JsonSchema\Tests\Constraints\AdditionalPropertiesTest::testInvalidCases with data set #0 ('{\n                  "prop":"1...     }', '{\n                  "type":"o...     }', null, array(array('', '', 'The property additionalProp i...erties', 'additionalProp')))

Undefined property: JsonSchema\Constraints\UndefinedConstraint::$schemaStorage

/home/travis/build/justinrainbow/json-schema/src/JsonSchema/Constraints/UndefinedConstraint.php:63
/home/travis/build/justinrainbow/json-schema/src/JsonSchema/Constraints/UndefinedConstraint.php:41
/home/travis/build/justinrainbow/json-schema/src/JsonSchema/Constraints/Constraint.php:181
/home/travis/build/justinrainbow/json-schema/src/JsonSchema/Constraints/SchemaConstraint.php:30
/home/travis/build/justinrainbow/json-schema/src/JsonSchema/Validator.php:36
/home/travis/build/justinrainbow/json-schema/tests/Constraints/BaseTestCase.php:41

@staabm
Copy link
Contributor

staabm commented Oct 10, 2016

@digitalkaoz
Copy link
Contributor Author

@staabm thanks. i had to rebase once more, should be fixed now

@shmax
Copy link
Collaborator

shmax commented Oct 10, 2016

getting some weird errors in my builds

Ah, sorry about that--snuck another PR in there ahead of you. I don't have any more surprises, so happy merging...

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.

6 participants