-
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: reflect: rename ArenaNew as NewFrom #56234
Comments
I'm personally not a huge fan of "From" because it feels too generic of a namespace to take for arenas, especially given this scenario:
Such an interface would only ever come after multiple such allocators exist, I think. I also don't think there are any other ideas currently being seriously considered for memory allocators. What about a suffix like "InArena"? It's a bit verbose, but it gets the point across. |
Alternatively, we could try and go for an allocator interface: type Allocator interface {
New(p any) // where p is a **T
MakeSlice(p any, int, int) // where p is a *[]T
GrowSlice(p any, int) // where p is a *[]T
MakeMap(p any, int) // where p is a *map[K]V
MakeChan(p any, int) // where p is a *chan T
} However, I'm not sure how much performance cost will be lost going through a virtual method call in the interface. |
Actually, if we declared those methods on For example, instead of: va.Set(reflect.NewFrom(va.Type().Elem(), a)) we could just do: a.New(va.Addr().Interface()) Instead of: va.Set(reflect.MakeMapFrom(t, a)) we could just do: a.MakeMap(va.Addr().Interface(), 0) |
I understood that this issue was about an extension to the arena proposal, rather than an extension to the |
CL 423361 adds
reflect.ArenaNew
to allocate a newValue
from an arena.As a form of bikeshedding, I think the "Arena" prefix is unfortunate since it means related functionality will not sort well in godoc. Perhaps we should establish a convention for a suffix that indicates the use of an
*arena.Arena
?I propose using
From
as a suffix to indicate that the allocation was made from an allocator:as it would coexist nicely with the
New
function with anAt
suffix.You can imagine other related functionality added in the future:
MakeSliceFrom
needs to allocate the slice header (not just the backing array) somewhere. I believe the correct behavior is to also allocate the header from the arena.With the acceptance of #48000, we could also add:
Notably missing from the list is the arena variants of
Append
andAppendSlice
. If we put thearena.Arena
argument at the end, it would not work well withAppend
, which has a variadic argument. Also, theFrom
suffix seems ambiguous whether it is referring to the arena or the source slice. I think this omission is okay since the implementations for those two functions are relatively easy to implement yourself given the existence ofValue.GrowFrom
.Other considerations: Having these functions take in an
*arena.Arena
make it such that supporting a different allocator in the future would require expanding the API surface again. Is there an interface we could accept? Would there ever be a different allocator implementation?\cc @danscales @mknyszek @cherrymui
The text was updated successfully, but these errors were encountered: