No we can't be sure.  But your point about re-use leading to objects that
could be stack allocated being moved to long-lived storage and thus
triggering full GC's is a strong hint.

For small vectors like this, it is also worth pointing out that accessing
one element is barely faster than allocating and copying 3 elements.  Once
you pay the cache miss price of accessing the first element, the rest come
almost for free.

Another factor that comes into play is that with ad-hoc code or with small
vectors (or, indeed, with vectors instead of matrices) it is very likely
that the CPU is completely bus-bound rather than compute bound.  This again
makes the full copy just about as fast as mutation.

All of these effects are even before you consider the problem of excess
copying due to impenetrable usage patterns.

On Tue, Apr 28, 2009 at 6:06 AM, Gilles Sadowski <
gil...@harfang.homelinux.org> wrote:

> > > Would you consider implementing a _mutable_ 3D-vector class?
> >
> > 3D vectors have been changed from mutable to immutable for both
> robustness and efficiency reasons ...
>
> I'm all convinced that immutability makes for cleaner, and safer code.
>
> The question is: Can we be sure that programming with immutable "Vector3D"
> ojects will always be as efficient?
> Or equivalently, is the constructor as fast as a method call that would
> modify an existing object?
>



-- 
Ted Dunning, CTO
DeepDyve

111 West Evelyn Ave. Ste. 202
Sunnyvale, CA 94086
www.deepdyve.com
858-414-0013 (m)
408-773-0220 (fax)

Reply via email to