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 default field resolver #783

Merged
merged 1 commit into from
Nov 30, 2020

Conversation

murtukov
Copy link
Member

@murtukov murtukov commented Nov 13, 2020

Q A
Bug fix? no
New feature? no
BC breaks? no
Deprecations? yes
Tests pass? yes
License MIT

Made changes to the default field resolver:

  • 1. Replace self with static for a better extensibility

  • 2. Add checking of getters that start with has prefix (like in PropertyAccess)

  • 3. Reduce function calls (micro-optimisation):

    This call:

    str_replace(' ', '', ucwords(str_replace('_', ' ', $fieldName)))

    is equal to this:

    str_replace('_', '', ucwords($fieldName, '_'))

    Additionally, PHP method names are case-insensitive, which means it is not required to call ucwords. In other words these 2 calls are equal: $object->getMyParentPosts() and $object->getmyparentposts(). So it can be reduced to a single function call:

    str_replace('_', '', $fieldName)

    Also successive calls to guessObjectMethod were replaced with a foreach loop, which is faster.

Result

As a result it saves unnecessary function calls (on each type's field):

  • str_replace
  • uswords
  • guessObjectMethod (1 to 3 times, depending on prefix)

If user requests a collection of 200 objects with 10 fields each:

  • best case (only get prefixes): 200 objects x 10 fields x 3 functions = 6000 calls saved
  • worst case (only '' prefixes): 200 objects x 10 fields x 5 functions = 10000 calls saved

@murtukov murtukov force-pushed the refactor/default-field-resolver branch from 486c043 to 3678e94 Compare November 13, 2020 18:16
@murtukov murtukov merged commit 2b954fd into overblog:master Nov 30, 2020
@murtukov murtukov deleted the refactor/default-field-resolver branch November 30, 2020 10:54
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.

2 participants