Merge "Optimize returning the zero value from provider APIs" into main am: 321aad0a8c am: 162690bb2a

Original change: https://android-review.googlesource.com/c/platform/build/blueprint/+/2876680

Change-Id: Ibacede1bbe35ab746ed15b05dfbbabcba658a18a
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Treehugger Robot 2024-01-02 21:13:33 +00:00 committed by Automerger Merge Worker
commit fb95058b94
3 changed files with 9 additions and 33 deletions

View file

@ -4036,18 +4036,12 @@ func (c *Context) ModuleType(logicModule Module) string {
}
// ModuleProvider returns the value, if any, for the provider for a module. If the value for the
// provider was not set it returns the zero value of the type of the provider, which means the
// return value can always be type-asserted to the type of the provider. The return value should
// always be considered read-only. It panics if called before the appropriate mutator or
// GenerateBuildActions pass for the provider on the module. The value returned may be a deep
// copy of the value originally passed to SetProvider.
// provider was not set it returns nil and false. The return value should always be considered read-only.
// It panics if called before the appropriate mutator or GenerateBuildActions pass for the provider on the
// module. The value returned may be a deep copy of the value originally passed to SetProvider.
func (c *Context) ModuleProvider(logicModule Module, provider AnyProviderKey) (any, bool) {
module := c.moduleInfo[logicModule]
value, ok := c.provider(module, provider.provider())
if value == nil {
value = provider.provider().zero
}
return value, ok
return c.provider(module, provider.provider())
}
func (c *Context) BlueprintFile(logicModule Module) string {

View file

@ -334,16 +334,14 @@ type BaseModuleContext interface {
OtherModuleReverseDependencyVariantExists(name string) bool
// OtherModuleProvider returns the value for a provider for the given module. If the value is
// not set it returns the zero value of the type of the provider, so the return value can always
// be type asserted to the type of the provider. The value returned may be a deep copy of the
// value originally passed to SetProvider.
// not set it returns nil and false. The value returned may be a deep copy of the value originally
// passed to SetProvider.
//
// This method shouldn't be used directly, prefer the type-safe android.OtherModuleProvider instead.
OtherModuleProvider(m Module, provider AnyProviderKey) (any, bool)
// Provider returns the value for a provider for the current module. If the value is
// not set it returns the zero value of the type of the provider, so the return value can always
// be type asserted to the type of the provider. It panics if called before the appropriate
// not set it returns nil and false. It panics if called before the appropriate
// mutator or GenerateBuildActions pass for the provider. The value returned may be a deep
// copy of the value originally passed to SetProvider.
//
@ -611,24 +609,11 @@ func (m *baseModuleContext) OtherModuleReverseDependencyVariantExists(name strin
func (m *baseModuleContext) OtherModuleProvider(logicModule Module, provider AnyProviderKey) (any, bool) {
module := m.context.moduleInfo[logicModule]
value, ok := m.context.provider(module, provider.provider())
if value == nil {
value = provider.provider().zero
}
return value, ok
return m.context.provider(module, provider.provider())
}
func (m *baseModuleContext) Provider(provider AnyProviderKey) (any, bool) {
value, ok := m.context.provider(m.module, provider.provider())
if value == nil {
value = provider.provider().zero
}
return value, ok
}
func (m *baseModuleContext) HasProvider(provider AnyProviderKey) bool {
_, ok := m.context.provider(m.module, provider.provider())
return ok
return m.context.provider(m.module, provider.provider())
}
func (m *baseModuleContext) SetProvider(provider AnyProviderKey, value interface{}) {

View file

@ -48,7 +48,6 @@ type typedProviderKey[K any] struct {
type providerKey struct {
id int
typ string
zero any
mutator string
}
@ -84,7 +83,6 @@ func NewProvider[K any]() ProviderKey[K] {
func NewMutatorProvider[K any](mutator string) ProviderKey[K] {
checkCalledFromInit()
zero := *new(K)
typ := fmt.Sprintf("%T", *new(K))
provider := ProviderKey[K]{
@ -92,7 +90,6 @@ func NewMutatorProvider[K any](mutator string) ProviderKey[K] {
providerKey: providerKey{
id: len(providerRegistry),
typ: typ,
zero: zero,
mutator: mutator,
},
},