Skip to content

Restore delegate caching in IdResolver - #8973

Merged
flobernd merged 1 commit into
elastic:mainfrom
fresh55:fix/idresolver-delegate-caching
Aug 14, 2026
Merged

Restore delegate caching in IdResolver#8973
flobernd merged 1 commit into
elastic:mainfrom
fresh55:fix/idresolver-delegate-caching

Conversation

@fresh55

@fresh55 fresh55 commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

IdResolver only caches the getter delegate when resolution fails. On success it
returns without caching, so the delegate is rebuilt on every call.

Dropped in 4117107 during the aot rework. RoutingResolver from the same
commit still caches both paths.

100k resolves of the same type: 64 ms / 55.2 MB before, 3.5 ms / 40 bytes after.

Partially addresses #7664. That issue also mentions closure allocations in
RoutingResolver which I left alone here, happy to do that in a separate pr.

`IdResolver.Resolve` builds a compiled getter delegate via
`DynamicPropertyAccessor` and caches it so subsequent resolves of the same
type avoid reflection. Commit 4117107 ("Add (experimental) native AOT
support") kept the cache write on the failure path but dropped it on the
success path, so the delegate was rebuilt for every call.

In practice this means a full round of reflection (`GetProperty`,
`MakeGenericMethod`, `MethodInfo.Invoke`, `CreateDelegate`) plus two closure
allocations for every document whose id is inferred, which happens twice per
document during bulk indexing (once in `BulkIndexOperation.SetValues` and
again in `IdConverter.Write`).

Measured over 100,000 resolves of the same type: ~64 ms and 55.2 MB
allocated before, ~3.5 ms and 40 bytes after.

Note the sibling `RoutingResolver`, rewritten in the same commit, caches on
both the success and failure paths.

Partially addresses elastic#7664.
@flobernd

Copy link
Copy Markdown
Member

Hi @fresh55 , thanks for the PR!

@flobernd
flobernd merged commit b7100f1 into elastic:main Aug 14, 2026
27 of 29 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

💚 All backports created successfully

Status Branch Result
8.19
9.5

Questions ?

Please refer to the Backport tool documentation and see the Github Action logs for details

@fresh55

fresh55 commented Aug 14, 2026

Copy link
Copy Markdown
Contributor Author

thanks for merging

is the routingresolver half of #7664 still worth a pr or has that moved on. happy to open one if it is useful

flobernd pushed a commit that referenced this pull request Aug 14, 2026
Co-authored-by: Bruno Gale <32834882+fresh55@users.noreply.github.com>
flobernd pushed a commit that referenced this pull request Aug 14, 2026
Co-authored-by: Bruno Gale <32834882+fresh55@users.noreply.github.com>
This was referenced Aug 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants