优化func (session *Session) slice2Bean方法中的strings.ToLower以及减少map创建和访问次数(对应issue:https://gitea.com/xorm/xorm/issues/2243)

This commit is contained in:
洪坤安 2023-04-03 02:37:04 +08:00
parent b9ee143cc2
commit a8ddcde883
2 changed files with 18 additions and 12 deletions

View File

@ -710,20 +710,9 @@ func (session *Session) slice2Bean(scanResults []interface{}, allColum *AllColum
buildAfterProcessors(session, bean)
tempMap := make(map[string]int)
var pk schemas.PK
for i, field := range allColum.Fields {
var idx int
var ok bool
if idx, ok = tempMap[field.LowerFieldName]; !ok {
idx = 0
} else {
idx++
}
tempMap[field.LowerFieldName] = idx
col, fieldValue, err := getField(dataStruct, table, field.FieldName, idx)
col, fieldValue, err := getField(dataStruct, table, field.FieldName, field.TempIndex)
if _, ok := err.(ErrFieldIsNotExist); ok {
continue
} else if err != nil {

View File

@ -167,6 +167,7 @@ type QueryedField struct {
FieldName string
LowerFieldName string
ColumnType *sql.ColumnType
TempIndex int
}
type AllColumn struct {
@ -190,6 +191,22 @@ func ParseQueryRows(fieldNames []string, types []*sql.ColumnType) *AllColumn {
}
allColumn.Fields = fields
tempMap := make(map[string]int)
for _, field := range fields {
var idx int
var ok bool
if idx, ok = tempMap[field.LowerFieldName]; !ok {
idx = 0
} else {
idx++
}
tempMap[field.LowerFieldName] = idx
field.TempIndex = idx
}
return &allColumn
}