Skip to content

Instantly share code, notes, and snippets.

@berfarah
Created October 1, 2018 21:32
Show Gist options
  • Save berfarah/05dd038c27f718dd586396c4b2284c99 to your computer and use it in GitHub Desktop.
Save berfarah/05dd038c27f718dd586396c4b2284c99 to your computer and use it in GitHub Desktop.
Iterative Optimization on Hot Paths: Optimization 2: Cutting out the middleman
func parseQueryRow(table *Table, rows *sql.Rows) (interface{}, error) {
ptr := reflect.New(table.Type)
elem := ptr.Elem()
values := make([]interface{}, len(table.Columns))
- [... populate scanners slice with column.Descriptor.Scanner() ...]
+ for i, column := range table.Columns {
+ scanner := column.Descriptor.Scanner()
+ scanner.Target(field)
+ values[i] = scanner
+ }
if err := rows.Scan(values...); err != nil {
return nil, err
}
- for i, column := range table.Columns {
- scanners[i].CopyTo(elem.FieldByIndex(column.Index))
- }
return ptr.Interface(), nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment