8000 [Cache] Optimize ArrayAdapter by using a generator by aurimasniekis · Pull Request #17435 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Cache] Optimize ArrayAdapter by using a generator #17435

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Added generators to optimize code
  • Loading branch information
Aurimas Niekis committed Jan 19, 2016
commit 0e0ff3b1ae24695d1e007b6cf808414137ca9455
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"symfony/yaml": "self.version"
},
"require-dev": {
"cache/integration-tests": "^0.6",
"cache/integration-tests": "^0.7",
"doctrine/cache": "~1.6",
"doctrine/data-fixtures": "1.0.*",
"doctrine/dbal": "~2.4",
Expand Down
20 changes: 13 additions & 7 deletions src/Symfony/Component/Cache/Adapter/ArrayAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,11 @@ public function getItem($key)
*/
public function getItems(array $keys = array())
{
$f = $this->createCacheItem;
$items = array();
$now = time();

foreach ($keys as $key) {
$isHit = isset($this->expiries[$this->validateKey($key)]) && ($this->expiries[$key] >= $now || !$this->deleteItem($key));
$items[$key] = $f($key, $isHit ? $this->values[$key] : null, $isHit);
$this->validateKey($key);
}

return $items;
return $this->createGenerator($keys);
}

/**
Expand Down Expand Up @@ -177,4 +172,15 @@ private function validateKey($key)

return $key;
}

private function createGenerator(array $keys)
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure splitting this to a private provides any benefit, do you?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

By doing this, I am getting key validation before returning a generator. Otherwise, you will not get invalid key exception until you call the first iteration

Copy link
Member

Choose a reason for hiding this comment

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

@nicolas-grekas this is about making some logic running before the creation of the generator. If you inline the method, all the code is part of the generator and so runs only when you start iterating. Here, the code before the private method is not part of the generator logic.

Copy link
Member

Choose a reason for hiding this comment

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

Oh ok, of course!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's possible to do inline function but I thought from code style approach it's not a good way

/**
     * {@inheritdoc}
     */
    public function getItems(array $keys = array())
    {
        foreach ($keys as $key) {
            $this->validateKey($key);
        }

        return (function (array $keys) {
            $f = $this->createCacheItem;
            $now = time();

            foreach ($keys as $key) {
                $isHit = isset($this->expiries[$key]) && ($this->expiries[$key] >= $now || !$this->deleteItem($key));

                yield $key => $f($key, $isHit ? $this->values[$key] : null, $isHit);
            }
        })($keys);
    }

Copy link
Member

Choose a reason for hiding this comment

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

I prefer the private method :)

Copy link
Member

Choose a reason for hiding this comment

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

this is still not inlining the function. It still uses a separate function, even though it is anonymous rather than named.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry for incorrect naming

{
$f = $this->createCacheItem;

foreach ($keys as $key) {
$isHit = isset($this->expiries[$key]) && ($this->expiries[$key] >= time() || !$this->deleteItem($key));

yield $key => $f($key, $isHit ? $this->values[$key] : null, $isHit);
}
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/Cache/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"psr/cache": "~1.0"
},
"require-dev": {
"cache/integration-tests": "^0.6",
"cache/integration-tests": "^0.7",
"doctrine/cache": "~1.6"
},
"autoload": {
Expand Down
0