From eeae0fe21cc497c2bc35af50491314fe98c83a00 Mon Sep 17 00:00:00 2001 From: Michael Giagnocavo Date: Tue, 5 Mar 2013 18:10:27 -0700 Subject: [PATCH] Fix scaling issues and provides a small performance boost (5-8%). setScale returns a new BigDecimal; it does not mutate. Thus everywhere we were using it, we were throwing away the scaled value. Add some short-circuit code for setScale (often called), and mark things read-only. Lift "ten" out of functions and into a static readonly variable. --- .../Types/ThirdParty.BigDecimal.cs | 15 +++++++------ .../Types/ThirdParty.BigInteger.cs | 2 +- VoltDB.Data.Client/Types/VoltDecimal.cs | 21 +++++++------------ 3 files changed, 15 insertions(+), 23 deletions(-) diff --git a/VoltDB.Data.Client/Types/ThirdParty.BigDecimal.cs b/VoltDB.Data.Client/Types/ThirdParty.BigDecimal.cs index b4d8330..a42d209 100644 --- a/VoltDB.Data.Client/Types/ThirdParty.BigDecimal.cs +++ b/VoltDB.Data.Client/Types/ThirdParty.BigDecimal.cs @@ -140,10 +140,11 @@ namespace VoltDB.ThirdParty.Math #pragma warning disable 1591 public struct BigDecimal { - private BigInteger biNumber; - private int iScale; - private static string DecimalSeparator = CultureInfo.InvariantCulture.NumberFormat.CurrencyDecimalSeparator; - private static string GroupSeparator = CultureInfo.InvariantCulture.NumberFormat.CurrencyGroupSeparator; + private readonly BigInteger biNumber; + private readonly int iScale; + private static readonly string DecimalSeparator = CultureInfo.InvariantCulture.NumberFormat.CurrencyDecimalSeparator; + private static readonly string GroupSeparator = CultureInfo.InvariantCulture.NumberFormat.CurrencyGroupSeparator; + private static readonly BigInteger ten = new BigInteger(10); public BigDecimal(long num) { @@ -293,7 +294,7 @@ public int Scale public BigDecimal setScale(int val) { - BigInteger ten = new BigInteger(10); + if (val == iScale) return this; BigInteger num = biNumber; if (val > iScale) for (int i = 0; i < val - iScale; i++) @@ -436,7 +437,6 @@ public BigDecimal MovePointRight(int n) return new BigDecimal(biNumber, iScale - n); else { - BigInteger ten = new BigInteger(10); BigInteger num = biNumber; for (int i = 0; i < n - iScale; i++) num *= ten; @@ -491,8 +491,7 @@ public byte[] ToBytes() } public byte[] ToBytes(int scale) { - this.setScale(scale); - return this.ToBytes(); + return this.setScale(scale).ToBytes(); } } } diff --git a/VoltDB.Data.Client/Types/ThirdParty.BigInteger.cs b/VoltDB.Data.Client/Types/ThirdParty.BigInteger.cs index 25d3a0a..21f6705 100644 --- a/VoltDB.Data.Client/Types/ThirdParty.BigInteger.cs +++ b/VoltDB.Data.Client/Types/ThirdParty.BigInteger.cs @@ -167,7 +167,7 @@ public class BigInteger 1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999 }; - private uint[] data = null; // stores bytes from the Big Integer + private readonly uint[] data = null; // stores bytes from the Big Integer public int dataLength; // number of actual chars used diff --git a/VoltDB.Data.Client/Types/VoltDecimal.cs b/VoltDB.Data.Client/Types/VoltDecimal.cs index 2d3e4b7..3ee33dd 100644 --- a/VoltDB.Data.Client/Types/VoltDecimal.cs +++ b/VoltDB.Data.Client/Types/VoltDecimal.cs @@ -117,8 +117,7 @@ public byte[] ToBytes() /// Value used for initialization. public VoltDecimal(BigDecimal num) { - this.Value = num; - this.Value.setScale(FixedScale); + this.Value = num.setScale(FixedScale); ValidOrThrow(this.Value); } @@ -128,8 +127,7 @@ public VoltDecimal(BigDecimal num) /// Value used for initialization. public VoltDecimal(long num) { - this.Value = new BigDecimal(num); - this.Value.setScale(FixedScale); + this.Value = new BigDecimal(num).setScale(FixedScale); ValidOrThrow(this.Value); } @@ -139,8 +137,7 @@ public VoltDecimal(long num) /// Value used for initialization. public VoltDecimal(ulong num) { - this.Value = new BigDecimal(num); - this.Value.setScale(FixedScale); + this.Value = new BigDecimal(num).setScale(FixedScale); ValidOrThrow(this.Value); } @@ -150,8 +147,7 @@ public VoltDecimal(ulong num) /// Value used for initialization. public VoltDecimal(double num) { - this.Value = new BigDecimal(num); - this.Value.setScale(FixedScale); + this.Value = new BigDecimal(num).setScale(FixedScale); ValidOrThrow(this.Value); } @@ -161,8 +157,7 @@ public VoltDecimal(double num) /// Value used for initialization. public VoltDecimal(BigInteger num) { - this.Value = new BigDecimal(num); - this.Value.setScale(FixedScale); + this.Value = new BigDecimal(num).setScale(FixedScale); ValidOrThrow(this.Value); } @@ -173,8 +168,7 @@ public VoltDecimal(BigInteger num) /// Scale of the decimal value to create. public VoltDecimal(BigInteger num, int scale) { - this.Value = new BigDecimal(num, scale); - this.Value.setScale(FixedScale); + this.Value = new BigDecimal(num, scale).setScale(FixedScale); ValidOrThrow(this.Value); } @@ -184,8 +178,7 @@ public VoltDecimal(BigInteger num, int scale) /// Value used for initialization. public VoltDecimal(string num) { - this.Value = new BigDecimal(num); - this.Value.setScale(FixedScale); + this.Value = new BigDecimal(num).setScale(FixedScale); ValidOrThrow(this.Value); }