• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Mutable alternative to BigDecimal?

 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello folks,

The background: I have a system that represents a value using the double type. I want to stop doing that and represent the value using a BigDecimal (or similar) type. The system in question creates and discards these values at an obscene rate and using BigDecimal as a direct replacement would result in too much object creation and GC activity.

The proposed solution: The system only needs a small number of these values at any given time so an Object Pool is the natural choice. However, an object pool of BigDecimal will not work because due to its immutable nature the object cannot be reused with different decimal values.

Question: Do you know of any Java types similar to BigDecimal that are mutable that would be suitable for use in an object pool?
 
Saloon Keeper
Posts: 15484
363
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I couldn't find anything.

You could make a MutableBigDecimal class which is based on a StringBuilder, with just those operations that you need cribbed from the BigDecimal source code.
 
Tim Cooke
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Me neither Stephan. I was hoping to avoid rolling my own mutable value type. Even my Java savvy colleagues came up empty on this one too. This must be a solved problem.
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Applications that need such a high level of performance in mathematical calculations probably don't use Java.
 
Stephan van Hulst
Saloon Keeper
Posts: 15484
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might consider using JNA to interface with MPFR.
 
Saloon Keeper
Posts: 27752
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because to handle really long numbers, you've almost got to store them in (constructed) arrays of differing sizes (or risk wasting tons of RAM), avoiding constructors probably won't gain you much.

The BigInteger class (which implements key parts of BigDecimal) does try to minimize overhead, however, by providing pre-constructed values, and since they're immutable can be kept in an inexhaustible pool. The spec apparently mandates that the values 0, 1, 10, and 100 should always be pre-constructed, but the GNU implementation also pre-constructs in the range of -100 to 1000.

As always, you should measure before attempting to optimize - it's entirely possible that any overhead from constructors would be swamped by long-precision calculations anyway.

But I'm not optimistic that you can easily out-perform the standard class without paying a penalty somewhere. The most likely way (to me) to avoid constructions would be to create a class with a fixed number of digit slots a la an array of bytes. And pay the price in RAM.
 
I am mighty! And this is a mighty small ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic