{"record":{"id":"3ef199e176999bc7","repo":"microsoft/FASTER","slug":"cannot-use-blittableparameterserializer-with-non-blittable-3ef199","errorCode":null,"errorMessage":"Cannot use BlittableParameterSerializer with non-blittable types - specify serializer explicitly","messagePattern":"Cannot use BlittableParameterSerializer with non-blittable types - specify serializer explicitly","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"cs/remote/src/FASTER.server/FixedLenSerializer.cs","lineNumber":27,"sourceCode":"namespace FASTER.server\n{\n    /// <summary>\n    /// Server-side serializer for blittable types\n    /// </summary>\n    /// <typeparam name=\"Key\">Key</typeparam>\n    /// <typeparam name=\"Value\">Value</typeparam>\n    /// <typeparam name=\"Input\">Input</typeparam>\n    /// <typeparam name=\"Output\">Output</typeparam>\n    public unsafe struct FixedLenSerializer<Key, Value, Input, Output> : IServerSerializer<Key, Value, Input, Output>\n        where Key : unmanaged\n        where Value : unmanaged\n        where Input : unmanaged\n        where Output : unmanaged\n    {\n        static FixedLenSerializer()\n        {\n            if (!IsBlittable<Key>() || !IsBlittable<Value>() || !IsBlittable<Input>() || !IsBlittable<Output>())\n                throw new Exception(\"Cannot use BlittableParameterSerializer with non-blittable types - specify serializer explicitly\");\n        }\n\n        /// <inheritdoc />\n        [MethodImpl(MethodImplOptions.AggressiveInlining)]\n        public ref Key ReadKeyByRef(ref byte* src)\n        {\n            var _src = (void*)src;\n            src += Unsafe.SizeOf<Key>();\n            return ref Unsafe.AsRef<Key>(_src);\n        }\n\n        /// <inheritdoc />\n        [MethodImpl(MethodImplOptions.AggressiveInlining)]\n        public ref Value ReadValueByRef(ref byte* src)\n        {\n            var _src = (void*)src;\n            src += Unsafe.SizeOf<Value>();\n            return ref Unsafe.AsRef<Value>(_src);","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/microsoft/FASTER/blob/321d872eabda6a0345c8bd76419f89723ed864ae/cs/remote/src/FASTER.server/FixedLenSerializer.cs#L9-L45","documentation":"FixedLenSerializer's static constructor validates at type-initialization time that Key, Value, Input and Output are all blittable (contain no references and have identical managed/native layout). FASTER's FixedLen/BlittableParameterSerializer path memcpy's raw bytes and can only work with unmanaged, blittable types; using it with e.g. string or a struct containing references would corrupt data, so it throws during static init (as TypeInitializationException).","triggerScenarios":"Declaring FixedLenSerializer<...> (or FASTER's blittable parameter serializer) with any generic type argument that is non-blittable — e.g. string, object, a struct with a string/object field — which fires the static constructor on first use.","commonSituations":"Using string keys/values with the fixed-length serializer for convenience; defining a record struct containing a reference-type field and assuming it is blittable; upgrading FASTER versions where serializer selection became stricter.","solutions":["Use blittable types (long, int, fixed-size structs with only primitives) for Key/Value/Input/Output.","For non-blittable types, specify an explicit ISerializer implementation (e.g. a var-length or custom serializer) instead of FixedLenSerializer.","If using a struct with reference fields, switch to byte arrays/fixed buffers or a varlen serializer path (e.g. VarLenSerializer or MemorySerializer)."],"exampleFix":"// before\nusing var session = store.For(...).NewSession<FixedLenSerializer<long, string, long, string>>();\n// after\nusing var session = store.For(...).NewSession<VarLenSerializer<long, string, long, string>>(); // or use fixed-size struct for Value","handlingStrategy":"type-guard","validationCode":"static bool IsBlittable<T>() => !(typeof(T).IsArray || typeof(T) == typeof(string) || typeof(T).IsClass) && (typeof(T).IsValueType && !typeof(T).IsGenericType && typeof(T).GetFields().All(f => f.FieldType == typeof(T) || f.FieldType.IsPrimitive || IsBlittable(f.FieldType)));","typeGuard":"static bool CanUseFixedLen<K,V,I,O>() => IsBlittable<K>() && IsBlittable<V>() && IsBlittable<I>() && IsBlittable<O>();","tryCatchPattern":"try { Activator.CreateInstance(typeof(FixedLenSerializer<K,V,I,O>)); } catch (TypeInitializationException ex) { /* select explicit serializer instead */ }","preventionTips":["Restrict FixedLenSerializer to primitives and structs of primitives only.","For strings or reference-bearing structs, always choose VarLenSerializer or a custom ISerializer up front."],"tags":["csharp","faster","serializer","blittable","generics"],"backgroundTag":"type-mismatch","analyzedSha":"321d872eabda6a0345c8bd76419f89723ed864ae","analyzedAt":"2026-09-15T22:18:00.693Z","contentChangedAt":"2026-09-15T22:18:00.693Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}