Skip to content

Commit

Permalink
fix: reduce memory consumption during full indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
sitepark-veltrup committed May 15, 2024
1 parent 0543849 commit 6d64470
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 15 deletions.
13 changes: 9 additions & 4 deletions src/Service/Indexer/InternalResourceIndexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ private function indexTranslationSplittedResources(
/**
* The resources for a language are indexed here.
*
* @param ResourceLocation[] $locations
* @param string[] $locations
*/
private function indexResourcesPerLanguageIndex(
string $processId,
Expand Down Expand Up @@ -335,7 +335,7 @@ private function indexResourcesPerLanguageIndex(
* methods accept a chunk with all paths that are to be indexed via a
* request.
*
* @param ResourceLocation[] $locations
* @param string[] $locations
*/
private function indexChunks(
string $processId,
Expand All @@ -346,6 +346,7 @@ private function indexChunks(
int $length
): int|false {
$resourceList = $this->loadResources(
$lang,
$locations,
$offset,
$length
Expand Down Expand Up @@ -373,10 +374,11 @@ private function indexChunks(
}

/**
* @param ResourceLocation[] $locations
* @param string[] $locations
* @return Resource[]|false
*/
private function loadResources(
ResourceLanguage $lang,
array $locations,
int $offset,
int $length
Expand All @@ -391,7 +393,10 @@ private function loadResources(

$resourceList = [];
for ($i = $offset; $i < $end; $i++) {
$location = $locations[$i];
$location = ResourceLocation::of(
$locations[$i],
$lang
);
try {
$resource = $this->resourceLoader->load($location);
$resourceList[] = $resource;
Expand Down
5 changes: 3 additions & 2 deletions src/Service/Indexer/SiteKit/SubDirTranslationSplitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ public function split(array $pathList): TranslationSplitterResult
continue;
}
if ($location->lang === ResourceLanguage::default()) {
$bases[] = $location;
$bases[] = $location->location;
continue;
}
if (!isset($translations[$location->lang->code])) {
$translations[$location->lang->code] = [];
}
$translations[$location->lang->code][] = $location;
$translations[$location->lang->code][] = $location->location;
}
gc_collect_cycles();

return new TranslationSplitterResult($bases, $translations);
}
Expand Down
9 changes: 4 additions & 5 deletions src/Service/Indexer/TranslationSplitterResult.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
namespace Atoolo\Search\Service\Indexer;

use Atoolo\Resource\ResourceLanguage;
use Atoolo\Resource\ResourceLocation;

class TranslationSplitterResult
{
/**
* @param ResourceLocation[] $bases
* @param array<string,array<ResourceLocation>> $translations
* @param string[] $bases
* @param array<string,array<string>> $translations
*/
public function __construct(
private readonly array $bases,
Expand All @@ -34,15 +33,15 @@ public function getLanguages(): array
}

/**
* @return ResourceLocation[]
* @return string[]
*/
public function getBases(): array
{
return $this->bases;
}

/**
* @return ResourceLocation[]
* @return string[]
*/
public function getTranslations(ResourceLanguage $lang): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Atoolo\Search\Test\Service\Indexer\SiteKit;

use Atoolo\Resource\ResourceLanguage;
use Atoolo\Resource\ResourceLocation;
use Atoolo\Search\Service\Indexer\SiteKit\SubDirTranslationSplitter;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -58,8 +57,8 @@ public function testGetTranlsations(): void
$translations = $result->getTranslations($lang);

$expected = [
ResourceLocation::of('/a/b.php', $lang),
ResourceLocation::of('/c/d.php', $lang),
'/a/b.php',
'/c/d.php',
];

$this->assertEquals(
Expand All @@ -80,7 +79,7 @@ public function testSplitWithLocParameter(): void
$translations = $result->getTranslations($lang);

$expected = [
ResourceLocation::of('/a/b.php', $lang)
'/a/b.php',
];

$this->assertEquals(
Expand Down

0 comments on commit 6d64470

Please sign in to comment.