mirror of
https://github.com/protocolbuffers/protobuf-go.git
synced 2025-01-17 01:12:51 +00:00
3903b218df
Remove List from KnownFields, UnknownFields, ExtensionFieldTypes, and Map. Rationale: * Each of those interfaces already have a Range method, which provides a superset of the functionality of List. Furthermore, Range is more expressive as it allows you to terminate iteration and provides both keys and values. * List must always allocate a slice and populates it. * Range is allocation free in some cases. For example, if you simply wanted to iterate over all the populated fields to clear them, there is no need for a closure, so a static version of the function can be directly referenced (i.e., there is no need to create a stub function header that references the closed-over variables). * In the cases where a closure is needed, the allocation cost is O(1) since there are a finite number of variables being closed over. * In highly performance sensitive cases, the closured function could close over a struct, such that the function and struct are stored in a sync.Pool when not in use. For example: type MapLister struct { Entries []struct{K MapKey; V Value} f func(MapKey, Value) true } func (m *MapLister) Ranger() func(MapKey, Value) bool { if m.f != nil { m.f = func(k MapKey, v Value) bool { m.Entries = append(m.Entries, ...) return true } } m.Entries = m.Entries[:0] return m.f } The main benefit of List is the ease of use: for _, num := range knownFields.List() { ... } as opposed to: knownFields.Range(func(n FieldNumber, v Value) bool { ... return true }) However, this is a marginal benefit. Thus, remove List as it mostly inferior to Range. Change-Id: I25586c6ea07a4706072ba06b1cf25cb6efb5e8a7 Reviewed-on: https://go-review.googlesource.com/c/142888 Reviewed-by: Damien Neil <dneil@google.com> |
||
---|---|---|
.. | ||
protoreflect | ||
protoregistry | ||
prototype |