estimateStringSplitCost estimates cost for string split operations. Split creates a list of substrings, so cost includes both traversal and list allocation proportional to the input size.
(estimator checker.CostEstimator, target *checker.AstNode, args []checker.AstNode)
| 987 | // Split creates a list of substrings, so cost includes both traversal and |
| 988 | // list allocation proportional to the input size. |
| 989 | func estimateStringSplitCost(estimator checker.CostEstimator, target *checker.AstNode, args []checker.AstNode) *checker.CallEstimate { |
| 990 | if target == nil || len(args) < 1 { |
| 991 | return nil |
| 992 | } |
| 993 | targetSize := estimateSize(estimator, *target) |
| 994 | // Traversal cost proportional to input size. |
| 995 | traversalCost := targetSize.Add(fixedSizeEstimate(1)).MultiplyByCostFactor(stringCostFactor) |
| 996 | // Worst case: split("") produces N elements for a string of size N. |
| 997 | resultSize := rangedSizeEstimate(0, targetSize.Max) |
| 998 | // Include list creation base cost plus allocation for each element. |
| 999 | allocationCost := resultSize.MultiplyByCostFactor(1).Add(checker.FixedCostEstimate(common.ListCreateBaseCost)) |
| 1000 | cost := traversalCost.Add(allocationCost).Add(callCostEstimate) |
| 1001 | return callEstimate(cost, &resultSize) |
| 1002 | } |
| 1003 | |
| 1004 | // estimateStringJoinCost estimates cost for string join operations. |
| 1005 | // Join iterates over all list elements and concatenates them, so cost is |
nothing calls this directly
no test coverage detected