Repository navigation
Conversation
FieldMapping created a mapper function given as a class name (the default StandardMapper of every field) for each value: on reading, for every field of every row. It is now created once, at its first use, and reused; each with*Function() clears it. A function given as an instance was already reused. MapFromDbToInstanceHandler resolved the property name and the field mapping of each field for every row (getPropertyName(), getFieldMap(), fixFieldName()); it keeps them for the query (the handler is created for each query). Entity reads (getByQuery, the wallets reads) about 12-17% faster.
It implements byjg/anydataset-db EntityHydratorInterface: for each column, the property name, the field mapping (selectFunction) and how the entity takes it (byjg/serializer ObjectCopy::writerFor()) are resolved once per query, and each row is one loop, with the same result as ObjectCopy::copy() with the handler (key order, backed enums, dynamic or magic properties, stdClass). MapFromDbToInstanceHandlerTest compares hydrate() with ObjectCopy::copy() for setters, protected properties, a mapped column, a column without property, a selectFunction, backed enums and stdClass. getByQuery about 1.6x faster (20k entities); the wallets reads 1.4-1.7x. Requires the byjg/anydataset-db and byjg/serializer versions with EntityHydratorInterface and ObjectCopy::writerFor().
Mapper::entityHydrator() returns a function that builds an entity from the values of a row, with the mapping resolved once: the defaults of a new entity (first level) are read once and a MapFromDbToInstanceHandler fills each entity. getEntity() is one call of it, and getByQuery() with several mappers (a join) uses one hydrator per mapper for the query, instead of reading the defaults and creating a handler for each row and mapper. testGetEntityBuildsAsBefore compares getEntity() and entityHydrator() with the code before this change (setters, protected properties, mapped column, column without property, selectFunction, backed enums, stdClass).
mapper-functions.md: keep a mapper function stateless (created once per field mapping and reused), and what $instance is (the entity on update/insert, the row on select; it said the entity always, also in controlling-the-data.md). using-mapper-object.md: getEntity() and entityHydrator(). CHANGELOG-7.0: the mapper function reuse, performance, the joins.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
MapFromDbToInstanceHandlerhydrates the entity itself (byjg/anydataset-dbEntityHydratorInterface): the property, the field mapping (selectFunction) and how the entity takes each column (byjg/serializerObjectCopy::writerFor()) are resolved once per query; each row is one loop, with the same result asObjectCopy::copy()with the handler. Every repository read uses it, nothing to change in the entities.Mapper::entityHydrator()(new): builds entities from rows with the mapping resolved once.getEntity()uses it, andgetByQuery()with several mappers (joins) creates one per mapper per query instead of per row.FieldMapping(StandardMapperwas created for every field of every row) and the field map is resolved once per query.mapper-functions.md(keep a mapper function stateless;$instanceis the row on select: it said the entity, also fixed incontrolling-the-data.md),using-mapper-object.md(getEntity()/entityHydrator(), example run), CHANGELOG-7.0.Performance (PHP 8.5, Xdebug off), with byjg/php-serializer#19 and byjg/php-anydataset-db#42, vs. the start of 7.0:
getByQuery()20,000 entities MySQL ~152 ms -> ~29 ms, SQLite ~147 ms -> ~32 ms; byjg/walletsgetAllByWalletId3.9x,verifyChain3.0x.Breaking changes
FieldMapping, reused for every row and query (only a custom class that keeps state between calls is affected).Test plan
MapFromDbToInstanceHandlerTest(hydrate() ==ObjectCopy::copy()with the handler, all properties, for setters, protected properties, mapped column, column without property, selectFunction, backed enums, stdClass;getEntity()/entityHydrator()== the code before this change),testFunctionClassNameIsCreatedOnceAndReused(fails on the code before),testFunctionReplacedIsUsed.hydrate()makes 2 of these tests fail.vendor/bin/phpunitwithdocker compose up -d: 418 tests OK.psalm: no errors (with the serializer and anydataset-db branches).Merge after byjg/php-serializer#19 and byjg/php-anydataset-db#42: this branch needs
ObjectCopy::writerFor()andEntityHydratorInterface.