8000 minor #47903 [Asset] improve performance of `UrlPackage::chooseBaseUr… · symfony/symfony@0de91ed · GitHub
[go: up one dir, main page]

Skip to content

Commit 0de91ed

Browse files
committed
minor #47903 [Asset] improve performance of UrlPackage::chooseBaseUrl() (kbond)
This PR was merged into the 6.2 branch. Discussion ---------- [Asset] improve performance of `UrlPackage::chooseBaseUrl()` | Q | A | ------------- | --- | Branch? | 6.2 | Bug fix? | no | New feature? | no | Deprecations? | no | Tickets | n/a | License | MIT | Doc PR | n/a `@frankdejonge` discovered this performance improvement when implementing a similar feature in flysystem (thephpleague/flysystem#1570). A simple benchmark by me indicated it was 3-4x faster. Note: Sites using this feature that have assets heavily cached would now have a different distribution. Commits ------- 35dff8f [Asset] improve performance of `UrlPackage::chooseBaseUrl()`
2 parents 43a56e8 + 35dff8f commit 0de91ed

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

src/Symfony/Component/Asset/Tests/UrlPackageTest.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ public function getConfigs()
4848
['file:///example/com/foo/', '', 'foo', 'file:///example/com/foo/foo?v1'],
4949

5050
[['http://example.com'], '', '/foo', 'http://example.com/foo?v1'],
51-
[['http://example.com', 'http://example.net'], '', '/foo', 'http://example.com/foo?v1'],
52-
[['http://example.com', 'http://example.net'], '', '/fooa', 'http://example.net/fooa?v1'],
53-
[['file:///example/com', 'file:///example/net'], '', '/foo', 'file:///example/com/foo?v1'],
54-
[['ftp://example.com', 'ftp://example.net'], '', '/fooa', 'ftp://example.net/fooa?v1'],
51+
[['http://example.com', 'http://example.net'], '', '/foo', 'http://example.net/foo?v1'],
52+
[['http://example.com', 'http://example.net'], '', '/fooa', 'http://example.com/fooa?v1'],
53+
[['file:///example/com', 'file:///example/net'], '', '/foo', 'file:///example/net/foo?v1'],
54+
[['ftp://example.com', 'ftp://example.net'], '', '/fooa', 'ftp://example.com/fooa?v1'],
5555

5656
['http://example.com', 'version-%2$s/%1$s', '/foo', 'http://example.com/version-v1/foo'],
5757
['http://example.com', 'version-%2$s/%1$s', 'foo', 'http://example.com/version-v1/foo'],
@@ -77,15 +77,16 @@ public function getContextConfigs()
7777
return [
7878
[false, 'http://example.com', '', 'foo', 'http://example.com/foo?v1'],
7979
[false, ['http://example.com'], '', 'foo', 'http://example.com/foo?v1'],
80-
[false, ['http://example.com', 'https://example.com'], '', 'foo', 'http://example.com/foo?v1'],
81-
[false, ['http://example.com', 'https://example.com'], '', 'fooa', 'https://example.com/fooa?v1'],
80+
[false, ['http://example.com', 'https://example.com'], '', 'foo', 'https://example.com/foo?v1'],
81+
[false, ['http://example.com', 'https://example.com'], '', 'fooa', 'http://example.com/fooa?v1'],
8282
[false, ['http://example.com/bar'], '', 'foo', 'http://example.com/bar/foo?v1'],
8383
[false, ['http://example.com/bar/'], '', 'foo', 'http://example.com/bar/foo?v1'],
8484
[false, ['//example.com/bar/'], '', 'foo', '//example.com/bar/foo?v1'],
8585

8686
[true, ['http://example.com'], '', 'foo', 'http://example.com/foo?v1'],
8787
[true, ['http://example.com', 'https://example.com'], '', 'foo', 'https://example.com/foo?v1'],
88-
[true, ['', 'https://example.com'], '', 'foo', '/foo?v1'],
88+
[true, ['', 'https://example.com'], '', 'foo', 'https://example.com/foo?v1'],
89+
[true, ['', 'https://example.com'], '', 'bar', '/bar?v1'],
8990
];
9091
}
9192

src/Symfony/Component/Asset/UrlPackage.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ public function getBaseUrl(string $path): string
107107
*/
108108
protected function chooseBaseUrl(string $path): int
109109
{
110-
return (int) fmod(hexdec(substr(hash('sha256', $path), 0, 10)), \count($this->baseUrls));
110+
return abs(crc32($path)) % \count($this->baseUrls);
111111
}
112112

113113
private function getSslUrls(array $urls)

0 commit comments

Comments
 (0)
0