Skip to content

Commit

Permalink
8245304: Re-examine ThreadLocal usage in java.math.BigDecimal
Browse files Browse the repository at this point in the history
Reviewed-by: darcy, alanb
  • Loading branch information
Brian Burkhalter committed Aug 13, 2020
1 parent 12ae68b commit 03e5f25
Showing 1 changed file with 7 additions and 17 deletions.
24 changes: 7 additions & 17 deletions src/java.base/share/classes/java/math/BigDecimal.java
Expand Up @@ -283,14 +283,6 @@ public class BigDecimal extends Number implements Comparable<BigDecimal> {
@java.io.Serial
private static final long serialVersionUID = 6108874887143696463L;

private static final ThreadLocal<StringBuilderHelper>
threadLocalStringBuilderHelper = new ThreadLocal<StringBuilderHelper>() {
@Override
protected StringBuilderHelper initialValue() {
return new StringBuilderHelper();
}
};

// Cache of common small BigDecimal values.
private static final BigDecimal ZERO_THROUGH_TEN[] = {
new BigDecimal(BigInteger.ZERO, 0, 0, 1),
Expand Down Expand Up @@ -3798,19 +3790,17 @@ public BigDecimal ulp() {
return BigDecimal.valueOf(1, this.scale(), 1);
}

// Private class to build a string representation for BigDecimal object.
// "StringBuilderHelper" is constructed as a thread local variable so it is
// thread safe. The StringBuilder field acts as a buffer to hold the temporary
// representation of BigDecimal. The cmpCharArray holds all the characters for
// the compact representation of BigDecimal (except for '-' sign' if it is
// negative) if its intCompact field is not INFLATED. It is shared by all
// calls to toString() and its variants in that particular thread.
// Private class to build a string representation for BigDecimal object. The
// StringBuilder field acts as a buffer to hold the temporary representation
// of BigDecimal. The cmpCharArray holds all the characters for the compact
// representation of BigDecimal (except for '-' sign' if it is negative) if
// its intCompact field is not INFLATED.
static class StringBuilderHelper {
final StringBuilder sb; // Placeholder for BigDecimal string
final char[] cmpCharArray; // character array to place the intCompact

StringBuilderHelper() {
sb = new StringBuilder();
sb = new StringBuilder(32);
// All non negative longs can be made to fit into 19 character array.
cmpCharArray = new char[19];
}
Expand Down Expand Up @@ -3921,7 +3911,7 @@ private String layoutChars(boolean sci) {
StringBuilderHelper.DIGIT_ONES[lowInt]) ;
}

StringBuilderHelper sbHelper = threadLocalStringBuilderHelper.get();
StringBuilderHelper sbHelper = new StringBuilderHelper();
char[] coeff;
int offset; // offset is the starting index for coeff array
// Get the significand as an absolute value
Expand Down

0 comments on commit 03e5f25

Please sign in to comment.