Skip to content
This repository was archived by the owner on Oct 18, 2024. It is now read-only.

Commit bd57b48

Browse files
authored
Merge pull request #88 from khevse/master
fix (convert): syscalls optimizations
2 parents 03af1fd + e74fce0 commit bd57b48

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

gocov/convert.go

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,23 @@ import (
2626
"go/build"
2727
"go/parser"
2828
"go/token"
29+
"os"
2930
"path/filepath"
30-
31-
"golang.org/x/tools/cover"
31+
"strings"
3232

3333
"github.com/axw/gocov"
3434
"github.com/axw/gocov/gocovutil"
35+
"golang.org/x/tools/cover"
3536
)
3637

38+
type packagesCache map[string]*build.Package
39+
3740
func convertProfiles(filenames ...string) error {
38-
var ps gocovutil.Packages
41+
var (
42+
ps gocovutil.Packages
43+
packages = make(packagesCache)
44+
)
45+
3946
for i := range filenames {
4047
converter := converter{
4148
packages: make(map[string]*gocov.Package),
@@ -45,7 +52,7 @@ func convertProfiles(filenames ...string) error {
4552
return err
4653
}
4754
for _, p := range profiles {
48-
if err := converter.convertProfile(p); err != nil {
55+
if err := converter.convertProfile(packages, p); err != nil {
4956
return err
5057
}
5158
}
@@ -54,11 +61,9 @@ func convertProfiles(filenames ...string) error {
5461
ps.AddPackage(pkg)
5562
}
5663
}
57-
bytes, err := marshalJson(ps)
58-
if err != nil {
64+
if err := marshalJson(os.Stdout, ps); err != nil {
5965
return err
6066
}
61-
fmt.Println(string(bytes))
6267
return nil
6368
}
6469

@@ -72,8 +77,8 @@ type statement struct {
7277
*StmtExtent
7378
}
7479

75-
func (c *converter) convertProfile(p *cover.Profile) error {
76-
file, pkgpath, err := findFile(p.FileName)
80+
func (c *converter) convertProfile(packages packagesCache, p *cover.Profile) error {
81+
file, pkgpath, err := findFile(packages, p.FileName)
7782
if err != nil {
7883
return err
7984
}
@@ -130,15 +135,20 @@ func (c *converter) convertProfile(p *cover.Profile) error {
130135
}
131136

132137
// findFile finds the location of the named file in GOROOT, GOPATH etc.
133-
func findFile(file string) (filename string, pkgpath string, err error) {
138+
func findFile(packages packagesCache, file string) (filename, pkgpath string, err error) {
134139
dir, file := filepath.Split(file)
135140
if dir != "" {
136-
dir = dir[:len(dir)-1] // drop trailing '/'
141+
dir = strings.TrimSuffix(dir, "/")
137142
}
138-
pkg, err := build.Import(dir, ".", build.FindOnly)
139-
if err != nil {
140-
return "", "", fmt.Errorf("can't find %q: %v", file, err)
143+
pkg, ok := packages[dir]
144+
if !ok {
145+
pkg, err = build.Import(dir, ".", build.FindOnly)
146+
if err != nil {
147+
return "", "", fmt.Errorf("can't find %q: %w", file, err)
148+
}
149+
packages[dir] = pkg
141150
}
151+
142152
return filepath.Join(pkg.Dir, file), pkg.ImportPath, nil
143153
}
144154

gocov/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"encoding/json"
2525
"flag"
2626
"fmt"
27+
"io"
2728
"os"
2829

2930
"github.com/axw/gocov"
@@ -41,8 +42,8 @@ func usage() {
4142
os.Exit(2)
4243
}
4344

44-
func marshalJson(packages []*gocov.Package) ([]byte, error) {
45-
return json.Marshal(struct{ Packages []*gocov.Package }{packages})
45+
func marshalJson(w io.Writer, packages []*gocov.Package) error {
46+
return json.NewEncoder(w).Encode(struct{ Packages []*gocov.Package }{packages})
4647
}
4748

4849
func unmarshalJson(data []byte) (packages []*gocov.Package, err error) {

0 commit comments

Comments
 (0)