enhance: pre allocate space to prevent slice grow (#38065)

Signed-off-by: lixinguo <xinguo.li@zilliz.com>
Co-authored-by: lixinguo <xinguo.li@zilliz.com>
pull/38004/head
smellthemoon 2024-11-29 14:26:43 +08:00 committed by GitHub
parent 9d3035c8ab
commit 48ed586167
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 12 additions and 12 deletions

View File

@ -127,7 +127,7 @@ func convertRange(field *schemapb.FieldSchema, result gjson.Result) (string, err
fieldType := field.DataType fieldType := field.DataType
if fieldType == schemapb.DataType_Int64 { if fieldType == schemapb.DataType_Int64 {
var dataArray []int64 dataArray := make([]int64, 0, len(result.Array()))
for _, data := range result.Array() { for _, data := range result.Array() {
if data.Type == gjson.String { if data.Type == gjson.String {
value, err := cast.ToInt64E(data.Str) value, err := cast.ToInt64E(data.Str)
@ -145,7 +145,7 @@ func convertRange(field *schemapb.FieldSchema, result gjson.Result) (string, err
} }
resultStr = joinArray(dataArray) resultStr = joinArray(dataArray)
} else if fieldType == schemapb.DataType_VarChar { } else if fieldType == schemapb.DataType_VarChar {
var dataArray []string dataArray := make([]string, 0, len(result.Array()))
for _, data := range result.Array() { for _, data := range result.Array() {
value, err := cast.ToStringE(data.Str) value, err := cast.ToStringE(data.Str)
if err != nil { if err != nil {
@ -175,7 +175,7 @@ func checkGetPrimaryKey(coll *schemapb.CollectionSchema, idResult gjson.Result)
// --------------------- collection details --------------------- // // --------------------- collection details --------------------- //
func printFields(fields []*schemapb.FieldSchema) []gin.H { func printFields(fields []*schemapb.FieldSchema) []gin.H {
var res []gin.H res := make([]gin.H, 0, len(fields))
for _, field := range fields { for _, field := range fields {
if field.Name == common.MetaFieldName { if field.Name == common.MetaFieldName {
continue continue
@ -187,7 +187,7 @@ func printFields(fields []*schemapb.FieldSchema) []gin.H {
} }
func printFieldsV2(fields []*schemapb.FieldSchema) []gin.H { func printFieldsV2(fields []*schemapb.FieldSchema) []gin.H {
var res []gin.H res := make([]gin.H, 0, len(fields))
for _, field := range fields { for _, field := range fields {
if field.Name == common.MetaFieldName { if field.Name == common.MetaFieldName {
continue continue
@ -242,7 +242,7 @@ func printFieldDetail(field *schemapb.FieldSchema, oldVersion bool) gin.H {
} }
func printFunctionDetails(functions []*schemapb.FunctionSchema) []gin.H { func printFunctionDetails(functions []*schemapb.FunctionSchema) []gin.H {
var res []gin.H res := make([]gin.H, 0, len(functions))
for _, function := range functions { for _, function := range functions {
res = append(res, gin.H{ res = append(res, gin.H{
HTTPReturnFunctionName: function.Name, HTTPReturnFunctionName: function.Name,
@ -269,7 +269,7 @@ func getMetricType(pairs []*commonpb.KeyValuePair) string {
} }
func printIndexes(indexes []*milvuspb.IndexDescription) []gin.H { func printIndexes(indexes []*milvuspb.IndexDescription) []gin.H {
var res []gin.H res := make([]gin.H, 0, len(indexes))
for _, index := range indexes { for _, index := range indexes {
res = append(res, gin.H{ res = append(res, gin.H{
HTTPIndexName: index.IndexName, HTTPIndexName: index.IndexName,
@ -291,7 +291,7 @@ func checkAndSetData(body string, collSchema *schemapb.CollectionSchema) (error,
return merr.ErrMissingRequiredParameters, reallyDataArray, validDataMap return merr.ErrMissingRequiredParameters, reallyDataArray, validDataMap
} }
var fieldNames []string fieldNames := make([]string, 0, len(collSchema.Fields))
for _, field := range collSchema.Fields { for _, field := range collSchema.Fields {
if field.IsDynamic { if field.IsDynamic {
continue continue
@ -1099,12 +1099,12 @@ func anyToColumns(rows []map[string]interface{}, validDataMap map[string][]bool,
} }
func serializeFloatVectors(vectorStr string, dataType schemapb.DataType, dimension, bytesLen int64, fpArrayToBytesFunc func([]float32) []byte) ([][]byte, error) { func serializeFloatVectors(vectorStr string, dataType schemapb.DataType, dimension, bytesLen int64, fpArrayToBytesFunc func([]float32) []byte) ([][]byte, error) {
values := make([][]byte, 0)
var fp32Values [][]float32 var fp32Values [][]float32
err := json.Unmarshal([]byte(vectorStr), &fp32Values) err := json.Unmarshal([]byte(vectorStr), &fp32Values)
if err != nil { if err != nil {
return nil, merr.WrapErrParameterInvalid(schemapb.DataType_name[int32(dataType)], vectorStr, err.Error()) return nil, merr.WrapErrParameterInvalid(schemapb.DataType_name[int32(dataType)], vectorStr, err.Error())
} }
values := make([][]byte, 0, len(fp32Values))
for _, vectorArray := range fp32Values { for _, vectorArray := range fp32Values {
if int64(len(vectorArray)) != dimension { if int64(len(vectorArray)) != dimension {
return nil, merr.WrapErrParameterInvalid(schemapb.DataType_name[int32(dataType)], vectorStr, return nil, merr.WrapErrParameterInvalid(schemapb.DataType_name[int32(dataType)], vectorStr,
@ -1150,7 +1150,7 @@ func serializeFloatOrByteVectors(jsonResult gjson.Result, dataType schemapb.Data
} }
func serializeSparseFloatVectors(vectors []gjson.Result, dataType schemapb.DataType) ([][]byte, error) { func serializeSparseFloatVectors(vectors []gjson.Result, dataType schemapb.DataType) ([][]byte, error) {
values := make([][]byte, 0) values := make([][]byte, 0, len(vectors))
for _, vector := range vectors { for _, vector := range vectors {
vectorBytes := []byte(vector.String()) vectorBytes := []byte(vector.String())
sparseVector, err := typeutil.CreateSparseFloatRowFromJSON(vectorBytes) sparseVector, err := typeutil.CreateSparseFloatRowFromJSON(vectorBytes)
@ -1185,6 +1185,7 @@ func convertQueries2Placeholder(body string, dataType schemapb.DataType, dimensi
case schemapb.DataType_VarChar: case schemapb.DataType_VarChar:
valueType = commonpb.PlaceholderType_VarChar valueType = commonpb.PlaceholderType_VarChar
res := gjson.Get(body, HTTPRequestData).Array() res := gjson.Get(body, HTTPRequestData).Array()
values = make([][]byte, 0, len(res))
for _, v := range res { for _, v := range res {
values = append(values, []byte(v.String())) values = append(values, []byte(v.String()))
} }
@ -1242,8 +1243,6 @@ func genDynamicFields(fields []string, list []*schemapb.FieldData) []string {
} }
func buildQueryResp(rowsNum int64, needFields []string, fieldDataList []*schemapb.FieldData, ids *schemapb.IDs, scores []float32, enableInt64 bool) ([]map[string]interface{}, error) { func buildQueryResp(rowsNum int64, needFields []string, fieldDataList []*schemapb.FieldData, ids *schemapb.IDs, scores []float32, enableInt64 bool) ([]map[string]interface{}, error) {
var queryResp []map[string]interface{}
columnNum := len(fieldDataList) columnNum := len(fieldDataList)
if rowsNum == int64(0) { // always if rowsNum == int64(0) { // always
if columnNum > 0 { if columnNum > 0 {
@ -1299,6 +1298,7 @@ func buildQueryResp(rowsNum int64, needFields []string, fieldDataList []*schemap
if rowsNum == int64(0) { if rowsNum == int64(0) {
return []map[string]interface{}{}, nil return []map[string]interface{}{}, nil
} }
queryResp := make([]map[string]interface{}, 0, rowsNum)
dynamicOutputFields := genDynamicFields(needFields, fieldDataList) dynamicOutputFields := genDynamicFields(needFields, fieldDataList)
for i := int64(0); i < rowsNum; i++ { for i := int64(0); i < rowsNum; i++ {
row := map[string]interface{}{} row := map[string]interface{}{}
@ -1439,7 +1439,7 @@ func buildQueryResp(rowsNum int64, needFields []string, fieldDataList []*schemap
} }
func formatInt64(intArray []int64) []string { func formatInt64(intArray []int64) []string {
stringArray := make([]string, 0) stringArray := make([]string, 0, len(intArray))
for _, i := range intArray { for _, i := range intArray {
stringArray = append(stringArray, strconv.FormatInt(i, 10)) stringArray = append(stringArray, strconv.FormatInt(i, 10))
} }