Skip to content

Commit

Permalink
Performance improvement.
Browse files Browse the repository at this point in the history
Improve performance by skipping comparison of functions that can't meet threshold.  Handles hard parsing errors.
  • Loading branch information
jeffsvajlenko committed Feb 14, 2022
1 parent c3b640c commit 88c5c12
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
10 changes: 6 additions & 4 deletions internal/detection/detection.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ func DetectClones(pset *parsing.ParseSet, threshold float64) (*CloneSet, error)

for j := i + 1; j < len(functions); j++ {
f2 := functions[j]
if isClone(f1, f2, threshold) {
cclones <- Clone{
FunctionId1: f1.Id,
FunctionId2: f2.Id,
if math.Ceil(math.Max(float64(f1.LineLength), float64(f2.LineLength))*threshold) > math.Min(float64(f1.LineLength), float64(f1.LineLength)) {
if isClone(f1, f2, threshold) {
cclones <- Clone{
FunctionId1: f1.Id,
FunctionId2: f2.Id,
}
}
}
}
Expand Down
22 changes: 17 additions & 5 deletions internal/parsing/parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"go/printer"
"go/token"
"io/fs"
"log"
"path/filepath"
"strings"
"sync"
Expand Down Expand Up @@ -36,6 +37,7 @@ type Function struct {
StartColumn uint
EndLine uint
EndColumn uint
LineLength uint
Name string
Params []Param
Results []Param
Expand Down Expand Up @@ -87,10 +89,19 @@ func Parse(files []string) (*ParseSet, error) {

go func(file File, cfunc chan Function) {
defer wg.Done()

functions, _ := parsefile(file, parseSet.FileSet, &parseSet.Strings)
for _, function := range functions {
cfunc <- function
defer func() {
if perr := recover(); perr != nil {
log.Println("parsing failed for file", file.Path)
}
}()

functions, errs := parseFile(file, parseSet.FileSet, &parseSet.Strings)
if len(errs) > 0 {
log.Println("prasing failed for file", file)
} else {
for _, function := range functions {
cfunc <- function
}
}
}(file, cfunc)
}
Expand All @@ -105,7 +116,7 @@ func Parse(files []string) (*ParseSet, error) {
return &parseSet, nil
}

func parsefile(file File, fset *token.FileSet, intern *sync.Map) ([]Function, []error) {
func parseFile(file File, fset *token.FileSet, intern *sync.Map) ([]Function, []error) {
retval := make([]Function, 0, 10)

errors := make([]error, 0, 1)
Expand Down Expand Up @@ -163,6 +174,7 @@ func parsefile(file File, fset *token.FileSet, intern *sync.Map) ([]Function, []
StartColumn: uint(startPosition.Column),
EndLine: uint(endPosition.Line),
EndColumn: uint(endPosition.Column),
LineLength: uint(endPosition.Line - startPosition.Line + 1),
Name: d.Name.Name,
Params: params,
Results: results,
Expand Down

0 comments on commit 88c5c12

Please sign in to comment.