damccorm commented on code in PR #17429:
URL: https://github.com/apache/beam/pull/17429#discussion_r864441783


##########
sdks/go/pkg/beam/registration/registration.tmpl:
##########
@@ -0,0 +1,263 @@
+{{define "BuildWrapper_StartFinishBundle"}}
+{{$lowerName := "unknown"}}{{$upperName := "unknown"}}{{if (eq .func 
"startBundle")}}{{$lowerName = "startBundle"}}{{$upperName = 
"StartBundle"}}{{end}}{{if (eq .func "finishBundle")}}{{$lowerName = 
"finishBundle"}}{{$upperName = "FinishBundle"}}{{end}}{{$startFinishBundleMaxIn 
:= .startFinishBundleMaxIn}}{{$processElementMaxIn := .processElementMaxIn}}
+{{range $funcIn := upto $startFinishBundleMaxIn}}{{range $funcOut := upto 2}}
+func 
register{{$upperName}}{{$funcIn}}x{{$funcOut}}FuncAndMakeStructWrapper{{(genericTypingRepresentation
 $funcIn $funcOut true)}}() func(interface{}) reflectx.Func {
+    reflectx.RegisterFunc(reflect.TypeOf((*func({{range $in := upto 
$funcIn}}{{if $in}}, {{end}}I{{$in}}{{end}}){{if $funcOut}} 
R0{{end}})(nil)).Elem(), func(fn interface{}) reflectx.Func {
+        return &caller{{$funcIn}}x{{$funcOut}}{{(genericTypingRepresentation 
$funcIn $funcOut false)}}{fn: fn.(func({{range $in := upto $funcIn}}{{if $in}}, 
{{end}}I{{$in}}{{end}}){{if $funcOut}} R0{{end}})}
+    })
+
+    return func(fn interface{}) reflectx.Func {
+        return reflectx.MakeFunc(func({{range $in := upto $funcIn}}{{if $in}}, 
{{end}}a{{$in}} I{{$in}}{{end}}){{if $funcOut}} R0{{end}} {
+            {{if $funcOut}}return 
{{end}}fn.({{$lowerName}}{{$funcIn}}x{{$funcOut}}{{(genericTypingRepresentation 
$funcIn $funcOut false)}}).{{$upperName}}({{range $in := upto $funcIn}}{{if 
$in}}, {{end}}a{{$in}}{{end}})
+        })
+    }
+}
+{{end}}{{end}}
+{{$numParams := $processElementMaxIn}}
+func build{{$upperName}}Wrapper{{(genericTypingRepresentation $numParams 0 
true)}}(doFn interface{}) func(interface{}) reflectx.Func {
+    {{$lowerName}}In := -1
+       {{$lowerName}}Out := -1
+       {{$lowerName}}Method := 
reflect.ValueOf(doFn).MethodByName("{{$upperName}}")
+       if {{$lowerName}}Method.IsValid() {
+               {{$lowerName}}In = {{$lowerName}}Method.Type().NumIn()
+               {{$lowerName}}Out = {{$lowerName}}Method.Type().NumOut()
+           switch {
+{{range $funcIn := upto $startFinishBundleMaxIn}}
+    case {{$lowerName}}In == {{$funcIn}}:
+            switch { {{range $funcOut := upto 2}}{{$possibleCombos := 
(possibleBundleLifecycleParameterCombos $funcIn $numParams)}}{{if 
$possibleCombos}}
+            case {{$lowerName}}Out == {{$funcOut}}:
+{{$first := true}}{{range $funcCombo := $possibleCombos}}{{if $first}}{{$first 
= false}}                {{else}} else {{end}}if _, ok := 
doFn.({{$lowerName}}{{$funcIn}}x{{$funcOut}}{{if (or $funcIn 
$funcOut)}}[{{(join $funcCombo ", ")}}{{if $funcOut}}{{if $funcIn}}, 
{{end}}error{{end}}]{{end}}); ok {
+                    return 
register{{$upperName}}{{$funcIn}}x{{$funcOut}}FuncAndMakeStructWrapper{{if (or 
$funcIn $funcOut)}}[{{(join $funcCombo ", ")}}{{if $funcOut}}{{if $funcIn}}, 
{{end}}error{{end}}]{{end}}()
+                } {{end}}{{end}}{{end}}
+            default:
+                panic("Invalid signature for {{$upperName}}")
+            }
+{{end}}
+        default:
+            panic("Invalid signature for {{$upperName}}")
+        }
+    }
+    return nil

Review Comment:
   We could get there if either the function doesn't have a StartBundle 
function (or whatever function we're dealing with here), or if we were unable 
to infer the types of the StartBundle function.
   
   Returning nil in the first case is definitely the right thing, I updated to 
panic in the second case. The second should never happen, but I agree its 
better to be explicit



##########
sdks/go/pkg/beam/registration/doc.go:
##########
@@ -0,0 +1,31 @@
+// 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.
+
+//go:generate go install github.com/apache/beam/sdks/v2/go/cmd/specialize
+//go:generate specialize --package=registration --input=registration.tmpl 
--x=data,universals --imports=typex
+//go:generate go fmt
+
+/*
+Package registration contains functions for registering and optimizing your 
DoFn.
+
+This package contains generic registration/optimization function for each 
possible combination of input and output arities in a DoFn's ProcessElement 
function.
+For example, given a DoFn with a ProcessElement function that takes 4 inputs 
and returns 3 outputs, you can call
+register.DoFn4x3[input1 type, input2 type, input3 type, input4 type, output1 
type, output2 type, output3 type](&doFn{}) during pipeline construction. This 
will
+register your DoFn and perform some reflection based optimization to 
significantly speed up execution at runtime.
+
+See DoFn2x1 for a full example.
+*/
+

Review Comment:
   Neat - I did not understand this before



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscr...@beam.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to