-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
[BEAM-3612] Closurize method invocations #7161
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one or more | ||
// contributor license agreements. See the NOTICE file distributed with | ||
// this work for additional information regarding copyright ownership. | ||
// The ASF licenses this file to You under the Apache License, Version 2.0 | ||
// (the "License"); you may not use this file except in compliance with | ||
// the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package reflectx | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
"sync" | ||
|
||
"github.com/apache/beam/sdks/go/pkg/beam/log" | ||
) | ||
|
||
var ( | ||
structFuncs = make(map[string]func(interface{}) map[string]Func) | ||
structFuncsMu sync.Mutex | ||
) | ||
|
||
// RegisterStructWrapper takes in the reflect.Type of a structural DoFn, and | ||
// a wrapping function that will take an instance of that struct type and | ||
// produce a map of method names to of closured Funcs that call the method | ||
// on the instance of the struct. | ||
// | ||
// The goal is to avoid the implicit reflective method invocation penalty | ||
// that occurs when passing a method through the reflect package. | ||
func RegisterStructWrapper(t reflect.Type, wrapper func(interface{}) map[string]Func) { | ||
structFuncsMu.Lock() | ||
defer structFuncsMu.Unlock() | ||
|
||
if t.Kind() != reflect.Struct { | ||
log.Fatalf(context.Background(), "RegisterStructWrapper for %v should be a struct type, but was %v", t, t.Kind()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this the extent of validity checking that can be performed on the struct? No required members? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, we could be more strict here. The generator does lifecycle method filtering so only the life cycle methods have code generated for them. Beam at pipeline construction time should be doing that required method checking at construction time anyway. Short of always using the tool, the development flow would be using the reflection based paths, so one hopes that beam catches those errors then. Finally, I'd rather have any more dedicated checks in a vetting runner of some kind, to catch typos and the like, rather than in the code generation code. |
||
} | ||
|
||
key := t.String() | ||
if _, exists := funcs[key]; exists { | ||
log.Warnf(context.Background(), "StructWrapper for %v already registered. Overwriting.", key) | ||
} | ||
structFuncs[key] = wrapper | ||
} | ||
|
||
// WrapMethods takes in a struct value as an interface, and returns a map of | ||
// method names to Funcs of those methods wrapped in a closure for the struct instance. | ||
func WrapMethods(fn interface{}) (map[string]Func, bool) { | ||
return wrapMethodsKeyed(reflect.TypeOf(fn), fn) | ||
} | ||
|
||
// WrapMethodsKeyed takes in a struct value as an interface | ||
func wrapMethodsKeyed(t reflect.Type, fn interface{}) (map[string]Func, bool) { | ||
structFuncsMu.Lock() | ||
defer structFuncsMu.Unlock() | ||
// Registering happens on the value, not the proto type. | ||
if t.Kind() == reflect.Ptr { | ||
t = t.Elem() | ||
} | ||
key := t.String() | ||
if f, exists := structFuncs[key]; exists { | ||
log.Debugf(context.Background(), "EXTRACTING StructWrapper for %v", key) | ||
return f(fn), true | ||
} | ||
return nil, false | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... method names to closured Funcs...