[GODRIVER-2887] bson: remove use of reflect.Value.MethodByName Created: 30/Jun/23  Updated: 28/Oct/23  Resolved: 06/Jul/23

Status: Closed
Project: Go Driver
Component/s: BSON
Affects Version/s: None
Fix Version/s: 1.12.1

Type: Improvement Priority: Unknown
Reporter: Matt Dale Assignee: Qingyang Hu
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Related
is related to GODRIVER-2890 Abstract en(de)coder assertion code i... Backlog
Documentation Changes: Not Needed
Documentation Changes Summary:

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?


 Description   

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.



 Comments   
Comment by Githook User [ 01/Aug/23 ]

Author:

{'name': 'Charlie Vieth', 'email': 'charlie.vieth@gmail.com', 'username': 'charlievieth'}

Message: GODRIVER-2887 Remove use of reflect.Value.MethodByName in bson (#1308)
Branch: release/1.12
https://github.com/mongodb/mongo-go-driver/commit/8857a04b3ad7c9060a2335e72561562c64a52db1

Comment by Githook User [ 06/Jul/23 ]

Author:

{'name': 'Charlie Vieth', 'email': 'charlie.vieth@gmail.com', 'username': 'charlievieth'}

Message: GODRIVER-2887 Remove use of reflect.Value.MethodByName in bson (#1308)
Branch: master
https://github.com/mongodb/mongo-go-driver/commit/65cd095b164d3bde0cb0a007d986eaa99c337035

Generated at Thu Feb 08 08:39:34 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.