Skip to content

[Translator] fix performance issue in MessageCatalogue and catalogue operations #35125

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

Merged
merged 1 commit into from
Dec 31, 2019
Merged

Conversation

ArtemBrovko
Copy link
Contributor

Q A
Branch? 3.4
Bug fix? no
New feature? no
Deprecations? no
Tickets -
License MIT
Doc PR -

In our project we use lots of catalogue operations during importing of translations to our system and we ran into performance issue. Code profiler showed lots or array_replace calls in MessageCatalogue::add method. This method is actually called by MessageCatalogue::set, which is quite an overkill, because MessageCatalogue::set is meant to set only one translation at a time. Method was reworked. MergeOperation and TargetOperation was reworked as well to use this improved MessageCatalogue::set method instead of constructing array with only one translation and passing it to MessageCatalogue::add method.

Table shows execution time before and after

Time in seconds (avg. of 10 executions)
Before 50
After 8

Looks like 4.* and 5.* versions can also be improved by the same changes.

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Dec 28, 2019

Can you try replacing the call to array_replace() by a foreach over $messages (and keeping set() as-is).
What's the perf?

@ArtemBrovko
Copy link
Contributor Author

Did that, that leads to same performance impovement. foreach over $messages is as good as what i did in terms of speed. Should I update pull request?

@nicolas-grekas
Copy link
Member

Yes please, because that would improve the performance of add() too, and would make the patch shorter.

@nicolas-grekas nicolas-grekas added this to the 3.4 milestone Dec 28, 2019
@ArtemBrovko
Copy link
Contributor Author

Done

@nicolas-grekas nicolas-grekas changed the title [Translator] Performance improvement in MessageCatalogue and catalogue operations. [Translator] fix performance issue in MessageCatalogue and catalogue operations. Dec 31, 2019
@nicolas-grekas nicolas-grekas changed the title [Translator] fix performance issue in MessageCatalogue and catalogue operations. [Translator] fix performance issue in MessageCatalogue and catalogue operations Dec 31, 2019
@nicolas-grekas
Copy link
Member

Thank you @ArtemBrovko.

nicolas-grekas added a commit that referenced this pull request Dec 31, 2019
… catalogue operations (ArtemBrovko)

This PR was merged into the 3.4 branch.

Discussion
----------

[Translator] fix performance issue in MessageCatalogue and catalogue operations

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

In our project we use lots of catalogue operations during importing of translations to our system and we ran into performance issue. Code profiler showed lots or `array_replace` calls in  [MessageCatalogue::add](https://github.com/symfony/symfony/blob/3.4/src/Symfony/Component/Translation/MessageCatalogue.php#L128) method. This method is actually called by [MessageCatalogue::set](https://github.com/symfony/symfony/blob/3.4/src/Symfony/Component/Translation/MessageCatalogue.php#L70), which is quite an overkill, because `MessageCatalogue::set` is meant to set only one translation at a time. Method was reworked. `MergeOperation` and `TargetOperation` was reworked as well to use this improved `MessageCatalogue::set` method instead of constructing array with only one translation and passing it to `MessageCatalogue::add` method.

Table shows execution time before and after

|  | Time in seconds (avg. of 10 executions)
----------- | ------
Before | 50
After | 8

Looks like 4.* and 5.* versions can also be improved by the same changes.
<!--
Replace this notice by a short README for your feature/bugfix. This will help people
understand your PR and can be used as a start for the documentation.

Additionally (see https://symfony.com/roadmap):
 - Always add tests and ensure they pass.
 - Never break backward compatibility (see https://symfony.com/bc).
 - Bug fixes must be submitted against the lowest maintained branch where they apply
   (lowest branches are regularly merged to upper ones so they get the fixes too.)
 - Features and deprecations must be submitted against branch master.
-->

Commits
-------

5179af4 [Translator] Performance improvement in MessageCatalogue and catalogue operations.
@nicolas-grekas nicolas-grekas merged commit 5179af4 into symfony:3.4 Dec 31, 2019
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.

4 participants