Skip to content

Fixes property parsing being invoked inefficiently - #132

Merged
janpecha merged 3 commits into
Tharos:developfrom
dakujem:develop
Sep 8, 2018
Merged

janpecha merged 3 commits into
Tharos:developfrom
dakujem:develop

Conversation

@dakujem

@dakujem dakujem commented Sep 5, 2018

Copy link
Copy Markdown
Contributor

The problem:

  • property parsing was being triggered multiple times for the same files, exponentially in relation to the ancestry depth

The solution:

  • do not call property parsing in the constructor
  • parse properties on first access

The result:

  • the complexity of parsing is reduced from exponential complexity to linear complexity, in relation to the depth of ancestry of the final entity classes

The test that shows the problem can be seen here:
https://github.com/dakujem/LeanMapper/blob/props-parsing-hell/tests/LeanMapper/EntityReflection.parsing.phpt

The tests for the fixed version can be found here:
https://github.com/dakujem/LeanMapper/blob/props-parsing-test/tests/LeanMapper/EntityReflection.parsing.phpt

I did not include the test intentionally, as it requires a static hook in the EntityReflection class or changing the visibility of methods in the class (private->protected). I am open to discussion.

@janpecha

janpecha commented Sep 5, 2018

Copy link
Copy Markdown
Collaborator

Cool, thanks! I'll look at it tomorrow. Only one note - it breaks some tests, can you fix them? Just change

$reflection = Abc::getReflection()

to

$reflection = Abc::getReflection();
$reflection->getEntityProperties();

Thanks!

@dakujem

dakujem commented Sep 6, 2018

Copy link
Copy Markdown
Contributor Author

I see. Now that the props are lazy loaded, the constructor does not trigger the errors. I will update the tests.

Anyway, what is your configuration to run th tests? I did not find any info on running tests, or contributing in general.

@janpecha

janpecha commented Sep 6, 2018

Copy link
Copy Markdown
Collaborator

I use vendor/bin/tester -p php -c tests/php-unix.ini tests/ (on Ubuntu 16.04 with PHP 7.2).

  1. composer install
  2. edit tests/php-unix.ini (if needed)
  3. vendor/bin/tester -p php -c tests/php-unix.ini tests/

@dakujem

dakujem commented Sep 6, 2018

Copy link
Copy Markdown
Contributor Author

I updated the failing test. I also added another optimization (I forgot to include it yesterday) that enables on-demand getter/setter initialization. The performance impact of it is not so remarkable, but removes some unncessary method name parsings.

@dakujem

dakujem commented Sep 6, 2018

Copy link
Copy Markdown
Contributor Author

By the way, the tests fail using PHP 7.2.4:

-- FAILED: Entity.emptyQuery.phpt
   Exited with error code 255 (expected 0)
   E_WARNING: count(): Parameter must be an array or an object that implements Countable

   in tests\LeanMapper\Entity.emptyQuery.phpt(38)

-- FAILED: Entity.assign.1.phpt
   Exited with error code 255 (expected 0)
   E_DEPRECATED: The each() function is deprecated. This message will be suppressed on further calls

   in tests\LeanMapper\Entity.assign.1.phpt(77)
   in tests\LeanMapper\Entity.assign.1.phpt(77) each()

@dakujem

dakujem commented Sep 6, 2018

Copy link
Copy Markdown
Contributor Author

vendor/bin/tester -p php -c tests/php-unix.ini tests/ does not really work, because at least sqlite is needed. I'm running the tests in my local environment using tester v2 an the switch -C (using my local cli configuration). I suggest using this configuration, contributing will be easier for general public.

I'm also using this snippet in composer.json:

	"scripts": {
		"test":"tester tests -C"
	}

then it is very easy to just type composer test and have the tests running.

@janpecha

janpecha commented Sep 6, 2018 •

Copy link
Copy Markdown
Collaborator

I updated the failing test.

Please also update Entity.hasMany.inversed.phpt (see https://travis-ci.org/Tharos/LeanMapper/builds/425134127)

on-demand getter/setter initialization

You read my mind :)

...tests fail using PHP 7.2.4

Have you latest version? It was fixed in 62a7363.

because at least sqlite is needed.

Yeah, the tests currently require json, pdo, sqlite3 and tokenizer extensions (on my configuration). They are listed in php-unix.ini.

- property parsing was being triggered multiple times for the same files, exponentially in relation to the ancestry depth
@dakujem

dakujem commented Sep 7, 2018

Copy link
Copy Markdown
Contributor Author

Sorry, guys. I did not realize that I had forked the repo before 🤣 I was thinking I was working on the latest version, because I clicked the fork button on the day I created the PR...
Anyway, I synced the repo and all the tests are passing now. Even those 7.2 issues disappeared, as you mentioned.

@janpecha I believe the PR is now ready to be merged.

@janpecha

janpecha commented Sep 7, 2018

Copy link
Copy Markdown
Collaborator

@dakujem Can you squash commits into single commit with message (for example):

EntityReflection: improved performance

Added on-demand property parsing and getter/setter initialization.

Thanks!

@dakujem

dakujem commented Sep 7, 2018

Copy link
Copy Markdown
Contributor Author

Hey, I thought GitHub offers this feature when merging. If not, let me know and I will squash.

@janpecha
janpecha merged commit f3b7e3d into Tharos:develop Sep 8, 2018
@janpecha

janpecha commented Sep 8, 2018 •

Copy link
Copy Markdown
Collaborator

Yes, Github offers this feature, but from my point of view is better if author of PR prepare it because final commit is tested on CI once again before merging.

Merged, thanks!

@dakujem

dakujem commented Sep 8, 2018

Copy link
Copy Markdown
Contributor Author

I will do that next time.

janpecha added a commit to inlm/LeanMapper that referenced this pull request Feb 10, 2019
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