(b *testing.B)
| 3812 | } |
| 3813 | |
| 3814 | func BenchmarkDynamicDispatch(b *testing.B) { |
| 3815 | env := testEnv(b, |
| 3816 | HomogeneousAggregateLiterals(), |
| 3817 | Function("first", |
| 3818 | MemberOverload("first_list_int", []*Type{ListType(IntType)}, IntType, |
| 3819 | UnaryBinding(func(list ref.Val) ref.Val { |
| 3820 | l := list.(traits.Lister) |
| 3821 | if l.Size() == types.IntZero { |
| 3822 | return types.IntZero |
| 3823 | } |
| 3824 | return l.Get(types.IntZero) |
| 3825 | }), |
| 3826 | ), |
| 3827 | MemberOverload("first_list_double", []*Type{ListType(DoubleType)}, DoubleType, |
| 3828 | UnaryBinding(func(list ref.Val) ref.Val { |
| 3829 | l := list.(traits.Lister) |
| 3830 | if l.Size() == types.IntZero { |
| 3831 | return types.Double(0.0) |
| 3832 | } |
| 3833 | return l.Get(types.IntZero) |
| 3834 | }), |
| 3835 | ), |
| 3836 | MemberOverload("first_list_string", []*Type{ListType(StringType)}, StringType, |
| 3837 | UnaryBinding(func(list ref.Val) ref.Val { |
| 3838 | l := list.(traits.Lister) |
| 3839 | if l.Size() == types.IntZero { |
| 3840 | return types.String("") |
| 3841 | } |
| 3842 | return l.Get(types.IntZero) |
| 3843 | }), |
| 3844 | ), |
| 3845 | MemberOverload("first_list_list_string", []*Type{ListType(ListType(StringType))}, ListType(StringType), |
| 3846 | UnaryBinding(func(list ref.Val) ref.Val { |
| 3847 | l := list.(traits.Lister) |
| 3848 | if l.Size() == types.IntZero { |
| 3849 | return types.DefaultTypeAdapter.NativeToValue([]string{}) |
| 3850 | } |
| 3851 | return l.Get(types.IntZero) |
| 3852 | }), |
| 3853 | ), |
| 3854 | ), |
| 3855 | ) |
| 3856 | prg := compile(b, env, ` |
| 3857 | [].first() == 0 |
| 3858 | && [1, 2].first() == 1 |
| 3859 | && [1.0, 2.0].first() == 1.0 |
| 3860 | && ["hello", "world"].first() == "hello" |
| 3861 | && [["hello"], ["world", "!"]].first().first() == "hello"`) |
| 3862 | prgDyn := compile(b, env, ` |
| 3863 | dyn([]).first() == 0 |
| 3864 | && dyn([1, 2]).first() == 1 |
| 3865 | && dyn([1.0, 2.0]).first() == 1.0 |
| 3866 | && dyn(["hello", "world"]).first() == "hello" |
| 3867 | && dyn([["hello"], ["world", "!"]]).first().first() == "hello"`) |
| 3868 | b.ResetTimer() |
| 3869 | b.Run("DirectDispatch", func(b *testing.B) { |
| 3870 | for i := 0; i < b.N; i++ { |
| 3871 | prg.Eval(NoVars()) |
nothing calls this directly
no test coverage detected