On Wednesday, 6 May 2015 at 02:07:40 UTC, deadalnix wrote:
On Tuesday, 5 May 2015 at 20:40:59 UTC, Luís Marques wrote:
Hi,
For a comparison with the Java language, I'm trying to come up
with some good examples of custom types that should be value
types (but that must be ref types in Java). I think the most
obvious ones are numeric types. So BigNum, MyNum, etc. are
good examples because programmers are used to numeric types
being value types, and having them suddenly become a ref type
just because it's MyNum instead of long is really annoying.
Still, could you come up with some type that would really
benefit from being a value type but that isn't numeric (or
otherwise similar)?
Thanks for your help!
Luís
Let me tell you an actual war story of mine.
We have this program that is computationally intensive written
in java. Somewhere in the core of the program, we have a LRU
cache, with some entries sticking in there, and most entry
getting evicted soon enough (typical pareto kind of thing).
Problem is, all these entries needs to be value types (we are
in java) and, by the time things gets evicted from the LRU
cache, they have moved to the old generation.
The whole damn thing generate a ton of garbage.
The obvious solution is to use value types in the cache, but
that not possible. I won't go in the details, but that was a
really hard problem to solve, that kept us busy for for longer
then it should have because of language limitations.
Long story short: value types are useful.
People tend to use array of primitives in such cases.
So you store an object in arrays like
long longs[MAX*NUM_LONGS_PER_RECORD];
int ints[MAX*NUM_INTS_PER_RECORD];
byte bytes[MAX*NUM_BYTES_PER_RECORD];
and then for each record you have an offset.
There are even open source collections that are completely GC
free that use this principle.
This looks like C style programming in Java.
But in the end you still use JVM and you use that for critical
part of the project only.
So value types are useful.
The good thing about D is that you can allocate such a crazy
amount of objects using malloc and avoid GC scan for them (if my
understanding of D is correct).