perf(keycloak): cache JsonSerializerOptions instead of per-call allocation (CA1869) #681

Closed
opened 2026-07-17 20:04:18 +00:00 by spikerj · 1 comment
Owner

KeycloakAdminService allocated a fresh JsonSerializerOptions at 4 serialize/deserialize sites (1x DefaultIgnoreCondition=WhenWritingNull, 3x PropertyNameCaseInsensitive=true). System.Text.Json caches type metadata per options-instance, so a new instance each call rebuilds that reflection cache (CA1869) — avoidable allocation/CPU on the Keycloak admin path (user create/update/query).

Hoisted to two static readonly instances shared across the sites. Identical config → no behavior change; immutable after first use, safe for concurrent reads. All 41 KeycloakAdminService tests pass; build clean.

(Continuation of #364 which fixed the same pattern in BookController. 66 CA1869 remain, mostly in test files where per-call allocation is irrelevant + a few other production services.) SonarQube rule external_roslyn:CA1869.

KeycloakAdminService allocated a fresh JsonSerializerOptions at 4 serialize/deserialize sites (1x DefaultIgnoreCondition=WhenWritingNull, 3x PropertyNameCaseInsensitive=true). System.Text.Json caches type metadata per options-instance, so a new instance each call rebuilds that reflection cache (CA1869) — avoidable allocation/CPU on the Keycloak admin path (user create/update/query). Hoisted to two static readonly instances shared across the sites. Identical config → no behavior change; immutable after first use, safe for concurrent reads. All 41 KeycloakAdminService tests pass; build clean. (Continuation of #364 which fixed the same pattern in BookController. 66 CA1869 remain, mostly in test files where per-call allocation is irrelevant + a few other production services.) SonarQube rule external_roslyn:CA1869.
Author
Owner

Resolved in spikersoft-backend PR #379 (merged to master). Hoisted KeycloakAdminService's 4 per-call JsonSerializerOptions to 2 static readonly instances; all 41 tests pass. Closing.

Resolved in spikersoft-backend PR #379 (merged to master). Hoisted KeycloakAdminService's 4 per-call JsonSerializerOptions to 2 static readonly instances; all 41 tests pass. Closing.
Sign in to join this conversation.