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

Avoid manually-coded loops #2087

Merged
merged 5 commits into from
Sep 5, 2024
Merged

Conversation

mtrmac
Copy link
Collaborator

@mtrmac mtrmac commented Sep 4, 2024

Use slices and maps, or other existing features, where appropriate.

This is basically a single pass looking for append.*..., and skimming all for loops.

Copy link
Contributor

@cgwalters cgwalters left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great to me overall

@@ -1641,13 +1641,11 @@ func (s *store) CreateImage(id string, names []string, layer, metadata string, i
options.Metadata = iOptions.Metadata
}
options.BigData = append(options.BigData, copyImageBigDataOptionSlice(iOptions.BigData)...)
options.NamesHistory = append(options.NamesHistory, copyStringSlice(iOptions.NamesHistory)...)
options.NamesHistory = append(options.NamesHistory, iOptions.NamesHistory...)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only skimmed/spot checked but I noticed in this one we stopped copying...expected?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes — append is “grow slice + copy incoming data to the destination”; so making another copy of the incoming data could only make a difference if the source and destination were overlapping. That’s not the case here (the destination is either unset, or already a freshly-allocated clone).

Actually I’ll remove another copy when updating options.Digests field above.

The BigData one is different - copyImageBigDataOptionSlice is a deep copy, but append only does a shallow one.

Copy link
Contributor

openshift-ci bot commented Sep 4, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: cgwalters, mtrmac

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
Signed-off-by: Miloslav Trmač <mitr@redhat.com>
Conservatively use Index* + Delete to delete the
first element where it's not obvious that the code would really
want to delete all instances.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
Signed-off-by: Miloslav Trmač <mitr@redhat.com>
Use the "slices", "maps" standard library packages, or other
readily-available features.

Should not change behavior.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
@rhatdan
Copy link
Member

rhatdan commented Sep 5, 2024

/lgtm

@openshift-ci openshift-ci bot added the lgtm label Sep 5, 2024
@openshift-merge-bot openshift-merge-bot bot merged commit 9f9e76b into containers:main Sep 5, 2024
18 checks passed
@mtrmac mtrmac deleted the shorten branch September 5, 2024 19:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants