Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: immutable BigInteger



Mike,

your approach looks like an example of the general strategy to work with immutable, value, objects.

1-- all values are represented by immutable objects
2-- Operations are specified by Mutator objects which do NOT represent a value.

A mutator object has method(s) to obtain the result(s) as an immutable value object of processing on one or more immutable input objects, passed in the constructor or in parameter methods. A Mutator is like a Command Design pattern.

safely wrapped inside the mutator instance the input value(s) can be represented in any way needed for optimal access and modification. Inner classes or package private helper classes can be used to structure the implementation of the mutator. this is what BigInteger's implementors did.

BTW the bytes[] you get from BigDecimal.getBytes() is a freshly allocated copy of the immutable value and can conveniently be used inside a mutator to store its initial and mutating state.

The separation between values and operations is essential to make use of the values inherently thread safe. I think that is a compelling reason to follow these design principles.

cheers,
edward

At 14:04 -0700 08-10-2007, Michael Hall wrote:
--- Thomas Singer <email@hidden> wrote:

 Integer, Double and so on all are immutable, so you
 can safely put them in a
 hash map and retrieve them later. If you need
 mutable objects for better
 memory management (you know "don't optimize before
 you really have a
 performance problem"?), you are free to write your
 own classes with the
 methods best suiting your needs.


Thanks for the replies. It seems somewhat strange to me that without more compelling reasons that no mutable alternatives have been provided. I know that since 1.3 there are mutable classes that are package private to BigInteger. I am currently considering alternative performance approaches for some crypto code. So it's sort of the point of the exercise. I have seen, and measured, where replacing a single BigInteger instantiation with a constant results in memory improvements. The code is highly iterative. In another case, of three choices for a particular algorithm, the one where I eliminated BigInteger (extracted the byte[] from a BigInteger and performed simple manipulations like shift and add, subtract against that), was both the fastest and used the least memory. One of the others was a BigInteger version of the same algorithm, 2nd fastest, worst memory (did -Xloggc and counted gc's performed). But, I didn't want to keep up efforts in avoiding BigInteger only to later have someone say, but we must have immutable for such and such compelling reason. The reasons I have heard are good but not completely compelling, so I guess I'll keep tinkering away to see if sometimes avoiding it can result in any substantial performance gains, thanks.

Mike Hall


--
Eduard de Jong
        M: +44 787 627 2204 (UK)
	F: +44 117 904 4669 (UK)
	M: +1  650 703 4608 (US)
        M: +31  62 669 2972 (NL)
    skype in:	(USA)	+1  650 515 3448
		(UK)	+44 289 581 0208



_______________________________________________
Do not post admin requests to the list. They will be ignored.
Java-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden
References: 
 >Re: immutable BigInteger (From: Michael Hall <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.