Skip to content

Optimize repeated annotation stream processing in SpringCacheAnnotationParser #35598

@kamilkrzywanski

Description

@kamilkrzywanski

Summary:
In the current implementation of cache annotation parsing in SpringCacheAnnotationParser , the code processes annotation collections multiple times using separate streams — once for each annotation type (@Cacheable, @CacheEvict, @CachePut, @Caching).
This results in multiple iterations over the same collection and creates several short-lived stream pipelines.

Current code example:

annotations.stream().filter(Cacheable.class::isInstance).map(Cacheable.class::cast).forEach(
		cacheable -> ops.add(parseCacheableAnnotation(ae, cachingConfig, cacheable)));
annotations.stream().filter(CacheEvict.class::isInstance).map(CacheEvict.class::cast).forEach(
		cacheEvict -> ops.add(parseEvictAnnotation(ae, cachingConfig, cacheEvict)));
annotations.stream().filter(CachePut.class::isInstance).map(CachePut.class::cast).forEach(
		cachePut -> ops.add(parsePutAnnotation(ae, cachingConfig, cachePut)));
annotations.stream().filter(Caching.class::isInstance).map(Caching.class::cast).forEach(
		caching -> parseCachingAnnotation(ae, cachingConfig, caching, ops));

This effectively loops over the same annotations collection four times, creating four independent stream pipelines.

Suggested improvement
Iterate over the annotations only once, using either a classic loop or a single stream with conditional branching.

This approach:

  • Reduces the number of iterations from 4 to 1
  • Avoids multiple stream creations and lambda allocations
  • Keeps functional behavior identical

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: coreIssues in core modules (aop, beans, core, context, expression)status: supersededAn issue that has been superseded by another

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions