Skip to content

Commit

Permalink
slightly more efficient loading of data
Browse files Browse the repository at this point in the history
  • Loading branch information
jesseduffield committed Apr 12, 2020
1 parent dfcceb9 commit 8481350
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 16 deletions.
21 changes: 17 additions & 4 deletions pkg/commands/config_parsing.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package commands

import (
"bytes"
"crypto/sha256"
"io"
"io/ioutil"

"github.com/buger/jsonparser"
)
Expand All @@ -16,13 +17,25 @@ func unescape(b []byte) string {
return string(buf)
}

func UnmarshalPackageConfig(r io.Reader) (*PackageConfig, error) {
configData, err := ioutil.ReadAll(r)
func UnmarshalPackageConfig(r io.Reader, previousPackageConfig *PackageConfig) (*PackageConfig, error) {
var buf bytes.Buffer
h := sha256.New()
wr := io.MultiWriter(&buf, h)

_, err := io.Copy(wr, r)
if err != nil {
return nil, err
}
configData := buf.Bytes()

// we need to create a hash of this thing and see if it's changed. If it hasn't we'll use the previous one.
sha := h.Sum(nil)

if previousPackageConfig != nil && bytes.Equal(sha, previousPackageConfig.Sha) {
return previousPackageConfig, nil
}

pkgConfig := &PackageConfig{}
pkgConfig := &PackageConfig{Sha: sha}

type stringMapping struct {
path []string
Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/config_parsing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestUnmarshalPackageConfig(t *testing.T) {
panic(err)
}

config, err := UnmarshalPackageConfig(file)
config, err := UnmarshalPackageConfig(file, nil)

assert.NoError(t, err)
assert.EqualValues(t, s.expectedPackageConfig, config)
Expand Down
19 changes: 14 additions & 5 deletions pkg/commands/npm_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,12 @@ func (m *NpmManager) IsLinked(name string, path string) (bool, error) {
return false, nil
}

func (m *NpmManager) GetPackages(paths []string) ([]*Package, error) {
func (m *NpmManager) GetPackages(paths []string, previousPackages []*Package) ([]*Package, error) {

previousPackageConfigMap := map[string]*PackageConfig{}
for _, prevPkg := range previousPackages {
previousPackageConfigMap[prevPkg.Path] = &prevPkg.Config
}

pkgs := make([]*Package, 0, len(paths))

Expand All @@ -78,7 +83,7 @@ func (m *NpmManager) GetPackages(paths []string) ([]*Package, error) {
m.Log.Error(err)
continue
}
pkgConfig, err := UnmarshalPackageConfig(file)
pkgConfig, err := UnmarshalPackageConfig(file, previousPackageConfigMap[path])
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -121,12 +126,13 @@ func (m *NpmManager) ChdirToPackageRoot() (bool, error) {
}
}

func (m *NpmManager) GetDeps(currentPkg *Package) ([]*Dependency, error) {
deps := currentPkg.SortedDependencies()
func (m *NpmManager) GetDeps(currentPkg *Package, previousDeps []*Dependency) ([]*Dependency, error) {
deps := currentPkg.SortedDependencies(previousDeps)

for _, dep := range deps {
depPath := filepath.Join(currentPkg.Path, "node_modules", dep.Name)
dep.Path = depPath
dep.LinkPath = ""
dep.ParentPackagePath = currentPkg.Path
fileInfo, err := os.Lstat(depPath)
if err != nil {
Expand All @@ -143,8 +149,10 @@ func (m *NpmManager) GetDeps(currentPkg *Package) ([]*Dependency, error) {
m.Log.Error(err)
continue
}
pkgConfig, err := UnmarshalPackageConfig(file)

pkgConfig, err := UnmarshalPackageConfig(file, dep.PackageConfig)
if err != nil {
dep.PackageConfig = nil
// swallowing error
m.Log.Error(err)
} else {
Expand All @@ -160,6 +168,7 @@ func (m *NpmManager) GetDeps(currentPkg *Package) ([]*Dependency, error) {
if err != nil {
return nil, err
}

dep.LinkPath = linkPath
}

Expand Down
22 changes: 21 additions & 1 deletion pkg/commands/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
)

type PackageConfig struct {
// this is the sha256 of the pakage.json file
Sha []byte
Name string
Version string
License string
Expand Down Expand Up @@ -83,7 +85,7 @@ type Package struct {
LinkedGlobally bool
}

func (p *Package) SortedDependencies() []*Dependency {
func (p *Package) SortedDependencies(previousDeps []*Dependency) []*Dependency {
deps := make([]*Dependency, 0, len(p.Config.Dependencies)+len(p.Config.DevDependencies)+len(p.Config.PeerDependencies)+len(p.Config.OptionalDependencies))

type blah struct {
Expand Down Expand Up @@ -123,9 +125,27 @@ func (p *Package) SortedDependencies() []*Dependency {
deps = append(deps, depsForKind...)
}

// if what we get is exactly the same as last time in terms of constraint, kind, and name, then just return last time's things because they've got even more data that we can cache
if DepArraysMatch(deps, previousDeps) {
return previousDeps
}

return deps
}

func DepArraysMatch(d1, d2 []*Dependency) bool {
if len(d1) != len(d2) {
return false
}

for i := range d1 {
if d1[i].Name != d2[i].Name || d1[i].Constraint != d2[i].Constraint || d1[i].Kind != d2[i].Kind {
return false
}
}
return true
}

func (p *Package) SortedScripts() []*Script {
scripts := make([]*Script, 0, len(p.Config.Scripts))
for name, command := range p.Config.Scripts {
Expand Down
5 changes: 2 additions & 3 deletions pkg/gui/list_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,8 @@ func (gui *Gui) packagesListView() *listView {

func (gui *Gui) depsListView() *listView {
return &listView{
viewName: "deps",
// TODO: handle more dep types
getItemsLength: func() int { return len(gui.currentPackage().SortedDependencies()) },
viewName: "deps",
getItemsLength: func() int { return len(gui.State.Deps) },
getSelectedLineIdxPtr: func() *int { return &gui.State.Panels.Deps.SelectedLine },
handleFocus: gui.handleDepSelect,
handleItemSelect: gui.handleDepSelect,
Expand Down
4 changes: 2 additions & 2 deletions pkg/gui/packages_panel.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ func (gui *Gui) currentPackage() *commands.Package {
func (gui *Gui) refreshStatePackages() error {
// get files to stage
var err error
gui.State.Packages, err = gui.NpmManager.GetPackages(gui.Config.GetAppState().RecentPackages)
gui.State.Packages, err = gui.NpmManager.GetPackages(gui.Config.GetAppState().RecentPackages, gui.State.Packages)
if err != nil {
return err
}

gui.State.Deps, err = gui.NpmManager.GetDeps(gui.currentPackage())
gui.State.Deps, err = gui.NpmManager.GetDeps(gui.currentPackage(), gui.State.Deps)
if err != nil {
return err
}
Expand Down

0 comments on commit 8481350

Please sign in to comment.