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

Optimize TypeRegistry::lookupName() from O(N) to O(1) #6082

Merged
merged 2 commits into from
Jul 15, 2023

Conversation

mvorisek
Copy link
Contributor

@mvorisek mvorisek commented Jul 1, 2023

Q A
Type performance issue
Fixed issues n/a

Summary

We use a lot of custom types and migration from Type::getName() to TypeRegistry->lookupName() discovered us the reverse lookup was very slow (O(N)). This PR makes it O(1).

Index is initialized lazily on the 1st reverse lookup. The type registry is expected to not hold duplicate type instances and this PR fixes it, in #6083 (comment) I was requested to fix it in this PR instead of a separate one.

@mvorisek mvorisek marked this pull request as draft July 1, 2023 15:08
@mvorisek mvorisek force-pushed the optimize_reverse_type_lookup branch 6 times, most recently from 5029656 to a5e6e82 Compare July 1, 2023 15:44
@mvorisek mvorisek force-pushed the optimize_reverse_type_lookup branch from a5e6e82 to 7f3c2bc Compare July 9, 2023 21:46
@mvorisek mvorisek force-pushed the optimize_reverse_type_lookup branch from 7f3c2bc to 71f1411 Compare July 13, 2023 08:04
@mvorisek mvorisek marked this pull request as ready for review July 13, 2023 08:05
Copy link
Member

@derrabus derrabus left a comment

Choose a reason for hiding this comment

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

Since this a performance improvement and not a bugfix, let's target 3.7.x.

src/Types/TypeRegistry.php Outdated Show resolved Hide resolved
@derrabus derrabus changed the base branch from 3.6.x to 3.7.x July 15, 2023 13:29
@derrabus derrabus changed the base branch from 3.7.x to 3.6.x July 15, 2023 13:29
@derrabus derrabus changed the base branch from 3.6.x to 3.7.x July 15, 2023 13:30
@derrabus derrabus added this to the 3.7.0 milestone Jul 15, 2023
@mvorisek mvorisek force-pushed the optimize_reverse_type_lookup branch from 71f1411 to 92a3c40 Compare July 15, 2023 13:53
@mvorisek
Copy link
Contributor Author

mvorisek commented Jul 15, 2023

Since this a performance improvement and not a bugfix, let's target 3.7.x.

Is there any chance this can go into 3.6.x? We use lookups heavily, thus for us the O(N) performance is a "bug".

@mvorisek mvorisek force-pushed the optimize_reverse_type_lookup branch 3 times, most recently from b3b09ad to ccfe3e6 Compare July 15, 2023 14:10
@mvorisek mvorisek requested a review from derrabus July 15, 2023 14:12
@derrabus
Copy link
Member

Is there any chance this can into 3.6.x?

No.

src/Types/TypeRegistry.php Outdated Show resolved Hide resolved
@mvorisek mvorisek force-pushed the optimize_reverse_type_lookup branch from ccfe3e6 to 33be3f8 Compare July 15, 2023 14:21
@mvorisek mvorisek force-pushed the optimize_reverse_type_lookup branch from 33be3f8 to 430c9ae Compare July 15, 2023 14:23
@derrabus
Copy link
Member

Thank you for this very nice improvement, @mvorisek!

@derrabus derrabus merged commit b30518d into doctrine:3.7.x Jul 15, 2023
@mvorisek mvorisek deleted the optimize_reverse_type_lookup branch July 15, 2023 14:48
derrabus added a commit to derrabus/dbal that referenced this pull request Jul 15, 2023
* 3.7.x:
  Optimize TypeRegistry::lookupName() from O(N) to O(1) (doctrine#6082)
  Add documentation for doctrine#6044 (doctrine#6069)
  Use triggerIfCalledFromOutside in listTableDetails
  Document alternative to Type::getName (doctrine#6077)
derrabus added a commit to derrabus/dbal that referenced this pull request Jul 15, 2023
* 3.7.x:
  Optimize TypeRegistry::lookupName() from O(N) to O(1) (doctrine#6082)
  Add documentation for doctrine#6044 (doctrine#6069)
  Use triggerIfCalledFromOutside in listTableDetails
  Document alternative to Type::getName (doctrine#6077)
derrabus added a commit to derrabus/dbal that referenced this pull request Jul 24, 2023
* 3.7.x:
  Avoid self deprecation about listTableColumn (doctrine#6108)
  Fix blob binding overwrite on DB2 (doctrine#6093)
  Optimize TypeRegistry::lookupName() from O(N) to O(1) (doctrine#6082)
  Add documentation for doctrine#6044 (doctrine#6069)
  Use triggerIfCalledFromOutside in listTableDetails
  Document alternative to Type::getName (doctrine#6077)
derrabus pushed a commit that referenced this pull request Aug 22, 2023
|      Q       |   A
|------------- | -----------
| Type         | feature
| Fixed issues | n/a

#### Summary

`Type` class has static methods for global `TypeRegistry` instance
shorter access like `Type::getType`.

This feature adds a static method for `TypeRegistry::lookupName`.

Thanks to #6082, the reverse lookup
is O(1) thus fine to be used massively - for ex. schema introspection
returns type as object, and this method is needed to obtain a string
name.

In DBAL 4.0, the method can be renamed to `Type::getName(Type $type):
string` to be more consistent with `Type::getType(string $name): Type`.
cgknx pushed a commit to cgknx/dbal that referenced this pull request Aug 30, 2023
|      Q       |   A
|------------- | -----------
| Type         | performance issue
| Fixed issues | n/a

#### Summary

We use a lot of custom types and migration from `Type::getName()` to
`TypeRegistry->lookupName()` discovered us the reverse lookup was very
slow (O(N)). This PR makes it O(1).

~Index is initialized lazily on the 1st reverse lookup.~ The type
registry is expected to not hold duplicate type instances and this PR
fixes it, in
doctrine#6083 (comment) I was
requested to fix it in this PR instead of a separate one.
cgknx pushed a commit to cgknx/dbal that referenced this pull request Aug 30, 2023
|      Q       |   A
|------------- | -----------
| Type         | feature
| Fixed issues | n/a

#### Summary

`Type` class has static methods for global `TypeRegistry` instance
shorter access like `Type::getType`.

This feature adds a static method for `TypeRegistry::lookupName`.

Thanks to doctrine#6082, the reverse lookup
is O(1) thus fine to be used massively - for ex. schema introspection
returns type as object, and this method is needed to obtain a string
name.

In DBAL 4.0, the method can be renamed to `Type::getName(Type $type):
string` to be more consistent with `Type::getType(string $name): Type`.
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants