Skip to content

Faster entity reads: the handler hydrates the entity; mapper functions created once - #39

Merged
byjg merged 4 commits into
masterfrom
7.0
Nov 6, 2026
Merged

byjg merged 4 commits into
masterfrom
7.0

Conversation

@byjg

@byjg byjg commented Nov 5, 2026

Copy link
Copy Markdown
Owner

Summary

  • MapFromDbToInstanceHandler hydrates the entity itself (byjg/anydataset-db EntityHydratorInterface): the property, the field mapping (selectFunction) and how the entity takes each column (byjg/serializer ObjectCopy::writerFor()) are resolved once per query; each row is one loop, with the same result as ObjectCopy::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, and getByQuery() with several mappers (joins) creates one per mapper per query instead of per row.
  • Mapper functions given as a class name are created once per FieldMapping (StandardMapper was created for every field of every row) and the field map is resolved once per query.
  • Docs: mapper-functions.md (keep a mapper function stateless; $instance is the row on select: it said the entity, also fixed in controlling-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/wallets getAllByWalletId 3.9x, verifyChain 3.0x.

Breaking changes

Test plan

  • New: 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.
  • A mutation check: removing the selectFunction from hydrate() makes 2 of these tests fail.
  • vendor/bin/phpunit with docker compose up -d: 418 tests OK. psalm: no errors (with the serializer and anydataset-db branches).
  • byjg/wallets with this branch: 138 tests OK.

Merge after byjg/php-serializer#19 and byjg/php-anydataset-db#42: this branch needs ObjectCopy::writerFor() and EntityHydratorInterface.

byjg added 4 commits November 5, 2026 16:21
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.
@byjg
byjg merged commit 69012df into master Nov 6, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant