-
Notifications
You must be signed in to change notification settings - Fork 26
perf: improve mapping lookup performance #144
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
Draft
Malte Janz (MalteJanz)
wants to merge
10
commits into
trunk
Choose a base branch
from
perf/improve-mapping-lookup-performance
base: trunk
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6c078bf
perf: improve mapping lookup performance
MalteJanz e9728c7
feat: wip MappingServiceV2 implementation
MalteJanz b4645c6
Merge branch 'trunk' into perf/improve-mapping-lookup-performance
MalteJanz 3089f3d
feat: wip intermediate commit of non working state
MalteJanz 873f4d4
feat: further progress
MalteJanz e69653d
fix: few bugs to get to working migration again
MalteJanz 6764217
feat: further progress on productConverter refactor
MalteJanz e67e360
feat: ProductConverter and migration working again
MalteJanz 59df48e
feat: apply workaround to resolve mappings on whole batch and do
MalteJanz 0afb86c
chore: enable profiling code
MalteJanz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| <?php declare(strict_types=1); | ||
|
|
||
| // Todo: do not merge this experiment | ||
|
|
||
| /** | ||
| * A task that holds a reference to a string and the source id of the mapping | ||
| * | ||
| * It makes it possible to update the referenced string at a later point | ||
| */ | ||
| class MappingTask | ||
| { | ||
| public function __construct( | ||
| public string &$reference, | ||
| public string $sourceId | ||
| ) { | ||
| } | ||
| } | ||
|
|
||
| class MappingService | ||
| { | ||
| /** | ||
| * @var list<MappingTask> | ||
| */ | ||
| private array $pendingTasks = []; | ||
|
|
||
| /** | ||
| * Important: this method returns a reference to a string which is also internally stored. | ||
| * It must be called like this: | ||
| * | ||
| * $data['someId'] = &$mappingService->getMapping('sourceId'); | ||
| * | ||
| * The use of '&' is very important, otherwise it will only copy the placeholder string. | ||
| * More info on this can be found in the PHP docs: | ||
| * https://www.php.net/manual/en/language.references.return.php | ||
| */ | ||
| public function &getMapping(string $sourceId): string | ||
| { | ||
| // previously we would fetch the mapping from the DB here, | ||
| // but reaching out to the DB every time is expensive | ||
|
|
||
| // so instead we just return a placeholder string | ||
| // and update it later, when we know all mappings that are needed | ||
|
|
||
| // and then use the string reference to update the placeholders | ||
|
|
||
| $pointer = 'placeholder-mapping-uuid'; | ||
|
|
||
| $task = new MappingTask($pointer, $sourceId); | ||
| $this->pendingTasks[] = $task; | ||
|
|
||
| return $pointer; | ||
| } | ||
|
|
||
| public function resolvePendingMappings(): void | ||
| { | ||
| // in here we can fetch all requested mappings from the DB | ||
| // in a single query and update all corresponding strings | ||
| foreach ($this->pendingTasks as $task) { | ||
| $task->reference = 'changed-' . $task->sourceId; | ||
| unset($task->reference); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // a stripped-down example of a converter | ||
| function convertData(MappingService $mappingService): array | ||
| { | ||
| $data['name'] = 'John Doe'; | ||
| $data['manufacturerId'] = &$mappingService->getMapping('manufacturer01'); | ||
| $data['price'] = []; | ||
| for ($i = 0; $i < 3; ++$i) { | ||
| $data['price'][] = convertCurrency($i, $mappingService); | ||
| } | ||
|
|
||
| return $data; | ||
| } | ||
|
|
||
| // a stripped-down example of internal logic in a converter | ||
| function convertCurrency(int $i, MappingService $mappingService): array | ||
| { | ||
| $price = [ | ||
| 'id' => $i, | ||
| // unfortunately, this does not work, but there is an easy workaround below | ||
| // 'currencyId' => &$mappingService->getMapping('currency0' . $i), | ||
| ]; | ||
|
|
||
| // this works fine | ||
| $price['currencyId'] = &$mappingService->getMapping('currency0' . $i); | ||
|
|
||
| return $price; | ||
| } | ||
|
|
||
| // === Main logic below === | ||
|
|
||
| $mappingService = new MappingService(); | ||
| $convertedData = convertData($mappingService); | ||
| echo '============== Initial data after converter run ==============' . \PHP_EOL; | ||
| var_dump($convertedData); | ||
|
|
||
| echo '============== data after mapping tasks got resolved ==============' . \PHP_EOL; | ||
| $mappingService->resolvePendingMappings(); | ||
| var_dump($convertedData); | ||
|
|
||
| // === Output looks like this === | ||
| /* | ||
| ============== Initial data after converter run ============== | ||
| array(3) { | ||
| ["name"]=> | ||
| string(8) "John Doe" | ||
| ["manufacturerId"]=> | ||
| &string(24) "placeholder-mapping-uuid" | ||
| ["price"]=> | ||
| array(3) { | ||
| [0]=> | ||
| array(2) { | ||
| ["id"]=> | ||
| int(0) | ||
| ["currencyId"]=> | ||
| &string(24) "placeholder-mapping-uuid" | ||
| } | ||
| [1]=> | ||
| array(2) { | ||
| ["id"]=> | ||
| int(1) | ||
| ["currencyId"]=> | ||
| &string(24) "placeholder-mapping-uuid" | ||
| } | ||
| [2]=> | ||
| array(2) { | ||
| ["id"]=> | ||
| int(2) | ||
| ["currencyId"]=> | ||
| &string(24) "placeholder-mapping-uuid" | ||
| } | ||
| } | ||
| } | ||
| ============== data after mapping tasks got resolved ============== | ||
| array(3) { | ||
| ["name"]=> | ||
| string(8) "John Doe" | ||
| ["manufacturerId"]=> | ||
| string(22) "changed-manufacturer01" | ||
| ["price"]=> | ||
| array(3) { | ||
| [0]=> | ||
| array(2) { | ||
| ["id"]=> | ||
| int(0) | ||
| ["currencyId"]=> | ||
| string(18) "changed-currency00" | ||
| } | ||
| [1]=> | ||
| array(2) { | ||
| ["id"]=> | ||
| int(1) | ||
| ["currencyId"]=> | ||
| string(18) "changed-currency01" | ||
| } | ||
| [2]=> | ||
| array(2) { | ||
| ["id"]=> | ||
| int(2) | ||
| ["currencyId"]=> | ||
| string(18) "changed-currency02" | ||
| } | ||
| } | ||
| } | ||
| */ | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Todo: remove this and document the approach properly somewhere else
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't come very far with implementing this as an actual
MappingServiceV2and using that in ourProductConverterto validate this idea further today.But I'm still curious what others would think about this rather unusual approach 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Creative! My main concern would be DX (using this right), but this is probably solvable with custom PHPStan rules or extra validation steps. Definitely worth exploring more 🙂