Skip to content

Commit

Permalink
optimize: add percentage for trim result (#220)
Browse files Browse the repository at this point in the history
  • Loading branch information
HeyJavaBean authored Jul 24, 2024
1 parent d75301b commit 263fa24
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 16 deletions.
7 changes: 5 additions & 2 deletions generator/golang/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,14 @@ func (g *GoBackend) Generate(req *plugin.Request, log backend.LogFunc) *plugin.R
g.prepareUtilities()
if g.utils.Features().TrimIDL {
g.log.Warn("You Are Using IDL Trimmer")
structureTrimmed, fieldTrimmed, err := trim.TrimAST(&trim.TrimASTArg{Ast: req.AST, TrimMethods: nil, Preserve: nil})
tr, err := trim.TrimAST(&trim.TrimASTArg{Ast: req.AST, TrimMethods: nil, Preserve: nil})
if err != nil {
g.log.Warn("trim error:", err.Error())
}
g.log.Warn(fmt.Sprintf("removed %d unused structures with %d fields", structureTrimmed, fieldTrimmed))

g.log.Warn(fmt.Sprintf("removed %d unused structures with %d fields", tr.StructsTrimmed, tr.FieldsTrimmed))

g.log.Warn(fmt.Sprintf("structs:%d->%d (%.1f%% Trimmed), fields:%d->%d (%.1f%% Trimmed).", tr.StructsTotal, tr.StructsLeft(), tr.StructTrimmedPercentage(), tr.FieldsTotal, tr.FieldsLeft(), tr.FieldTrimmedPercentage()))
}
g.prepareTemplates()
g.fillRequisitions()
Expand Down
2 changes: 1 addition & 1 deletion tool/trimmer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func main() {
structs, fields := countStructs(ast)

// trim ast
_, _, err = trim.TrimAST(&trim.TrimASTArg{
_, err = trim.TrimAST(&trim.TrimASTArg{
Ast: ast, TrimMethods: a.Methods, Preserve: preserveInput,
})
check(err)
Expand Down
48 changes: 39 additions & 9 deletions tool/trimmer/trim/trimmer.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,31 @@ type TrimASTArg struct {
MatchGoName *bool
}

type TrimResultInfo struct {
StructsTrimmed int
FieldsTrimmed int
StructsTotal int
FieldsTotal int
}

func (t *TrimResultInfo) StructsLeft() int {
return t.StructsTotal - t.StructsTrimmed
}

func (t *TrimResultInfo) FieldsLeft() int {
return t.FieldsTotal - t.FieldsTrimmed
}

func (t *TrimResultInfo) StructTrimmedPercentage() float64 {
return float64(t.StructsTrimmed) / float64(t.StructsTotal) * 100
}

func (t *TrimResultInfo) FieldTrimmedPercentage() float64 {
return float64(t.FieldsTrimmed) / float64(t.FieldsTotal) * 100
}

// TrimAST parse the cfg and trim the single AST
func TrimAST(arg *TrimASTArg) (structureTrimmed int, fieldTrimmed int, err error) {
func TrimAST(arg *TrimASTArg) (trimResultInfo *TrimResultInfo, err error) {
var preservedStructs []string
if wd, err := dir_utils.Getwd(); err == nil {
cfg := ParseYamlConfig(wd)
Expand Down Expand Up @@ -86,10 +109,10 @@ func TrimAST(arg *TrimASTArg) (structureTrimmed int, fieldTrimmed int, err error

// doTrimAST trim the single AST, pass method names if -m specified
func doTrimAST(ast *parser.Thrift, trimMethods []string, forceTrimming bool, matchGoName bool, preservedStructs []string) (
structureTrimmed int, fieldTrimmed int, err error) {
trimResultInfo *TrimResultInfo, err error) {
trimmer, err := newTrimmer(nil, "")
if err != nil {
return 0, 0, err
return nil, err
}
trimmer.asts[ast.Filename] = ast
trimmer.trimMethods = make([]*regexp2.Regexp, len(trimMethods))
Expand All @@ -110,33 +133,40 @@ func doTrimAST(ast *parser.Thrift, trimMethods []string, forceTrimming bool, mat
}
trimmer.trimMethods[i], err = regexp2.Compile(trimMethods[i], 0)
if err != nil {
return 0, 0, err
return nil, err
}
}
trimmer.preservedStructs = preservedStructs
trimmer.countStructs(ast)
originStructsNum := trimmer.structsTrimmed
originFieldNum := trimmer.fieldsTrimmed
trimmer.markAST(ast)
trimmer.traversal(ast, ast.Filename)
if path := parser.CircleDetect(ast); len(path) > 0 {
return 0, 0, fmt.Errorf("found include circle:\n\t%s", path)
return nil, fmt.Errorf("found include circle:\n\t%s", path)
}
checker := semantic.NewChecker(semantic.Options{FixWarnings: true})
_, err = checker.CheckAll(ast)
if err != nil {
return 0, 0, err
return nil, err
}
err = semantic.ResolveSymbols(ast)
if err != nil {
return 0, 0, err
return nil, err
}

for i, method := range trimMethods {
if !trimmer.trimMethodValid[i] {
return 0, 0, fmt.Errorf("err: method %s not found!\n", method)
return nil, fmt.Errorf("err: method %s not found!\n", method)
}
}

return trimmer.structsTrimmed, trimmer.fieldsTrimmed, nil
return &TrimResultInfo{
StructsTrimmed: trimmer.structsTrimmed,
FieldsTrimmed: trimmer.fieldsTrimmed,
StructsTotal: originStructsNum,
FieldsTotal: originFieldNum,
}, nil
}

// Trim to trim thrift files to remove unused fields
Expand Down
2 changes: 1 addition & 1 deletion tool/trimmer/trim/trimmer_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TrimBatchContentWithConfig(mainIDLFilePath string, IDLFileContentMap map[st
}

trimArgs.Ast = ast
_, _, err = TrimAST(&trimArgs)
_, err = TrimAST(&trimArgs)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions tool/trimmer/trim/trimmer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func TestTrimMethod(t *testing.T) {
methods := make([]string, 1)
methods[0] = "func1"

_, _, err = TrimAST(&TrimASTArg{
_, err = TrimAST(&TrimASTArg{
Ast: ast,
TrimMethods: methods,
Preserve: nil,
Expand All @@ -119,7 +119,7 @@ func TestPreserve(t *testing.T) {

preserve := false

_, _, err = TrimAST(&TrimASTArg{
_, err = TrimAST(&TrimASTArg{
Ast: ast,
TrimMethods: nil,
Preserve: &preserve,
Expand Down
2 changes: 1 addition & 1 deletion version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@

package version

const ThriftgoVersion = "0.3.14"
const ThriftgoVersion = "0.3.15"

0 comments on commit 263fa24

Please sign in to comment.