Jump to page: 1 2 3
Thread overview
Good examples of value types
May 05, 2015
Luís Marques
May 05, 2015
Justin Whear
May 06, 2015
Luís Marques
May 06, 2015
Justin Whear
May 06, 2015
deadalnix
May 06, 2015
deadalnix
May 06, 2015
Russel Winder
May 06, 2015
deadalnix
May 06, 2015
Luís Marques
May 06, 2015
deadalnix
May 06, 2015
Luís Marques
May 06, 2015
deadalnix
May 06, 2015
amiga
May 07, 2015
Freddy
May 07, 2015
Dejan Lekic
May 07, 2015
Russel Winder
May 07, 2015
Nikolay
May 05, 2015
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
May 05, 2015
On Tue, 05 May 2015 20:40:58 +0000, Luís Marques wrote:

> could you come up with some type that would really benefit from being a value type but that isn't numeric (or otherwise similar)?

Dates, times, durations, regular expressions, tokens, lazy generators, digests, vectors, really any type that provides a interpretation/behavior wrapper around one or more trivially copyable types.

Frankly, the real divide in my mind is polymorphic/non-polymorphic, not by-reference vs by-value, though I will occasionally use `final class` if I want a convenience reference type around a resource.
May 06, 2015
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.
May 06, 2015
On Wednesday, 6 May 2015 at 02:07:40 UTC, deadalnix wrote:
> Problem is, all these entries needs to be value types (we are in java)
Maybe you meant reference types here?
May 06, 2015
On Wed, 2015-05-06 at 02:07 +0000, deadalnix via Digitalmars-d wrote:
> 
[…]
> Let me tell you an actual war story of mine.

I think you need to date the story, and say with GC was being used.

> 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.

I suspect this was not using G1 as tons of garbage isn't as much of a problem compared to CMS, etc.

> 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.

This is true anyway no matter which language or GC is being used.

-- 
Russel. ============================================================================= Dr Russel Winder      t: +44 20 7585 2200   voip: sip:russel.winder@ekiga.net 41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel@winder.org.uk London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder


May 06, 2015
On Tuesday, 5 May 2015 at 21:04:22 UTC, Justin Whear wrote:
> Dates, times, durations, regular expressions, tokens, lazy generators,
> digests, vectors, really any type that provides a interpretation/behavior
> wrapper around one or more trivially copyable types.

That's a very nice list, thank you, it *was* helpful. Still, I have some issues with it, maybe we can improve it.

* Dates, times, durations, vectors -> still somewhat numeric. I can add two weeks to a date, 3 seconds to a duration, etc. (for types which are not at all numeric it would probably make no difference what value are assigned which bit patterns, while for more numeric types the bit patterns must have some regularity to be able to efficiently implement operations like addition, etc.)

* Regular expressions -> I have no idea what you have in mind for this one; even after looking at std.regex...

* Tokens -> On the one hand, I think this could be an excellent example, since it's a case where the bit pattern is arbitrary (because it generally has no numeric properties). On the other hand, I could see people arguing that just using an int is perfectly fine, so it doesn't benefit from a custom type. Do notice that, even in D, an enum converts without a cast to an int, so the fact that an enum might be used to list the possible abstract values (the tokens) doesn't quite make it a completely independent type, IMHO.

* lazy generators -> explain, please?

* digests -> do you mean the digest output, or the digest function state?

> Frankly, the real divide in my mind is polymorphic/non-polymorphic, not
> by-reference vs by-value, though I will occasionally use `final class` if
> I want a convenience reference type around a resource.

Aren't you arguing against yourself? In those cases you wanted your type to have reference semantics, even though it wasn't a polymorphic type. Doesn't that counterexample prove that polymorphic/non-polymorphic is just an heuristic, while the value/ref distinction is more fundamental?
May 06, 2015
On Wednesday, 6 May 2015 at 02:07:40 UTC, deadalnix wrote:
> 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.

That's sounds like a great story! What kind of "values" were you caching?
May 06, 2015
On Wednesday, 6 May 2015 at 09:20:50 UTC, Russel Winder wrote:
> On Wed, 2015-05-06 at 02:07 +0000, deadalnix via Digitalmars-d wrote:
>> 
> […]
>> Let me tell you an actual war story of mine.
>
> I think you need to date the story, and say with GC was being used.
>

It is relatively recent (a little more than 2 years ago). Any generational GC would have trashed the same way.

>> 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.
>
> I suspect this was not using G1 as tons of garbage isn't as much of a
> problem compared to CMS, etc.
>

All of them would have trashed the same way. Generational GC bet on the fact that most object dies young. In that case, it is true, but object are kept live long enough because of the LRU that the GC promote them, and they only get collected when the full collection kicks in.
May 06, 2015
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).


May 06, 2015
On Wednesday, 6 May 2015 at 08:50:19 UTC, Dominikus Dittes Scherkl wrote:
> On Wednesday, 6 May 2015 at 02:07:40 UTC, deadalnix wrote:
>> Problem is, all these entries needs to be value types (we are in java)
> Maybe you meant reference types here?

Yes, sorry for the confusion.
« First   ‹ Prev
1 2 3