Skip to content

Commit

Permalink
sql/sem: remove EvalContext.getTmpDec
Browse files Browse the repository at this point in the history
`apd.Decimal` can now be entirely stack allocated during arithmetic, so
there's no longer any need for this.

With cockroachdb/apd#104, this does not introduce
any new heap allocations:
```
➜ (cd pkg/sql/sem/tree && goescape . | grep moved | wc -l)
     328
```
  • Loading branch information
nvanbenschoten committed Jan 8, 2022
1 parent 4e71fca commit 851bdcd
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 13 deletions.
8 changes: 4 additions & 4 deletions pkg/sql/sem/tree/cast.go
Original file line number Diff line number Diff line change
Expand Up @@ -2755,12 +2755,12 @@ func performCastWithoutPrecisionTruncation(
case *DTime:
return NewDInterval(duration.MakeDuration(int64(*v)*1000, 0, 0), itm), nil
case *DDecimal:
d := ctx.getTmpDec()
var d apd.Decimal
dnanos := v.Decimal
dnanos.Exponent += 9
// We need HighPrecisionCtx because duration values can contain
// upward of 35 decimal digits and DecimalCtx only provides 25.
_, err := HighPrecisionCtx.Quantize(d, &dnanos, 0)
_, err := HighPrecisionCtx.Quantize(&d, &dnanos, 0)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -2921,8 +2921,8 @@ func performIntToOidCast(ctx *EvalContext, t *types.T, v DInt) (Datum, error) {
}

func roundDecimalToInt(ctx *EvalContext, d *apd.Decimal) (int64, error) {
newD := ctx.getTmpDec()
if _, err := DecimalCtx.RoundToIntegralValue(newD, d); err != nil {
var newD apd.Decimal
if _, err := DecimalCtx.RoundToIntegralValue(&newD, d); err != nil {
return 0, err
}
i, err := newD.Int64()
Expand Down
6 changes: 3 additions & 3 deletions pkg/sql/sem/tree/datum.go
Original file line number Diff line number Diff line change
Expand Up @@ -1057,10 +1057,10 @@ func (d *DDecimal) CompareError(ctx *EvalContext, other Datum) (int, error) {
// NULL is less than any non-NULL value.
return 1, nil
}
v := ctx.getTmpDec()
var v apd.Decimal
switch t := UnwrapDatum(ctx, other).(type) {
case *DDecimal:
v = &t.Decimal
v.Set(&t.Decimal)
case *DInt:
v.SetInt64(int64(*t))
case *DFloat:
Expand All @@ -1070,7 +1070,7 @@ func (d *DDecimal) CompareError(ctx *EvalContext, other Datum) (int, error) {
default:
return 0, makeUnsupportedComparisonMessage(d, other)
}
res := CompareDecimals(&d.Decimal, v)
res := CompareDecimals(&d.Decimal, &v)
return res, nil
}

Expand Down
9 changes: 3 additions & 6 deletions pkg/sql/sem/tree/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -1428,10 +1428,11 @@ var BinOps = map[BinaryOperatorSymbol]binOpOverload{
if rInt == 0 {
return nil, ErrDivByZero
}
div := ctx.getTmpDec().SetInt64(int64(rInt))
var div apd.Decimal
div.SetInt64(int64(rInt))
dd := &DDecimal{}
dd.SetInt64(int64(MustBeDInt(left)))
_, err := DecimalCtx.Quo(&dd.Decimal, &dd.Decimal, div)
_, err := DecimalCtx.Quo(&dd.Decimal, &dd.Decimal, &div)
return dd, err
},
Volatility: VolatilityImmutable,
Expand Down Expand Up @@ -3930,10 +3931,6 @@ func (ctx *EvalContext) Ctx() context.Context {
return ctx.Context
}

func (ctx *EvalContext) getTmpDec() *apd.Decimal {
return &ctx.tmpDec
}

// Eval implements the TypedExpr interface.
func (expr *AndExpr) Eval(ctx *EvalContext) (Datum, error) {
left, err := expr.Left.(TypedExpr).Eval(ctx)
Expand Down

0 comments on commit 851bdcd

Please sign in to comment.