Skip to content

[Translator] Further postpone adding resource files #27063

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
Apr 29, 2018

Conversation

nicolas-grekas
Copy link
Member

Q A
Branch? 4.1
Bug fix? no
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #27010
License MIT
Doc PR -

Copy link
Contributor

@dmaicher dmaicher left a comment

Choose a reason for hiding this comment

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

👍

Copy link

@grisendo grisendo left a comment

Choose a reason for hiding this comment

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

This is my first review, I don't want to sound aggresive or not polite or something, sorry if that's the case, it's not my intention at all 😃

@@ -75,7 +77,7 @@ public function __construct(ContainerInterface $container, MessageFormatterInter

$this->options = array_merge($this->options, $options);
$this->resourceLocales = array_keys($this->options['resource_files']);
$this->addResourceFiles($this->options['resource_files']);
$this->resourceFiles = $this->options['resource_files'];

Choose a reason for hiding this comment

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

This will create a copy of $this->options['resource_files'] array, right? Wouldn't it be better for memory to have a $resourceFilesLoaded boolean attribute instead, and use $this->options['resource_files'] later to iterate at addResourceFiles?

Copy link
Contributor

Choose a reason for hiding this comment

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

This will only copy the array of there is a modification to the array, if we don't touch to the array the copy-on-write mechanism will just keep a second reference to the same array.

Copy link
Member Author

Choose a reason for hiding this comment

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

actually, thanks to copy-on-write, no duplication will ever happen

Choose a reason for hiding this comment

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

Then, is ok for me also 😀

@@ -46,6 +46,8 @@ class Translator extends BaseTranslator implements WarmableInterface
*/
private $resources = array();

private $resourceFiles;

Choose a reason for hiding this comment

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

I think this could be a "private $resourceFilesLoaded" boolean (see next comment).

@@ -103,6 +105,9 @@ public function warmUp($cacheDir)

public function addResource($format, $resource, $locale, $domain = null)
{
if ($this->resourceFiles) {

Choose a reason for hiding this comment

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

This could be "if (!$this->resourceFilesLoaded) {"

@@ -117,6 +122,9 @@ protected function initializeCatalogue($locale)

protected function initialize()
{
if ($this->resourceFiles) {

Choose a reason for hiding this comment

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

This could also be "if (!$this->resourceFilesLoaded) { "

{
$filesByLocale = $this->resourceFiles;
Copy link

@grisendo grisendo Apr 26, 2018

Choose a reason for hiding this comment

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

With this assignment, the resources are cloned again, and now we have 3 times the same content for a moment. We already have $this->options['resource_files'] to iterate in the next line

@@ -103,6 +105,9 @@ public function warmUp($cacheDir)

public function addResource($format, $resource, $locale, $domain = null)
{
if ($this->resourceFiles) {
$this->addResourceFiles();
Copy link
Member

Choose a reason for hiding this comment

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

do we need to do it here ?

We could ensure we do it only on initialize by calling parent::addResource for the resource files instead of $this->addResource (avoiding to copy them first from resourceFiles to resources, to add them in the parent later.

As initialize will process resourceFiles before resources, the other will be preserved.

Copy link
Member Author

Choose a reason for hiding this comment

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

one could call the constructor, then addResource on the object, isn't it?

Copy link
Member Author

Choose a reason for hiding this comment

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

(I confirm, and this is already covered by tests :) )

@nicolas-grekas nicolas-grekas merged commit 2aa62df into symfony:master Apr 29, 2018
nicolas-grekas added a commit that referenced this pull request Apr 29, 2018
…olas-grekas)

This PR was merged into the 4.1-dev branch.

Discussion
----------

[Translator] Further postpone adding resource files

| Q             | A
| ------------- | ---
| Branch?       | 4.1
| Bug fix?      | no
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #27010
| License       | MIT
| Doc PR        | -

Commits
-------

2aa62df [Translator] Further postpone adding resource files
@nicolas-grekas nicolas-grekas deleted the translator-lazy branch April 30, 2018 01:25
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.

8 participants