Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache columeToIndexFieldMap #131

Merged
merged 1 commit into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions dbscan/dbscan.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"reflect"
"regexp"
"strings"
"sync"
)

// Rows is an abstract database rows that dbscan can iterate over and get the data from.
Expand Down Expand Up @@ -54,13 +55,16 @@ func SnakeCaseMapper(str string) string {

// API is the core type in dbscan. It implements all the logic and exposes functionality available in the package.
// With API type users can create a custom API instance and override default settings hence configure dbscan.
// API should not be copied after first use.
type API struct {
structTagKey string
columnSeparator string
fieldMapperFn NameMapperFunc
scannableTypesOption []interface{}
scannableTypesReflect []reflect.Type
allowUnknownColumns bool
// columnToIndexFieldMapCache stores a map of reflect.Type -> map[string][]int
columnToIndexFieldMapCache sync.Map
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I went back and forth on whether this should store a sync.Map itself or a pointer to a (heap-allocated) one. IMO both ways have risk of improper use. If we store the Map directly, the user could copy the API while the map is being updated. On the other hand, if we use a pointer, the value might not get initialized. (Or we would need to initialize it lazily, which adds some complexity and synchronization overhead.) I'm happy to change to another solution if you feel strongly.

}

// APIOption is a function type that changes API configuration.
Expand Down
12 changes: 12 additions & 0 deletions dbscan/structref.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ type toTraverse struct {
}

func (api *API) getColumnToFieldIndexMap(structType reflect.Type) map[string][]int {
resultIface, ok := api.columnToIndexFieldMapCache.Load(structType)
if ok {
return resultIface.(map[string][]int)
}

result := api.buildColumnToFieldIndexMap(structType)
resultIface, _ = api.columnToIndexFieldMapCache.LoadOrStore(structType, result)
result = resultIface.(map[string][]int)
return result
}

func (api *API) buildColumnToFieldIndexMap(structType reflect.Type) map[string][]int {
result := make(map[string][]int, structType.NumField())
var queue []*toTraverse
queue = append(queue, &toTraverse{Type: structType, IndexPrefix: nil, ColumnPrefix: ""})
Expand Down
Loading