perf: pre-allocate all known length arrays to avoid re-scaling (#5029)

Co-authored-by: Kevin Wan <wanjunfeng@gmail.com>
This commit is contained in:
Ioannis Pinakoulakis
2025-08-08 19:03:25 +03:00
committed by GitHub
parent a2b98dbcf7
commit 130e1ba963
19 changed files with 29 additions and 28 deletions

View File

@@ -36,13 +36,13 @@ func genConfig(dir string, cfg *config.Config, api *spec.ApiSpec) error {
}
authNames := getAuths(api)
var auths []string
auths := make([]string, 0, len(authNames))
for _, item := range authNames {
auths = append(auths, fmt.Sprintf("%s %s", item, jwtTemplate))
}
jwtTransNames := getJwtTrans(api)
var jwtTransList []string
jwtTransList := make([]string, 0, len(jwtTransNames))
for _, item := range jwtTransNames {
jwtTransList = append(jwtTransList, fmt.Sprintf("%s %s", item, jwtTransTemplate))
}

View File

@@ -43,7 +43,7 @@ func genMain(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) error {
}
func genMainImports(parentPkg string) string {
var imports []string
imports := make([]string, 0, 5)
imports = append(imports, fmt.Sprintf("\"%s\"", pathx.JoinPackages(parentPkg, configDir)))
imports = append(imports, fmt.Sprintf("\"%s\"", pathx.JoinPackages(parentPkg, handlerDir)))
imports = append(imports, fmt.Sprintf("\"%s\"\n", pathx.JoinPackages(parentPkg, contextDir)))

View File

@@ -154,7 +154,7 @@ func genTypesWithGroup(dir string, cfg *config.Config, api *spec.ApiSpec) error
}
for group, typeGroup := range groupTypes {
var types []spec.Type
types := make([]spec.Type, 0, len(typeGroup))
for _, v := range typeGroup {
types = append(types, v)
}