Repository navigation
Description
Proposal Details
// Slice returns a globally unique handle for the slice. Slices are equal as compared by [slices.Equal].
// Handle stores a cloned copy of the slice. Every slice returned by [Handle.Value] for a given [Handle]
// shares the same backing array: slices must be cloned before modification.
func Slice[S ~[]E, E comparable](s S) Handle[S]I'd like to be able to intern slices, the building block for collections of values in Go.
While slices aren't natively comparable in Go, they do have a fairly straightforward equality comparison as demonstrated with slices.Equal.
As unique currently already clones its inputs, I expect slices to be treated similarly: the slice and its backing array are cloned to a new copy, detached form the input.
A potential footgun is the value returned by Value: the elements of the backing array of the returned slice may be modified by the user, resulting in a different slice from the value passed in / represented by the key.
This can be mitigated by cloning the slice again in Value, but I don't think it should be done automatically by Value.
The cost of calculating the unique Handle means using unique may only make sense for sufficiently large values, or values with string-like properties where values hold pointers to readonly data. While we can't express that in Go's type system today, with sufficient care it can be used to optimize user code.
cc @mknyszek
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsNo status
- StatusShow more project fieldsNo status
Activity
gabyhelp commentedon May 5, 2025
Related Issues
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
mateusz834 commentedon May 5, 2025
This would require a change in Handle signature.
into
Can we change that in terms of backwards-compatibility?
mknyszek commentedon May 5, 2025
The concept of a frozen or 'stable' value would help a lot in writing APIs like this.
At face value, we've got two less-than-ideal options:
Handleaccidentally breaks howuniquefundamentally works.The problems with (2) might be possible to avert in many cases with a smarter compiler. Currently the compiler can't stack-allocate dynamically sized things, but if we had an answer to that, it might work better. (
Valuewould need to be inlined, but that doesn't seem that hard to ensure.)A third alternative might be to have a new API to pass a slice to copy into. This would benefit from a dedicated 'slice handle' type. For example:
EDIT: Updated based on @mateusz834's comment below.
We could also provide a separate
ValueorClonemethod for convenience.This API is unfortunate but also doesn't seem that bad.
mknyszek commentedon May 5, 2025
Oof, though, I can see my suggestion above quickly devolving into "let's support all immutable operations on slices." 😅
You could also imagine:
... On the other hand, maybe that's not that bad? It is a lot of API just to work around the inability to express an immutable slice.
EDIT: Updated based on @mateusz834's comment below.
mateusz834 commentedon May 5, 2025
One drawback of this API is the signature, say you want to use it in a struct and then you have to specify these two type-params. I think in the std we only used the
[S ~[]E, E comparable]form in funcs, where they are nicely infered.DeedleFake commentedon May 5, 2025
Would it be feasible to create an
Immutable[T any]orView[T any]inslicesthat, ifTiscomparable, could itself be used withuniqueinstead? Immutable slice views could be useful for a variety of things.mknyszek commentedon May 5, 2025
@mateusz834 Makes sense. It should probably just be
type Slice[E comparable] struct { ... }. Updated my comments above.@DeedleFake That might be one way to go about it, yeah.
uniquecould just return theslices.Immutable[T](or whatever it would be called) instead.The path this is going down is starting to seem more like a language change and that's a much deeper rabbit hole...
mateusz834 commentedon May 5, 2025
Also there is a third option to just document that the slice cannot be modified and put the slice in a read-only memory? (so that modifying it faults).
mknyszek commentedon May 5, 2025
That's unfortunately significantly harder than it sounds, especially for small slices.
mknyszek commentedon May 5, 2025
Aaaand we've been down this rabbit hole before. I'm not surprised. See https://docs.google.com/document/d/1-NzIYu0qnnsshMBpMPmuO21qd8unlimHgKjRD9qwp2A/edit?tab=t.0 which I found from #20443 (comment). Some of this is less relevant in our modern type-parametrized world, and the original proposal was also suggesting that
[]T -> immutable []Twould not force a clone operation. I would argue that it should.apparentlymart commentedon May 5, 2025
Due to the general lack of "reference to const data" types in Go today, it's already relatively common to return mutable-in-practice slices with a documented convention or requirement that the caller mustn't modify them, and also relatively common to make just-in-case copies to ensure encapsulation. The ecosystem seems pretty divided on which to use in which situations, but the fact that both already exist seems to suggest that both are defensible, even if not ideal for all applications.
If we do decide that
Valueshould return a slice over the same backing array to all callers with just a documentation warning not to modify it then I thinkValueshould ensure that there's no excess capacity in what it returns, so that uses ofappendon the result will always definitely allocate a new slice. (I expect that's a natural consequence of copying to a new backing array, but I'm suggesting to document it as a guarantee.)(I think I actually prefer the later-discussed formulation where
unique.Sliceis a type, similar to howsync.Mapis a type rather than just a set of conventions for working with a plain map, but the above comment is intended for the original proposal.)EdSchouten commentedon Nov 28, 2025
Another option would be to have some kind of
FrozenSlice[T]type that is comparable. Just like howstringcould be thought of as a frozen[]byte. Then you could just callunique.Make(FrozenSlice[T](...)).