Faster (unsafe) BinaryReader in .NET

后端 未结 4 1671
余生分开走
余生分开走 2021-01-31 18:06

I came across a situation where I have a pretty big file that I need to read binary data from.

Consequently, I realized that the default BinaryReader implementation in .

4条回答
  •  梦谈多话
    2021-01-31 18:41

    Interesting, reading the whole file into a buffer and going through it in memory made a huge difference. This is at the cost of memory, but we have plenty.

    This makes me think that the FileStream's (or BufferedStream's for that matter) buffer implementation is flawed, because no matter what size buffer I tried, performance still sucked.

      using (var br = new FileStream(cacheFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, 0x10000, FileOptions.SequentialScan))
      {
          byte[] buffer = new byte[br.Length];
          br.Read(buffer, 0, buffer.Length);
          using (var memoryStream = new MemoryStream(buffer))
          {
              while (memoryStream.Position < memoryStream.Length)
              {
                  var doc = DocumentData.Deserialize(memoryStream);
                  docData[doc.InternalId] = doc;
              }
          }
      }
    

    Down to 2-5 seconds (depends on disk cache I'm guessing) now from 22. Which is good enough for now.

提交回复
热议问题