Skip to content

Reduce allocations and duplication in attribute resolution - #816

Open
Nafis8709 wants to merge 1 commit into
PebbleTemplates:masterfrom
Nafis8709:refactor/optimize-attribute-resolution
Open

Reduce allocations and duplication in attribute resolution#816
Nafis8709 wants to merge 1 commit into
PebbleTemplates:masterfrom
Nafis8709:refactor/optimize-attribute-resolution

Conversation

@Nafis8709

Copy link
Copy Markdown

Problem

DefaultAttributeResolver and MemberCacheUtils sit on the attribute-resolution path that
runs on every {{ object.property }} access during rendering, so small inefficiencies here
are multiplied across every attribute in every template. Four issues live in this path:

  • getArgumentTypes() returns new Class<?>[0] on every no-argument access — the common
    case ({{ user.name }}) — allocating a throwaway empty array each time.
  • MemberCacheKey is a non-static inner class that never uses its enclosing instance, so
    every key stored in the cache silently holds a reference to the outer MemberCacheUtils.
  • reflect() contains three near-identical blocks trying the get / is / has prefixes.
  • findMethod() mixes the "perfect match" and "greedy match" strategies in one long method.

Change

  • Hot-path allocation: getArgumentTypes() returns a shared EMPTY_ARGUMENT_TYPES
    constant for the no-argument case instead of allocating each time.
  • Hidden reference: MemberCacheKey is now static, removing the implicit outer
    reference from every cached key.
  • Duplication: the three get/is/has lookups collapse into a single loop over a
    prefix array, tried in the same order as before.
  • Long method: findMethod() is split into findPerfectMatch() and findGreedyMatch().
    verifyUnsafeMethod() is still called at the same point, so matching and access validation
    are unchanged.

Non-breaking: the public API of DefaultAttributeResolver is unchanged, and method
matching order and results are identical. These are pure refactorings.

Tests

  • Full test suite green: mvn clean test -pl pebble -am — tests, 0 failures, 0 errors.
  • No test changes required, since behaviour is unchanged.

@ebussieres
ebussieres force-pushed the refactor/optimize-attribute-resolution branch from b410f22 to 24789b6 Compare July 22, 2026 16:46
@ebussieres

Copy link
Copy Markdown
Member

AI stuff I presume ?

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