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

perf: use XXH3 instead of MD5 #6487

Merged
merged 2 commits into from
Jul 21, 2024
Merged
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
6 changes: 3 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ Recommendations:

* don't change existing tests if possible
* always add a new `ApiResource` or a new `Entity/Document` to add a new test instead of changing an existing class
* as of API Platform 3 each component has it's own test directory, avoid the `tests/` directory except for functional tests
* as of API Platform 3 each component has its own test directory, avoid the `tests/` directory except for functional tests
* dependencies between components must be kept at its minimal (`api-platform/metadata`, `api-platform/state`) except for bridges (Doctrine, Symfony, Laravel etc.)
* for functional testing with phpunit (see `tests/Functional`, add your ApiResource to `ApiPlatform\Tests\Fixtures\PhpUnitResourceNameCollectionFactory`)

Expand All @@ -139,11 +139,11 @@ https://github.com/api-platform/core/blob/002c8b25283c9c06a085945f6206052a99a5fb

To launch unit tests:

vendor/bin/simple-phpunit --stop-on-defect -vvv
vendor/bin/phpunit --stop-on-defect

If you want coverage, you will need the `pcov` PHP extension and run:

vendor/bin/simple-phpunit --coverage-html coverage -vvv --stop-on-failure
vendor/bin/phpunit --coverage-html coverage --stop-on-defect

Sometimes there might be an error with too many open files when generating coverage. To fix this, you can increase the `ulimit`, for example:

Expand Down
2 changes: 1 addition & 1 deletion features/http_cache/headers.feature
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ Feature: Default values of HTTP cache headers
Scenario: Cache headers default value
When I send a "GET" request to "/relation_embedders"
Then the response status code should be 200
And the header "Etag" should be equal to '"7bfa587950d675e222660f68623f5f89"'
And the header "Etag" should be equal to '"032297ac74d75a50"'
And the header "Cache-Control" should be equal to "max-age=60, public, s-maxage=3600"
And the header "Vary" should be equal to "Accept, Cookie"
2 changes: 1 addition & 1 deletion src/Doctrine/Odm/Tests/DoctrineMongoDbOdmSetup.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
$namespace .= ':';
}

$cache->setNamespace($namespace.'dc2_'.md5($proxyDir.$hydratorDir).'_'); // to avoid collisions
$cache->setNamespace($namespace.'dc2_'.hash('xxh3', $proxyDir.$hydratorDir).'_'); // to avoid collisions

Check warning on line 96 in src/Doctrine/Odm/Tests/DoctrineMongoDbOdmSetup.php

View check run for this annotation

Codecov / codecov/patch

src/Doctrine/Odm/Tests/DoctrineMongoDbOdmSetup.php#L96

Added line #L96 was not covered by tests

return $cache;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Elasticsearch/Paginator.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@
$denormalizationContext = array_merge([AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => true], $this->denormalizationContext);

foreach ($this->documents['hits']['hits'] ?? [] as $document) {
$cacheKey = isset($document['_index'], $document['_id']) ? md5("{$document['_index']}_{$document['_id']}") : null;
$cacheKey = isset($document['_index'], $document['_id']) ? hash('xxh3', "{$document['_index']}_{$document['_id']}") : null;

Check warning on line 98 in src/Elasticsearch/Paginator.php

View check run for this annotation

Codecov / codecov/patch

src/Elasticsearch/Paginator.php#L98

Added line #L98 was not covered by tests

if ($cacheKey && \array_key_exists($cacheKey, $this->cachedDenormalizedDocuments)) {
$object = $this->cachedDenormalizedDocuments[$cacheKey];
Expand Down
2 changes: 1 addition & 1 deletion src/HttpCache/State/AddHeadersProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
$resourceCacheHeaders = $operation->getCacheHeaders() ?? [];

if ($this->etag && !$response->getEtag()) {
$response->setEtag(md5((string) $content));
$response->setEtag(hash('xxh3', (string) $content));
}

if (null !== ($maxAge = $resourceCacheHeaders['max_age'] ?? $this->maxAge) && !$response->headers->hasCacheControlDirective('max-age')) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(CacheItemPoolInterface $cacheItemPool, private reado
*/
public function create(string $resourceClass, string $property, array $options = []): ApiProperty
{
$cacheKey = self::CACHE_KEY_PREFIX.md5(serialize([$resourceClass, $property, $options]));
$cacheKey = self::CACHE_KEY_PREFIX.hash('xxh3', serialize([$resourceClass, $property, $options]));

return $this->getCached($cacheKey, fn (): ApiProperty => $this->decorated->create($resourceClass, $property, $options));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(CacheItemPoolInterface $cacheItemPool, private reado
*/
public function create(string $resourceClass, array $options = []): PropertyNameCollection
{
$cacheKey = self::CACHE_KEY_PREFIX.md5(serialize([$resourceClass, $options]));
$cacheKey = self::CACHE_KEY_PREFIX.hash('xxh3', serialize([$resourceClass, $options]));

return $this->getCached($cacheKey, fn (): PropertyNameCollection => $this->decorated->create($resourceClass, $options));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct(private readonly CacheItemPoolInterface $cacheItemPo
*/
public function create(string $resourceClass): ResourceMetadataCollection
{
$cacheKey = self::CACHE_KEY_PREFIX.md5($resourceClass);
$cacheKey = self::CACHE_KEY_PREFIX.hash('xxh3', $resourceClass);
if (\array_key_exists($cacheKey, $this->localCache)) {
return new ResourceMetadataCollection($resourceClass, $this->localCache[$cacheKey]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,6 @@

private function generateCacheKey(string $resourceClass = Dummy::class, string $property = 'dummy', array $options = []): string
{
return CachedPropertyMetadataFactory::CACHE_KEY_PREFIX.md5(serialize([$resourceClass, $property, $options]));
return CachedPropertyMetadataFactory::CACHE_KEY_PREFIX.hash('xxh3', serialize([$resourceClass, $property, $options]));

Check warning on line 95 in src/Metadata/Tests/Property/Factory/CachedPropertyMetadataFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Tests/Property/Factory/CachedPropertyMetadataFactoryTest.php#L95

Added line #L95 was not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,6 @@

private function generateCacheKey(string $resourceClass = Dummy::class, array $options = []): string
{
return CachedPropertyNameCollectionFactory::CACHE_KEY_PREFIX.md5(serialize([$resourceClass, $options]));
return CachedPropertyNameCollectionFactory::CACHE_KEY_PREFIX.hash('xxh3', serialize([$resourceClass, $options]));

Check warning on line 95 in src/Metadata/Tests/Property/Factory/CachedPropertyNameCollectionFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Tests/Property/Factory/CachedPropertyNameCollectionFactoryTest.php#L95

Added line #L95 was not covered by tests
}
}
8 changes: 4 additions & 4 deletions tests/TestSuiteConfigCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
final class TestSuiteConfigCache implements ConfigCacheInterface
{
/** @var array<string, string> */
public static $md5 = [];
public static array $hashes = [];

public function __construct(private readonly ConfigCacheInterface $decorated)
{
Expand All @@ -32,8 +32,8 @@
public function isFresh(): bool
{
$p = $this->getPath();
if (!isset(static::$md5[$p]) || static::$md5[$p] !== $this->getHash()) {
static::$md5[$p] = $this->getHash();
if (!isset(self::$hashes[$p]) || self::$hashes[$p] !== $this->getHash()) {
self::$hashes[$p] = $this->getHash();

Check warning on line 36 in tests/TestSuiteConfigCache.php

View check run for this annotation

Codecov / codecov/patch

tests/TestSuiteConfigCache.php#L35-L36

Added lines #L35 - L36 were not covered by tests

return false;
}
Expand All @@ -48,6 +48,6 @@

private function getHash(): string
{
return md5_file(__DIR__.'/Fixtures/app/var/resources.php');
return hash_file('xxh3', __DIR__.'/Fixtures/app/var/resources.php');

Check warning on line 51 in tests/TestSuiteConfigCache.php

View check run for this annotation

Codecov / codecov/patch

tests/TestSuiteConfigCache.php#L51

Added line #L51 was not covered by tests
}
}
Loading