Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a concrete struct while parsing JSON to reduce memory consumption. #153

Merged
merged 2 commits into from
Nov 8, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions pkg/feeds/npm/npm.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,18 +90,19 @@ func fetchPackage(baseURL, pkgTitle string) ([]*Package, error) {
if err != nil {
return nil, err
}
var jsonMap map[string]interface{}
err = json.Unmarshal(body, &jsonMap)

// We only care about the `time` field as it contains all the versions in
// date order, from oldest to newest.
// Using a struct for parsing also avoids the cost of deserializing data
// that is ultimately unused.
var packageDetails struct {
Time map[string]interface{} `json:"time"`
}
err = json.Unmarshal(body, &packageDetails)
if err != nil {
return nil, fmt.Errorf("%w : %v for package %s", errJSON, err, pkgTitle)
}

// The json string `time` contains versions in date order, oldest to newest.
versions, ok := jsonMap["time"].(map[string]interface{})
if !ok {
return nil, fmt.Errorf("%w : 'time' not found for package %s ",
errJSON, pkgTitle)
}
versions := packageDetails.Time

// If `unpublished` exists in the version map then at a given point in time
// the package was 'entirely' removed, the packageEvent(s) received are for package
Expand Down