(b *testing.B)
| 3706 | } |
| 3707 | |
| 3708 | func BenchmarkDynamicDispatch(b *testing.B) { |
| 3709 | env := testEnv(b, |
| 3710 | HomogeneousAggregateLiterals(), |
| 3711 | Function("first", |
| 3712 | MemberOverload("first_list_int", []*Type{ListType(IntType)}, IntType, |
| 3713 | UnaryBinding(func(list ref.Val) ref.Val { |
| 3714 | l := list.(traits.Lister) |
| 3715 | if l.Size() == types.IntZero { |
| 3716 | return types.IntZero |
| 3717 | } |
| 3718 | return l.Get(types.IntZero) |
| 3719 | }), |
| 3720 | ), |
| 3721 | MemberOverload("first_list_double", []*Type{ListType(DoubleType)}, DoubleType, |
| 3722 | UnaryBinding(func(list ref.Val) ref.Val { |
| 3723 | l := list.(traits.Lister) |
| 3724 | if l.Size() == types.IntZero { |
| 3725 | return types.Double(0.0) |
| 3726 | } |
| 3727 | return l.Get(types.IntZero) |
| 3728 | }), |
| 3729 | ), |
| 3730 | MemberOverload("first_list_string", []*Type{ListType(StringType)}, StringType, |
| 3731 | UnaryBinding(func(list ref.Val) ref.Val { |
| 3732 | l := list.(traits.Lister) |
| 3733 | if l.Size() == types.IntZero { |
| 3734 | return types.String("") |
| 3735 | } |
| 3736 | return l.Get(types.IntZero) |
| 3737 | }), |
| 3738 | ), |
| 3739 | MemberOverload("first_list_list_string", []*Type{ListType(ListType(StringType))}, ListType(StringType), |
| 3740 | UnaryBinding(func(list ref.Val) ref.Val { |
| 3741 | l := list.(traits.Lister) |
| 3742 | if l.Size() == types.IntZero { |
| 3743 | return types.DefaultTypeAdapter.NativeToValue([]string{}) |
| 3744 | } |
| 3745 | return l.Get(types.IntZero) |
| 3746 | }), |
| 3747 | ), |
| 3748 | ), |
| 3749 | ) |
| 3750 | prg := compile(b, env, ` |
| 3751 | [].first() == 0 |
| 3752 | && [1, 2].first() == 1 |
| 3753 | && [1.0, 2.0].first() == 1.0 |
| 3754 | && ["hello", "world"].first() == "hello" |
| 3755 | && [["hello"], ["world", "!"]].first().first() == "hello"`) |
| 3756 | prgDyn := compile(b, env, ` |
| 3757 | dyn([]).first() == 0 |
| 3758 | && dyn([1, 2]).first() == 1 |
| 3759 | && dyn([1.0, 2.0]).first() == 1.0 |
| 3760 | && dyn(["hello", "world"]).first() == "hello" |
| 3761 | && dyn([["hello"], ["world", "!"]]).first().first() == "hello"`) |
| 3762 | b.ResetTimer() |
| 3763 | b.Run("DirectDispatch", func(b *testing.B) { |
| 3764 | for i := 0; i < b.N; i++ { |
| 3765 | prg.Eval(NoVars()) |
nothing calls this directly
no test coverage detected