You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
NCBC-3726: Apply misc perf optimizations to transcoders
Motivation
----------
Generally improve performance of transcoders.
Modifications
-------------
- Support encoding `ReadOnlyMemory<byte>` and `Memory<byte>` in
`RawJsonTranscoder`, matching the behavior of `RawBinaryTranscoder`.
- Support decoding `IMemoryOwner<byte>` in `RawJsonTranscoder`, matching
the behavior of `RawBinaryTranscoder`.
- Use `typeof(T) == ` instead `value is` checks for value types, which
are elidable by JIT when it compiles variants for each value type
used.
- For both `RawJsonTranscoder` and `RawStringTranscoder` use a
`StreamWriter` when encoding a large string rather than allocating a
large buffer on the heap.
- Move `Utf8NoBomEncoding` to a shared internal class.
- Use `ThrowHelper`s for throwing exceptions in all transcoders.
- Add a variety of missing unit tests and reorganize a bit.
Results
-------
- Significantly reduced heap allocations when encoding large strings
using RawJsonTranscoder or RawStringTranscoder, especially if >1MB.
However, we may want to consider support for encoding `Memory<char>`
in the future as well to allow the consumer to reduce allocations of
`string` objects on the heap.
- The consumer may use `Memory<byte>`, `ReadOnlyMemory<byte>`, and
`IMemoryOwner<byte>` to work with pooled buffers to reduce heap
allocations when using `RawJsonTranscoder`.
- Encoding with `Memory<byte>` and `ReadOnlyMemory<byte>` will have
shorter JITed methods that are slightly more performant.
- .NET 8 with PGO (Profile Guided Optimization) enabled (the default)
may be able to perform guarded devirtualization and inline some of
these calls since they now use `ThrowHelper` instead of throwing
exceptions directly.
Note: `LegacyTranscoder` could also be rewritten to use `typeof(T) ==`
instead of `TypeCode`, and would see even more significant gains, but it
seems to fragile to risk the change.
Change-Id: I4c9c632ff820b47efc31ab0a2ae3f192b8dce766
Reviewed-on: https://review.couchbase.org/c/couchbase-net-client/+/207372
Reviewed-by: Emilien Bevierre <[email protected]>
Tested-by: Build Bot <[email protected]>
0 commit comments