FDB2002: Key serialized with .ToSlice() before a transaction call
| Analyzer | FoundationDB.Client |
| Severity | Warning (performance) |
| Code fix | Yes |
Message
The transaction encodes a typed key into its own buffer. Remove .ToSlice() and pass '{0}' directly.
Why
subspace.Key(...) returns a typed key struct that the transaction methods encode into a pooled or stack buffer. Calling .ToSlice() first allocates a byte array for the key, which the transaction copies again: one allocation per key, for nothing. Every transaction method has a typed overload, so the .ToSlice() call is never needed to make the code compile.
The rule reports a .ToSlice() call on a typed key (the FdbKeyExtensions.ToSlice() extension, or a key struct's own ToSlice()) when the result is the key argument of GetAsync, Set, Clear, ClearRange, GetRange, Atomic*, Watch, AddConflictRange, or SetVersionStampedKey, directly or through a local whose every use is such an argument. A key serialized for logging, for storing as a value, or for another API keeps its .ToSlice().
Bad
tr.Set(subspace.Key("a", 1).ToSlice(), value);
Good
tr.Set(subspace.Key("a", 1), value);
Code fix
The fix removes the .ToSlice() call from the argument, or from the initializer of a local declared with var.
Suppress
#pragma warning disable FDB2002 // reason
or, for a whole project, in .editorconfig:
dotnet_diagnostic.FDB2002.severity = none
The whole performance tier can be muted at once:
dotnet_analyzer_diagnostic.category-FdbPerformance.severity = none