lostluck commented on code in PR #17429: URL: https://github.com/apache/beam/pull/17429#discussion_r863988306
########## sdks/go/pkg/beam/registration/example_registration_test.go: ########## @@ -0,0 +1,51 @@ +// 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 registration_test + +import ( + "context" + + "github.com/apache/beam/sdks/v2/go/pkg/beam/registration" +) + +type myDoFn struct { +} + +func (fn *myDoFn) ProcessElement(word string, emit func(int)) int { + emit(len(word)) + return len(word) +} + +func (fn *myDoFn) StartBundle(_ context.Context, emit func(int)) { + emit(2) +} + +func (fn *myDoFn) FinishBundle(_ context.Context, emit func(int)) error { + emit(2) + return nil +} + +func (fn *myDoFn) Setup(_ context.Context) error { + return nil +} + +func (fn *myDoFn) Teardown() error { + return nil +} + +func ExampleDoFn2x1() { + registration.DoFn2x1[string, func(int), int](&myDoFn{}) Review Comment: It may be good to add a comment here explaining why myDoFn needs to be called with DoFn2x1 rather than anything else. ########## sdks/go/cmd/specialize/main_test.go: ########## @@ -0,0 +1,146 @@ +// 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 main + +import ( + "fmt" + "testing" +) + +func TestGenericTypingRepresentation(t *testing.T) { + tests := []struct { + in int + out int + includeType bool + representation string + }{ + {0, 0, true, ""}, + {0, 0, false, ""}, + {0, 1, true, "[R0 any]"}, + {0, 1, false, "[R0]"}, + {0, 3, true, "[R0, R1, R2 any]"}, + {0, 3, false, "[R0, R1, R2]"}, + {1, 0, true, "[I0 any]"}, + {1, 0, false, "[I0]"}, + {3, 0, true, "[I0, I1, I2 any]"}, + {3, 0, false, "[I0, I1, I2]"}, + {1, 1, true, "[I0, R0 any]"}, + {1, 1, false, "[I0, R0]"}, + {4, 1, true, "[I0, I1, I2, I3, R0 any]"}, + {4, 1, false, "[I0, I1, I2, I3, R0]"}, + {4, 2, true, "[I0, I1, I2, I3, R0, R1 any]"}, + {4, 2, false, "[I0, I1, I2, I3, R0, R1]"}, + {1, 2, true, "[I0, R0, R1 any]"}, + {1, 2, false, "[I0, R0, R1]"}, + } + + for _, test := range tests { + if got, want := genericTypingRepresentation(test.in, test.out, test.includeType), test.representation; got != want { + t.Errorf("genericTypingRepresentation(%v, %v, %v) = %v, want: %v", test.in, test.out, test.includeType, got, want) + } + } +} + +func TestPossibleBundleLifecycleParameterCombos(t *testing.T) { + tests := []struct { + numIn int + processElementIn int + representation [][]string + }{ + {0, 0, [][]string{[]string{}}}, + {0, 1, [][]string{[]string{}}}, + {1, 0, [][]string{[]string{"context.Context"}, []string{"typex.PaneInfo"}, []string{"[]typex.Window"}, []string{"typex.EventTime"}, []string{"typex.BundleFinalization"}}}, + {1, 1, [][]string{[]string{"I0"}, []string{"context.Context"}, []string{"typex.PaneInfo"}, []string{"[]typex.Window"}, []string{"typex.EventTime"}, []string{"typex.BundleFinalization"}}}, + {2, 1, [][]string{[]string{"context.Context", "I0"}, []string{"context.Context", "typex.PaneInfo"}, []string{"context.Context", "[]typex.Window"}, []string{"context.Context", "typex.EventTime"}, []string{"context.Context", "typex.BundleFinalization"}, + []string{"typex.PaneInfo", "I0"}, []string{"typex.PaneInfo", "[]typex.Window"}, []string{"typex.PaneInfo", "typex.EventTime"}, []string{"typex.PaneInfo", "typex.BundleFinalization"}, + []string{"[]typex.Window", "I0"}, []string{"[]typex.Window", "typex.EventTime"}, []string{"[]typex.Window", "typex.BundleFinalization"}, + []string{"typex.EventTime", "I0"}, []string{"typex.EventTime", "typex.BundleFinalization"}, + []string{"typex.BundleFinalization", "I0"}}}, + {2, 2, [][]string{[]string{"context.Context", "I1"}, []string{"context.Context", "typex.PaneInfo"}, []string{"context.Context", "[]typex.Window"}, []string{"context.Context", "typex.EventTime"}, []string{"context.Context", "typex.BundleFinalization"}, + []string{"typex.PaneInfo", "I1"}, []string{"typex.PaneInfo", "[]typex.Window"}, []string{"typex.PaneInfo", "typex.EventTime"}, []string{"typex.PaneInfo", "typex.BundleFinalization"}, + []string{"[]typex.Window", "I1"}, []string{"[]typex.Window", "typex.EventTime"}, []string{"[]typex.Window", "typex.BundleFinalization"}, + []string{"typex.EventTime", "I1"}, []string{"typex.EventTime", "typex.BundleFinalization"}, + []string{"typex.BundleFinalization", "I1"}, + []string{"I0", "I1"}}}, + {2, 3, [][]string{[]string{"context.Context", "I2"}, []string{"context.Context", "typex.PaneInfo"}, []string{"context.Context", "[]typex.Window"}, []string{"context.Context", "typex.EventTime"}, []string{"context.Context", "typex.BundleFinalization"}, + []string{"typex.PaneInfo", "I2"}, []string{"typex.PaneInfo", "[]typex.Window"}, []string{"typex.PaneInfo", "typex.EventTime"}, []string{"typex.PaneInfo", "typex.BundleFinalization"}, + []string{"[]typex.Window", "I2"}, []string{"[]typex.Window", "typex.EventTime"}, []string{"[]typex.Window", "typex.BundleFinalization"}, + []string{"typex.EventTime", "I2"}, []string{"typex.EventTime", "typex.BundleFinalization"}, + []string{"typex.BundleFinalization", "I2"}, + []string{"I1", "I2"}}}, + } + + for _, test := range tests { + got, want := possibleBundleLifecycleParameterCombos(test.numIn, test.processElementIn), test.representation + assertRepresentationEquals(t, got, want, fmt.Sprintf("possibleBundleLifecycleParameterCombos(%v, %v)", test.numIn, test.processElementIn)) + } +} + +func TestMakeStructRegisterEntry(t *testing.T) { + tests := []struct { + structName string + functionName string + ins []string + outs []string + entry string + }{ + {"StartBundle", "startBundle", []string{}, []string{}, "reflectx.MakeFunc(func() { fn.(StartBundle0x0).startBundle() })"}, + {"StartBundle", "startBundle", []string{"context.Context"}, []string{}, "reflectx.MakeFunc(func(a0 context.Context) { fn.(StartBundle1x0[context.Context]).startBundle(a0) })"}, + {"StartBundle", "startBundle", []string{"context.Context", "I0"}, []string{}, "reflectx.MakeFunc(func(a0 context.Context, a1 I0) { fn.(StartBundle2x0[context.Context, I0]).startBundle(a0, a1) })"}, + {"FinishBundle", "finishBundle", []string{}, []string{"[]typex.Window"}, "reflectx.MakeFunc(func() ([]typex.Window){ return fn.(FinishBundle0x1[[]typex.Window]).finishBundle() })"}, + {"FinishBundle", "finishBundle", []string{}, []string{"[]typex.Window", "R0"}, "reflectx.MakeFunc(func() ([]typex.Window, R0){ return fn.(FinishBundle0x2[[]typex.Window, R0]).finishBundle() })"}, + {"FinishBundle", "finishBundle", []string{"I0"}, []string{"[]typex.Window", "R0"}, "reflectx.MakeFunc(func(a0 I0) ([]typex.Window, R0){ return fn.(FinishBundle1x2[I0, []typex.Window, R0]).finishBundle(a0) })"}, + {"FinishBundle", "finishBundle", []string{"context.Context", "I0"}, []string{"[]typex.Window", "R0"}, "reflectx.MakeFunc(func(a0 context.Context, a1 I0) ([]typex.Window, R0){ return fn.(FinishBundle2x2[context.Context, I0, []typex.Window, R0]).finishBundle(a0, a1) })"}, + } + + for _, test := range tests { + if got, want := makeStructRegisterEntry(test.structName, test.functionName, test.ins, test.outs), test.entry; got != want { + t.Errorf("makeStructRegisterEntry(\"%v\", \"%v\", %v, %v) = %v, want: %v", test.structName, test.functionName, test.ins, test.outs, got, want) + } + } +} + +func assertRepresentationEquals(t *testing.T, got [][]string, want [][]string, functionInvocation string) { Review Comment: Add a call to t.Helper(), so the failure is ascribed to the call site and not in this function. Consider instead not having the assert function and moving the body into the test, since it looks like you ended up with just the one call site? I'll note that you may also wish to look at the cmp package, which may clean this up a little bit WRT the comparison. https://pkg.go.dev/github.com/google/go-cmp/cmp ########## sdks/go/pkg/beam/registration/registration.go: ########## @@ -0,0 +1,6990 @@ +// File generated by specialize. Do not edit. + +// 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. + +// Code generated from registration.tmpl. DO NOT EDIT. + +package registration + +import ( + "context" + "reflect" + + "github.com/apache/beam/sdks/v2/go/pkg/beam/core/runtime" + "github.com/apache/beam/sdks/v2/go/pkg/beam/core/runtime/graphx/schema" + "github.com/apache/beam/sdks/v2/go/pkg/beam/core/typex" + "github.com/apache/beam/sdks/v2/go/pkg/beam/core/util/reflectx" +) + +func registerStartBundle0x0FuncAndMakeStructWrapper() func(interface{}) reflectx.Func { + reflectx.RegisterFunc(reflect.TypeOf((*func())(nil)).Elem(), func(fn interface{}) reflectx.Func { + return &caller0x0{fn: fn.(func())} + }) + + return func(fn interface{}) reflectx.Func { + return reflectx.MakeFunc(func() { + fn.(startBundle0x0).StartBundle() + }) + } +} + +func registerStartBundle0x1FuncAndMakeStructWrapper[R0 any]() func(interface{}) reflectx.Func { Review Comment: This structure makes it clear that the factory structure doesn't even require any parameters passed in, just the types. That's awesome! ########## 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: rm this blank line. The line break prevent associating this comment with the package statement, applies to all Go Doc comments. Floating comments live only in the code, which is why it's important to have a break after the license, but before the comment so the licence doesn't go into the generated doc. ########## 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. Review Comment: Technically the "reflection based" optimization we're doing is "avoiding using reflection entirely", so it feels off to call it that. Maybe `...produces optimized callers for your DoFn to significantly speed up execution at runtime` instead? ########## 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: If I followed this correctly in the generated file, it looks like it might be possible for a dense innermost if/else ladder to return out, and land at this `return nil`. Is this intentional? If so, we should just have a comment just above this saying when the nil return is expected to happen, especially since the switch defaults are to panic. If not, either add a terminal else clause to the inner if/else ladder, or turn it to a switch so it can also have a terminal default. ########## sdks/go/pkg/beam/registration/registration.go: ########## @@ -0,0 +1,6990 @@ +// File generated by specialize. Do not edit. Review Comment: I'm just excited that the new file/diff is now small enough to show up in the diff UI. :D -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
