From ecd265dad9d36c5acdf7075fba11bf10365b2250 Mon Sep 17 00:00:00 2001 From: Jon Hanna Date: Sat, 31 Dec 2016 14:35:55 +0000 Subject: [PATCH] Don't assign _largeByteBuffer until clear it will be used. BsonBinaryWriter._largeByteBuffer is lazily-allocated, but a path following the allocation doesn't use it. Move the allocation into the path that makes use of it, to avoid the null-check and potentially the allocation. --- Src/Newtonsoft.Json/Bson/BsonBinaryWriter.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Src/Newtonsoft.Json/Bson/BsonBinaryWriter.cs b/Src/Newtonsoft.Json/Bson/BsonBinaryWriter.cs index 5cd2bbf3..24e2173a 100644 --- a/Src/Newtonsoft.Json/Bson/BsonBinaryWriter.cs +++ b/Src/Newtonsoft.Json/Bson/BsonBinaryWriter.cs @@ -210,12 +210,13 @@ namespace Newtonsoft.Json.Bson { if (s != null) { - if (_largeByteBuffer == null) - { - _largeByteBuffer = new byte[256]; - } if (byteCount <= 256) { + if (_largeByteBuffer == null) + { + _largeByteBuffer = new byte[256]; + } + Encoding.GetBytes(s, 0, s.Length, _largeByteBuffer, 0); _writer.Write(_largeByteBuffer, 0, byteCount); }