Skip to content

perf(mapper): cache per-class reflection plans during hydration - #2276

Open
osbre wants to merge 5 commits into
tempestphp:3.xfrom
osbre:perf/mapper-hydration
Open

perf(mapper): cache per-class reflection plans during hydration#2276
osbre wants to merge 5 commits into
tempestphp:3.xfrom
osbre:perf/mapper-hydration

Conversation

@osbre

@osbre osbre commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

Hydrating rows into models now caches reflection metadata, primary key scans, and resolved mappers to avoid redundant per-row work.

Improvements

  • Per-Class Reflection Plans: Caches reflection metadata, property types, and parent-child relations by class rather than recalculating them per row.
  • Cached Primary Key: Memoizes the primary key property lookup on model inspectors to prevent thousands of redundant scans per nested eager-loading query.
  • Singleton Mapper Cache: Replaces a factory-level cache that never hit (a fresh factory is built per map() call) with a container-scoped #[Singleton], so the cache lives and dies with its container - no cross-container leaks. It implements Resettable, so workers calling resetContainer() between requests re-resolve mappers instead of holding ones built from unregistered dependencies. ObjectFactory::__construct() takes a third argument as a result.

Performance Impact

Benchmark Before After Change
Mapper stage
2,000-row SQLite select
92.6ms 46.7ms -49.6%
End-to-end Model::all() 114ms 68ms -40.3%
Eager-loading
50 users with nested relations
1,517ms 551ms -63.7%
map() in a hydration loop
Per call, steady state
0.372ms 0.016ms -95.7%

@github-actions

github-actions Bot commented Sep 5, 2026

Copy link
Copy Markdown

Benchmark Results

Comparison of perf/mapper-hydration against 3.x (b1ae60ea63210569329e04ea911b471d7d9fd9d0).

Open to see the benchmark results

No benchmark changes above ±5%.

Generated by phpbench against commit 981b7a0

@osbre
osbre force-pushed the perf/mapper-hydration branch from 1b87a8f to e223c43 Compare September 5, 2026 21:00
@innocenzi
innocenzi requested a review from xHeaven September 7, 2026 17:05
@xHeaven
xHeaven force-pushed the perf/mapper-hydration branch from 8e580bd to 981b7a0 Compare September 8, 2026 00:10

@xHeaven xHeaven left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added 2 failing tests, please fix those regressions.

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.

2 participants