(e ast.Expr, function, overloadID string, target *AstNode, args []AstNode, argCosts []CostEstimate)
| 729 | } |
| 730 | |
| 731 | func (c *coster) functionCost(e ast.Expr, function, overloadID string, target *AstNode, args []AstNode, argCosts []CostEstimate) CallEstimate { |
| 732 | argCostSum := func() CostEstimate { |
| 733 | var sum CostEstimate |
| 734 | for _, a := range argCosts { |
| 735 | sum = sum.Add(a) |
| 736 | } |
| 737 | return sum |
| 738 | } |
| 739 | if len(c.overloadEstimators) != 0 { |
| 740 | if estimator, found := c.overloadEstimators[overloadID]; found { |
| 741 | if est := estimator(c.estimator, target, args); est != nil { |
| 742 | callEst := *est |
| 743 | return CallEstimate{CostEstimate: callEst.Add(argCostSum()), ResultSize: est.ResultSize} |
| 744 | } |
| 745 | } |
| 746 | } |
| 747 | if est := c.estimator.EstimateCallCost(function, overloadID, target, args); est != nil { |
| 748 | callEst := *est |
| 749 | return CallEstimate{CostEstimate: callEst.Add(argCostSum()), ResultSize: est.ResultSize} |
| 750 | } |
| 751 | switch overloadID { |
| 752 | // O(n) functions |
| 753 | case overloads.ExtFormatString: |
| 754 | if target != nil { |
| 755 | // ResultSize not calculated because we can't bound the max size. |
| 756 | return CallEstimate{ |
| 757 | CostEstimate: c.sizeOrUnknown(*target).MultiplyByCostFactor(common.StringTraversalCostFactor).Add(argCostSum())} |
| 758 | } |
| 759 | case overloads.StringToBytes: |
| 760 | if len(args) == 1 { |
| 761 | sz := c.sizeOrUnknown(args[0]) |
| 762 | // ResultSize max is when each char converts to 4 bytes. |
| 763 | return CallEstimate{ |
| 764 | CostEstimate: sz.MultiplyByCostFactor(common.StringTraversalCostFactor).Add(argCostSum()), |
| 765 | ResultSize: &SizeEstimate{Min: sz.Min, Max: sz.Max * 4}} |
| 766 | } |
| 767 | case overloads.BytesToString: |
| 768 | if len(args) == 1 { |
| 769 | sz := c.sizeOrUnknown(args[0]) |
| 770 | // ResultSize min is when 4 bytes convert to 1 char. |
| 771 | return CallEstimate{ |
| 772 | CostEstimate: sz.MultiplyByCostFactor(common.StringTraversalCostFactor).Add(argCostSum()), |
| 773 | ResultSize: &SizeEstimate{Min: sz.Min / 4, Max: sz.Max}} |
| 774 | } |
| 775 | case overloads.ExtQuoteString: |
| 776 | if len(args) == 1 { |
| 777 | sz := c.sizeOrUnknown(args[0]) |
| 778 | // ResultSize max is when each char is escaped. 2 quote chars always added. |
| 779 | return CallEstimate{ |
| 780 | CostEstimate: sz.MultiplyByCostFactor(common.StringTraversalCostFactor).Add(argCostSum()), |
| 781 | ResultSize: &SizeEstimate{Min: sz.Min + 2, Max: sz.Max*2 + 2}} |
| 782 | } |
| 783 | case overloads.StartsWithString, overloads.EndsWithString: |
| 784 | if len(args) == 1 { |
| 785 | return CallEstimate{CostEstimate: c.sizeOrUnknown(args[0]).MultiplyByCostFactor(common.StringTraversalCostFactor).Add(argCostSum())} |
| 786 | } |
| 787 | case overloads.InList: |
| 788 | // If a list is composed entirely of constant values this is O(1), but we don't account for that here. |
no test coverage detected