Skip to content

Perf: SessionContainer.ResolvePartitionLocalSessionTokenForGateway triggers repeated JSON deserialization on PartitionKeyRange.Parents per request #5751

@kirankumarkolli

Description

@kirankumarkolli

Parent issue: #5747

Problem

SessionContainer.ResolvePartitionLocalSessionTokenForGateway() (src/SessionContainer.cs:158-163) accesses PartitionKeyRange.Parents in a loop first checking if Parents != null, then iterating Parents.Count and accessing Parents[parentIndex]. Each access to .Parents triggers JToken.ToObject<Collection<string>>(), deserializing the entire collection from JSON on every access.

Hot path assessment: HOT

Called per-request when using Session consistency (the default consistency level). The method resolves session tokens for gateway-mode requests. With partition splits producing parent chains, this loop can execute multiple times per request.

Suggested fix

Cache request.RequestContext.ResolvedPartitionKeyRange.Parents in a local IReadOnlyList<string> variable before the null check and loop.

Lines

  • SessionContainer.cs:158 request.RequestContext.ResolvedPartitionKeyRange.Parents != null
  • SessionContainer.cs:161 request.RequestContext.ResolvedPartitionKeyRange.Parents.Count
  • SessionContainer.cs:163 request.RequestContext.ResolvedPartitionKeyRange.Parents[parentIndex]

Constraint

PartitionKeyRange is in compiled NuGet (Microsoft.Azure.Cosmos.Direct) fix must be at the call site.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions