Skip to content

feat(WebService): cache per-class annotation configuration - #155

Merged
usernane merged 1 commit into
mainfrom
dev
Sep 20, 2026
Merged

usernane merged 1 commit into
mainfrom
dev

Conversation

@usernane

Copy link
Copy Markdown
Member

Summary

Caches per-class annotation configuration in WebService so the reflection that derives routing, parameters, and authorization runs once per class per process instead of on every construction and per-method call.

Motivation

WebService re-reflected its attributes (#[RestController], #[GetMapping], #[RequestParam], #[RequiresAuth], #[PreAuthorize], #[UseParameterSet], …) on every construction and on per-method processing. Since a class's annotations are immutable for the process lifetime, this recomputes identical data on a universal hot path — every request constructs at least one service, and a WebServicesManager eagerly constructs all registered services. Closes #154. See ADR-0050.

Changes (three private static caches, keyed by static::class)

  • $annotationCache — constructor-path config (path, description, requestMethods, authRequired, annotation presence/name). Recorded only after the reflection path — including the DuplicateMappingException check — succeeds, so a throwing class never caches a bad state.
  • $methodParamCache — per-(class,method) RequestParam/UseParameterSet descriptors. Parameter mutations are replayed per instance with fresh objects, so no mutable state is shared (keeps OpenAPI output correct).
  • $methodAuthCache — per-(class,method) static auth facts only (attribute presence + PreAuthorize expression string). SecurityContext is always evaluated live, so authorization decisions are never cached.

Correctness safeguards

  • Name is never cached for unannotated classes — new WebService('users') vs new WebService('orders') stay independent; only an annotation name is cached, and the final name is resolved per instance.
  • No public API change; routing, parameters, authorization, OpenAPI docs, duplicate-mapping detection, and unannotated fallback-name behavior are all preserved.

How to Test / Verify

  • composer test → 644 tests pass (was 640; +4 new AnnotationCacheTest): cache-miss vs cache-hit produce identical derived state; per-method parameter services yield independent objects across instances; unannotated names stay independent; OpenAPI output is byte-identical across instances.
  • composer fix-cs clean.
  • Benchmark (clean back-to-back A/B, php -n -d opcache.enable_cli=1, Xdebug off, 50k iters, param-heavy service). Ratios are the portable takeaway (absolute µs are machine-specific):
Path Before After Speedup
Single WebService construction ~134 µs/op ~11 µs/op ~12×
Manager + 6 services ~1213 µs/op ~114 µs/op ~10×
configureParametersForMethod (fresh instance) ~217 µs/op ~33 µs/op ~6.5×
checkMethodAuthorization (fresh instance) ~197 µs/op ~40 µs/op ~4.9×

Breaking Changes and Migration Steps

None. Transparent internal optimization; no API or behavior change.

Checklist

  • I reviewed my own diff before requesting review
  • My commits follow Conventional Commits
  • I added/updated tests (or explained why not)
  • I updated docs (if needed) Docs Repo — ADR-0050 added; README unchanged (transparent optimization, no documented behavior affected)
  • I ran lint/cs-fixer (if applicable) (composer fix-cs)
  • I considered backward compatibility
  • I considered security (authorization decisions are never cached; SecurityContext evaluated live)

Related issues

Closes #154

Avoids repeated reflection on construction and per-method processing by
caching the derived, class-static annotation configuration keyed by
static::class. A class's annotations are immutable per process, so the
reflection runs once per class (and per class-method) instead of once per
instance/call.

- $annotationCache: constructor-path config (path, description, requestMethods,
  authRequired, annotation presence/name). Recorded only after the reflection
  path (incl. DuplicateMappingException) succeeds.
- $methodParamCache: per-(class,method) RequestParam/UseParameterSet descriptors;
  parameter mutations are replayed per instance with fresh objects (OpenAPI-safe).
- $methodAuthCache: per-(class,method) static auth facts only; SecurityContext is
  always evaluated live, so authorization decisions are never cached.

The service name is intentionally NOT cached for unannotated classes, so
constructing the same class with different names stays correct. No public API
change; behavior (routing, params, auth, OpenAPI docs, fallback name) is
preserved. See ADR-0050.

Measured (clean A/B, opcache on, Xdebug off, 50k iters, param-heavy service):
construction ~12x, manager+6 services ~10x, per-method params ~6x, per-method
auth ~5x.

Closes #154
@sonarqubecloud

Copy link
Copy Markdown

@codecov

codecov Bot commented Sep 20, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.59036% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.48%. Comparing base (0f5f936) to head (8a27306).

Files with missing lines Patch % Lines
WebFiori/Http/WebService.php 97.59% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main     #155      +/-   ##
============================================
+ Coverage     93.37%   93.48%   +0.11%     
- Complexity     1396     1412      +16     
============================================
  Files            39       39              
  Lines          3410     3454      +44     
============================================
+ Hits           3184     3229      +45     
+ Misses          226      225       -1     
Flag Coverage Δ
php-8.1 93.48% <97.59%> (+0.11%) ⬆️
php-8.2 93.37% <97.59%> (+0.11%) ⬆️
php-8.3 93.37% <97.59%> (+0.11%) ⬆️
php-8.4 93.37% <97.59%> (+0.11%) ⬆️
php-8.5 93.37% <97.59%> (+0.11%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@sonarqubecloud

Copy link
Copy Markdown

@usernane
usernane merged commit 1e7d8f4 into main Sep 20, 2026
18 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.

feat(WebService): cache per-class annotation configuration to avoid repeated reflection on construction

1 participant