(b *testing.B)
| 4842 | } |
| 4843 | |
| 4844 | func BenchmarkDynamicDispatch(b *testing.B) { |
| 4845 | env := testEnv(b, |
| 4846 | HomogeneousAggregateLiterals(), |
| 4847 | Function("first", |
| 4848 | MemberOverload("first_list_int", []*Type{ListType(IntType)}, IntType, |
| 4849 | UnaryBinding(func(list ref.Val) ref.Val { |
| 4850 | l := list.(traits.Lister) |
| 4851 | if l.Size() == types.IntZero { |
| 4852 | return types.IntZero |
| 4853 | } |
| 4854 | return l.Get(types.IntZero) |
| 4855 | }), |
| 4856 | ), |
| 4857 | MemberOverload("first_list_double", []*Type{ListType(DoubleType)}, DoubleType, |
| 4858 | UnaryBinding(func(list ref.Val) ref.Val { |
| 4859 | l := list.(traits.Lister) |
| 4860 | if l.Size() == types.IntZero { |
| 4861 | return types.Double(0.0) |
| 4862 | } |
| 4863 | return l.Get(types.IntZero) |
| 4864 | }), |
| 4865 | ), |
| 4866 | MemberOverload("first_list_string", []*Type{ListType(StringType)}, StringType, |
| 4867 | UnaryBinding(func(list ref.Val) ref.Val { |
| 4868 | l := list.(traits.Lister) |
| 4869 | if l.Size() == types.IntZero { |
| 4870 | return types.String("") |
| 4871 | } |
| 4872 | return l.Get(types.IntZero) |
| 4873 | }), |
| 4874 | ), |
| 4875 | MemberOverload("first_list_list_string", []*Type{ListType(ListType(StringType))}, ListType(StringType), |
| 4876 | UnaryBinding(func(list ref.Val) ref.Val { |
| 4877 | l := list.(traits.Lister) |
| 4878 | if l.Size() == types.IntZero { |
| 4879 | return types.DefaultTypeAdapter.NativeToValue([]string{}) |
| 4880 | } |
| 4881 | return l.Get(types.IntZero) |
| 4882 | }), |
| 4883 | ), |
| 4884 | ), |
| 4885 | ) |
| 4886 | prg := compile(b, env, ` |
| 4887 | [].first() == 0 |
| 4888 | && [1, 2].first() == 1 |
| 4889 | && [1.0, 2.0].first() == 1.0 |
| 4890 | && ["hello", "world"].first() == "hello" |
| 4891 | && [["hello"], ["world", "!"]].first().first() == "hello"`) |
| 4892 | prgDyn := compile(b, env, ` |
| 4893 | dyn([]).first() == 0 |
| 4894 | && dyn([1, 2]).first() == 1 |
| 4895 | && dyn([1.0, 2.0]).first() == 1.0 |
| 4896 | && dyn(["hello", "world"]).first() == "hello" |
| 4897 | && dyn([["hello"], ["world", "!"]]).first().first() == "hello"`) |
| 4898 | b.ResetTimer() |
| 4899 | b.Run("DirectDispatch", func(b *testing.B) { |
| 4900 | for i := 0; i < b.N; i++ { |
| 4901 | prg.Eval(NoVars()) |
nothing calls this directly
no test coverage detected