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

bson: remove use of reflect.Value.MethodByName

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 1.12.1
    • Affects Version/s: None
    • Component/s: BSON
    • None
    • Not Needed
    • 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?

      From PR 1308:

      Summary

      Remove all uses of reflect.Value.MethodByName since it prevents dead code elimination. This causes binaries built with the mongodb/mongo-go-driver library to be larger than they need to be.

      All occurrences of reflect.Value.MethodByName are replaced with type assertions.

      Source: https://go.dev/src/cmd/link/internal/ld/deadcode.go#L293

      Background & Motivation

      Reduce the size of binaries built with mongodb/mongo-go-driver.

      From the linked deadcode.go code:

      // The third case is handled by looking to see if any of:
      //   - reflect.Value.Method or MethodByName is reachable
      //   - reflect.Type.Method or MethodByName is called (through the
      //     REFLECTMETHOD attribute marked by the compiler).
      //
      // If any of these happen, all bets are off and all exported methods
      // of reachable types are marked reachable.
      

      TLDR: If your code calls reflect.Type.Method or reflect.Type.MethodByName, the linker can't prune any unreachable code and your compiled binary may be inflated. Never use those functions if you can avoid it.

            Assignee:
            qingyang.hu@mongodb.com Qingyang Hu
            Reporter:
            matt.dale@mongodb.com Matt Dale
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: