Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-1117

Add a few methods to IBsonStream and make it a fully functional stream

    XMLWordPrintableJSON

Details

    • Icon: Improvement Improvement
    • Resolution: Done
    • Icon: Major - P3 Major - P3
    • 2.0
    • None
    • BSON
    • None

    Description

      We currently have an IBsonStream interface that has specialized method to read and write BSON primitive data types. If we added just a few methods to it (Read, ReadByte, Write, WriteBytes, etc...) it could act as a fully functional stream.

      Our I/O methods are built upon a stream, and if it so happens that the stream we are provided implements IBsonStream, then we use the more efficiently implemented methods from IBsonStream, otherwise we fall back on using the provided stream's Read and Write methods (which requires us to allocate some short lived byte arrays).

      If IBsonStream were a fully functional stream (where fully functional is defined as what we need), then internally we could work only with an IBsonStream.

      We would end up with probably two implementations of IBsonStream:

      1. BsonStream (wraps a standard Stream and implements IBsonStream on top of it)
      2. SegmentedBufferStream (analogous to MemoryStream, but implemented on top of an ISegmentedBuffer)

      The first would allow us to work with any Stream at all, with some additional overhead because of the need to create intermediate byte arrays when reading and writing some BSON primitives.

      The second would be efficiently implemented on top of an ISegmentedBuffer in a way that reduces allocation of temporary byte arrays, thus reducing GC pressure.

      The benefit internally is that we would only have to check in one place whether a Stream passed in to us implements IBsonStream or not. If it does, good, otherwise we wrap it in a BsonStream (which implemens IBsonStream). This eliminates multiple checks for whether the interface is implemented and will likely reduce the levels of indirection as well.

      This would also allow us to get rid of the current BsonStreamReader and BsonStreamWriter classes, since the functionality they currently provide is already provided by IBsonStream.

      Attachments

        Activity

          People

            robert@mongodb.com Robert Stam
            robert@mongodb.com Robert Stam
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: