Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid draining the ArrayPool with undisposed JsonDocuments (#2258)
My understanding is this code is likely going to be overhauled to not use JsonDocument at all, but in the meantime we can make a small tweak to significantly improve throughput. JsonDocument.Parse creates a JsonDocument backed by one or more ArrayPool arrays; those arrays are returned when the instance is disposed. If it's never disposed, the arrays are never returned to the pool. That means that creating but not disposing lots of JsonDocument instances ends up incurring the cost (and contention) of searching the ArrayPool but ends up falling back to allocating anyway. The problem is that JsonClaimSet isn't disposable and so the underlying JsonDocument is never disposed. As a patch until the code is overhauled, we can use JsonElement.Clone(), which creates a new JsonDocument not backed by ArrayPool arrays... we can then dispose of the original in order to return the arrays to the pool. This still isn't ideal, as we're doing more allocating and copying than we'd like, but it ends up being much better than without. Co-authored-by: Stephen Toub <stoub@microsoft.com>
- Loading branch information