Skip to content

RFC: Use Go 1.23 iterators for locking+traversing stores. #2288

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mtrmac
Copy link
Collaborator

@mtrmac mtrmac commented Mar 18, 2025

RFC: this is a ~demo, only migrating one function out of several.

This replaces one kind of boilerplate with another, but overall seems a bit less repetitive. The primary benefit is that the code working within the locked stores can return directly, without boilerplate, and there is no need to manually declare a func(…) for the loop body.

The for store, err := range syntax is a bit weird, admittedly, but maybe worth it.

The primary downside is that this really doesn’t work for the exactly-one-closure-call helpers (writeTo…Store):

  • Using for store, err := range writeToSingleStore is very unintuitive, but maybe acceptable with good helper names
  • But Go doesn’t recognize that the loop body executes exactly once, and that if the loop body always returns, there is no need for the code afterwards to return as well. So every caller would need to add an unreachable invented return there… and that seems a bit much for me.

So, we would end up with two rather different helper patterns instead of ~one. I’m really unsure whether it is worth it. It might well make sense to leave the code as is, and to wait another 1/5/10 years for Go to gain type inference and a convenient closure syntax.

This replaces one kind of boilerplate with another,
but overall seems a bit less repetitive.

Signed-off-by: Miloslav Trmač <[email protected]>
Copy link
Contributor

openshift-ci bot commented Mar 18, 2025

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: 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

Copy link
Member

@Luap99 Luap99 left a comment

Choose a reason for hiding this comment

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

I am not an active c/storage contributor but I read the code regularly (i.e. when debugging a podman issue) and this seems to me clearly an improvement over the callback. It feels much more natural to read the callers as loops so I like this a lot.

// …
// }; done {
// return res, err
// for rlstore, err := range s.readAllLayerStores() {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Note to self: This looks almost too easy, without warning callers about the semantics. Maybe range iterateLockingAllLayerStores ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

iterateReadLockingAllLayerStores? As usual, I tend towards unwieldy names.

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.

2 participants