-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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
proposal: sync/v2: new package #71076
Comments
Related Issues
Related Documentation Related Discussions (Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.) |
There is one issue I’ve encountered with the current How the |
Part of me wants to change to: type CondLocker[T any] interface {
Locker
*T
}
type Cond[T any, L CondLocker[T]] struct {
L T
}
// Random example pseudo code to prove this is exploitable
func (c *Cond[T, L]) Test() {
var p L = &c.L
p.Lock()
p.Unlock()
}
// ... this makes the zero value of a cond valid if the locker's zero value is valid (like Other part of me thinks this is too much |
I'm wondering if there's even a need to make I'm not familiar with the use cases for |
I don't fully understand this piece:
For panicking if V is not a comparable type and doing the equality check if V is a comparable type — Is the intent of this proposal to use In #47657 (comment), Ian had written:
Separately, @Merovius pointed out in #47657 (comment):
... but I'm not sure what solution this proposal is relying upon (or maybe I misunderstood the issue 😅). |
Just realized: A trivial way to get those semantics is |
This is exciting! Should |
I think the goals here include
When those goals are put together, it seems hard to avoid having I am definitely open to suggestions.
I'm not specifying an implementation, but looking at the current implementation it seems straightforward for the v2 |
I think we could in principle drop I think that in practical use a If we were designing from scratch it might make sense to make the |
since the original sync package will continue to be available with Cond, do we really need to have another copy elsewhere? |
@thepudds What I'm trying to express in |
We could have a NewPool function that initializes a pool with a constructor function.
|
I think a goal here should be that people can, with a little bit of work, change from using sync to using sync/v2. We don't want a state in which people are using both the sync and sync/v2 package with no prospect of ever moving completely to the sync/v2 package. |
I don't think we should change A Pool must return a pointer or pointer-containing object to be useful. (A Pool is a pool of references to temporary objects. Since a Pool doesn't report when objects are evicted, those references must be garbage collected, and therefore must be pointers.) If the pooled objects contain pointers, it is possible to distinguish between the zero value reference and an initialized reference. Therefore, there is no need for |
@neild type Pool[T any] struct {
// contains filtered or unexported fields
}
func NewPool[T any](newf func() *T) *Pool[T]
func (p *Pool[T]) Put(x *T)
func (p *Pool[T]) Get() *T Edit: just ignore me; I had overlooked #71076 (comment). |
@jub0bs this will force people to store pointers to the slices which was quite a big problem with the original Pool. |
@neild's comment echos prior comments, which seems to be supported by various others. As some empirical experience supporting dropping |
What about pooling |
Before considering folding golang.org/x/sync/errgroup into sync/v2, its API should be improved. Relevant issue (and comments): #57534. |
Could it be I'm in favor of dropping |
Playing with it on the playground, this is legal syntax, but the type inference requires both type parameters, which is unfortunate. |
What exactly is the issue with just making it I also don't think there is a reason to forbid Restricting that does, IMO, nothing to help, but is unnecessarily restrictive. |
That would prevent reusing
There are other types which have pointers inside. |
@neild Thanks, I've updated the proposal to remove the |
Proposal Details
The math/rand/v2 package has been successful. Let's consider another v2 package: sync/v2.
This is an update of #47657.
In the sync/v2 package, the following types and functions will be the same as in the current sync package:
The existing
Map
type will be replaced by a new type that takes two type parameters. Note that the originalRange
method is replaced with anAll
method that returns an iterator.The existing
Pool
type will be replaced by a new type that takes a type parameter.2025-01-04: The new version of
Pool
does not have an exportedNew
field; instead, useNewPool
to create aPool
that calls a function to return new values.Note that the originalGet
method is replaced by one that returns two results, with the second being abool
indicating whether a value was returned. This is only useful if the New field is optional: we could also change this to require the New field to be set, or to provide a default implementation that returns the zero value ofT
.The text was updated successfully, but these errors were encountered: