From 92de6ea3407ad35ad816baf9278ae9dc470a3d63 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Sun, 12 May 2024 10:20:39 +0200 Subject: [PATCH] app/vmselect: use strings.EqualFold instead of strings.ToLower where appropriate Strings.EqualFold doesn't allocate memory contrary to strings.ToLower if the input string contains uppercase chars --- app/vmselect/graphiteql/parser.go | 4 ++-- app/vmselect/promql/binary_op.go | 4 ++-- app/vmselect/promql/eval.go | 10 +++++----- app/vmselect/promql/exec.go | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/app/vmselect/graphiteql/parser.go b/app/vmselect/graphiteql/parser.go index b827c0f0e1..a56bf74e65 100644 --- a/app/vmselect/graphiteql/parser.go +++ b/app/vmselect/graphiteql/parser.go @@ -213,11 +213,11 @@ func (p *parser) parseMetricExprOrFuncCall() (Expr, error) { // Metric epxression or bool expression or None. if isBool(ident) { be := &BoolExpr{ - B: strings.ToLower(ident) == "true", + B: strings.EqualFold(ident, "true"), } return be, nil } - if strings.ToLower(ident) == "none" { + if strings.EqualFold(ident, "none") { nne := &NoneExpr{} return nne, nil } diff --git a/app/vmselect/promql/binary_op.go b/app/vmselect/promql/binary_op.go index 0cf2f75280..ac347e5048 100644 --- a/app/vmselect/promql/binary_op.go +++ b/app/vmselect/promql/binary_op.go @@ -112,7 +112,7 @@ func binaryOpNeqFunc(bfa *binaryOpFuncArg) ([]*timeseries, error) { } func isUnionFunc(e metricsql.Expr) bool { - if fe, ok := e.(*metricsql.FuncExpr); ok && (fe.Name == "" || strings.ToLower(fe.Name) == "union") { + if fe, ok := e.(*metricsql.FuncExpr); ok && (fe.Name == "" || strings.EqualFold(fe.Name, "union")) { return true } return false @@ -303,7 +303,7 @@ func ensureSingleTimeseries(side string, be *metricsql.BinaryOpExpr, tss []*time func groupJoin(singleTimeseriesSide string, be *metricsql.BinaryOpExpr, rvsLeft, rvsRight, tssLeft, tssRight []*timeseries) ([]*timeseries, []*timeseries, error) { joinTags := be.JoinModifier.Args var skipTags []string - if strings.ToLower(be.GroupModifier.Op) == "on" { + if strings.EqualFold(be.GroupModifier.Op, "on") { skipTags = be.GroupModifier.Args } joinPrefix := "" diff --git a/app/vmselect/promql/eval.go b/app/vmselect/promql/eval.go index 6113254731..2f8b32cdac 100644 --- a/app/vmselect/promql/eval.go +++ b/app/vmselect/promql/eval.go @@ -542,7 +542,7 @@ func execBinaryOpArgs(qt *querytracer.Tracer, ec *EvalConfig, exprFirst, exprSec if err != nil { return nil, nil, err } - if len(tssFirst) == 0 && strings.ToLower(be.Op) != "or" { + if len(tssFirst) == 0 && !strings.EqualFold(be.Op, "or") { // Fast path: there is no sense in executing the exprSecond when exprFirst returns an empty result, // since the "exprFirst op exprSecond" would return an empty result in any case. // See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/3349 @@ -1168,7 +1168,7 @@ func evalInstantRollup(qt *querytracer.Tracer, ec *EvalConfig, funcName string, return evalExpr(qt, ec, be) case "rate": if iafc != nil { - if strings.ToLower(iafc.ae.Name) != "sum" { + if !strings.EqualFold(iafc.ae.Name, "sum") { qt.Printf("do not apply instant rollup optimization for incremental aggregate %s()", iafc.ae.Name) return evalAt(qt, timestamp, window) } @@ -1214,7 +1214,7 @@ func evalInstantRollup(qt *querytracer.Tracer, ec *EvalConfig, funcName string, return evalExpr(qt, ec, be) case "max_over_time": if iafc != nil { - if strings.ToLower(iafc.ae.Name) != "max" { + if !strings.EqualFold(iafc.ae.Name, "max") { qt.Printf("do not apply instant rollup optimization for non-max incremental aggregate %s()", iafc.ae.Name) return evalAt(qt, timestamp, window) } @@ -1276,7 +1276,7 @@ func evalInstantRollup(qt *querytracer.Tracer, ec *EvalConfig, funcName string, return tss, nil case "min_over_time": if iafc != nil { - if strings.ToLower(iafc.ae.Name) != "min" { + if !strings.EqualFold(iafc.ae.Name, "min") { qt.Printf("do not apply instant rollup optimization for non-min incremental aggregate %s()", iafc.ae.Name) return evalAt(qt, timestamp, window) } @@ -1345,7 +1345,7 @@ func evalInstantRollup(qt *querytracer.Tracer, ec *EvalConfig, funcName string, "increase", "increase_pure", "sum_over_time": - if iafc != nil && strings.ToLower(iafc.ae.Name) != "sum" { + if iafc != nil && !strings.EqualFold(iafc.ae.Name, "sum") { qt.Printf("do not apply instant rollup optimization for non-sum incremental aggregate %s()", iafc.ae.Name) return evalAt(qt, timestamp, window) } diff --git a/app/vmselect/promql/exec.go b/app/vmselect/promql/exec.go index 5414387ab5..c38de1293c 100644 --- a/app/vmselect/promql/exec.go +++ b/app/vmselect/promql/exec.go @@ -139,7 +139,7 @@ func maySortResults(e metricsql.Expr) bool { return false } case *metricsql.BinaryOpExpr: - if strings.ToLower(v.Op) == "or" { + if strings.EqualFold(v.Op, "or") { // Do not sort results for `a or b` in the same way as Prometheus does. // See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/4763 return false