Uploaded image for project: 'Go Driver'
  1. Go Driver
  2. GODRIVER-3533

Buffer valueWriters while encoding

    • Type: Icon: Bug Bug
    • Resolution: Unresolved
    • Priority: Icon: Major - P3 Major - P3
    • 2.2.1
    • Affects Version/s: None
    • Component/s: None
    • None
    • None
    • Go Drivers
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?
    • None
    • None
    • None
    • None
    • None
    • None

      Detailed steps to reproduce the problem?

      There is a notable difference between benchmarking encoding between v1 and master of the Go Driver.

      v1:

      goos: darwin
      goarch: arm64
      pkg: go.mongodb.org/mongo-driver/bson
      cpu: Apple M1 Pro
      BenchmarkMarshal/BSON/nested_struct-10           2706303               453.9 ns/op           416 B/op          1 allocs/op
      PASS
      ok      go.mongodb.org/mongo-driver/bson        1.970s
      

      master:

      goos: darwin
      goarch: arm64
      pkg: go.mongodb.org/mongo-driver/v2/bson
      cpu: Apple M1 Pro
      BenchmarkMarshal/BSON/nested_struct-10            639736              1948 ns/op            4830 B/op         13 allocs/op
      PASS
      ok      go.mongodb.org/mongo-driver/v2/bson     1.745s
      

      Looking at commits around the time of the BSON v2 implementation, the > 1 allocation problem began with 8c5c0b0. Performance was somewhat improved with 4c6d174 to “Re-add marshal optimizations”. But the results are still pretty bad.

      Notably, we don't carry over the valueWriter buffer for encoding in the implementation on master, instead we create a new document writer every time.

      Definition of done: what must be done to consider the task complete?

      Add a valueWriter sync.Pool back to the Marshal function in the bson package: https://github.com/mongodb/mongo-go-driver/blob/f7546adc4002ef89a78b8ad8def4aaffe000d5bd/bson/marshal.go#L193-L194

      The exact Go version used, with patch level:

      go version go1.23.1 darwin/arm64

      The exact version of the Go driver used:

      2.1.0

      Describe how MongoDB is set up. Local vs Hosted, version, topology, load balanced, etc.

      NA

      The operating system and version (e.g. Windows 7, OSX 10.8, ...)

      OSX 15.4

      Security Vulnerabilities

      NA

            Assignee:
            preston.vasquez@mongodb.com Preston Vasquez
            Reporter:
            preston.vasquez@mongodb.com Preston Vasquez
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: