View mode: basic / threaded / horizontal-split · Log in · Help
February 22, 2009
Re: Old problem with performance
Bill Baxter wrote:
> On Sun, Feb 22, 2009 at 5:10 AM, Denis Koroskin<2korden@gmail.com>  wrote:
>> On Sat, 21 Feb 2009 21:49:46 +0300, Bill Baxter<wbaxter@gmail.com>  wrote:
>>
>>> On Sun, Feb 22, 2009 at 1:02 AM, Weed<resume755@mail.ru>  wrote:
>>>> Bill Baxter пишет:
>>>>> 2009/2/21 Weed<resume755@mail.ru>:
>>>>>> Weed пишет:
>>>>>>> Bill Baxter пишет:
>>>>>>>
>>>>>>>> Why don't you just show us the class in the way you would like to
>>>>>>>> write it in C++, and we'll show you how to write it in D, or finally
>>>>>>>> agree with you that it's not possible.   But as long as you continue
>>>>>>>> to be hand-wavy about "common base classes" we're at a bit of an
>>>>>>>> impasse.  So far everyone thinks D can do what you want it to do
>>>>>>>> based
>>>>>>>> on your vague descriptions.
>>>>>> As I said, you can write everything using "goto" and "if".
>>>>>>
>>>>>> ...But why you do not like the original example of this thread?
>>>>> Please post again.  I don't seem to recall any detailed example.
>>>>>
>>>>> --bb
>>>>
>>>> http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=83506
>>> You should use a struct there!   Your code does not show you doing
>>> anything that would even remotely suggest using a class is worthwhile.
>>>   You're doing value operations on value types.  That's what structs
>>> are for.
>>>
>>> --bb
>> That's what *everyone* tells him!
>
> I am vaguely open to the possibility that he has a point.  FLENS uses
> inheritance, for instance.  But I don't recall what the intent of that
> inheritance was in FLENS, exactly.  I more or less ported it to D
> without that, though, so I guess I didn't find it to be essential.
> See this:
>    http://flens.cvs.sourceforge.net/viewvc/flens/FLENS/matvec/densevector.h?revision=1.36&view=markup
> Versus this:
>    http://www.dsource.org/projects/multiarray/browser/trunk/multiarray/dflat/DenseVector.d
>
> Weed, if you want to make a real argument, that would be a good place
> to start.  Investigate why and how FLENS uses inheritance.
>
> --bb

From a quick look at the flens DenseVector class:
it inherits from a Vector Interface which only declares the destructor 
as virtual.
My educated guess is that this interface has a similar purpose to the 
"struct interfaces" feature discussed here before - similar to C++ concepts.
I don't see any polymorphism here - the functions aren't virtual.
February 23, 2009
Re: Old problem with performance
Weed пишет:
> Bill Baxter пишет:
>> On Sun, Feb 22, 2009 at 1:02 AM, Weed <resume755@mail.ru> wrote:
>>> Bill Baxter пишет:
>>>> 2009/2/21 Weed <resume755@mail.ru>:
>>>>> Weed пишет:
>>>>>> Bill Baxter пишет:
>>>>>>
>>>>>>> Why don't you just show us the class in the way you would like to
>>>>>>> write it in C++, and we'll show you how to write it in D, or finally
>>>>>>> agree with you that it's not possible.   But as long as you continue
>>>>>>> to be hand-wavy about "common base classes" we're at a bit of an
>>>>>>> impasse.  So far everyone thinks D can do what you want it to do based
>>>>>>> on your vague descriptions.
>>>>> As I said, you can write everything using "goto" and "if".
>>>>>
>>>>> ...But why you do not like the original example of this thread?
>>>> Please post again.  I don't seem to recall any detailed example.
>>>>
>>>> --bb
>>> http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=83506
>> You should use a struct there!   Your code does not show you doing
>> anything that would even remotely suggest using a class is worthwhile.
>>  You're doing value operations on value types.  That's what structs
>> are for.
>  	
> Why?
> 
> What if I have not substantiated the fact that c1 is a class I should
> use there a structure?
> 
> Used in D a model of placement classes only in heap have a rule "if you
> made the class and trying to pass it by value is somewhere in your code,
> there is a design error?

Explains why the question is given in this form:

I am received or wrote a classes. Is it right to overload the operator
opAdd and use them? I think yes.

But why not allow this operation at the same speed that allows C++?
February 23, 2009
Re: Old problem with performance
Weed wrote:
> Weed пишет:
>> Bill Baxter пишет:
>>> On Sun, Feb 22, 2009 at 1:02 AM, Weed <resume755@mail.ru> wrote:
>>>> Bill Baxter пишет:
>>>>> 2009/2/21 Weed <resume755@mail.ru>:
>>>>>> Weed пишет:
>>>>>>> Bill Baxter пишет:
>>>>>>>
>>>>>>>> Why don't you just show us the class in the way you would like to
>>>>>>>> write it in C++, and we'll show you how to write it in D, or finally
>>>>>>>> agree with you that it's not possible.   But as long as you continue
>>>>>>>> to be hand-wavy about "common base classes" we're at a bit of an
>>>>>>>> impasse.  So far everyone thinks D can do what you want it to do based
>>>>>>>> on your vague descriptions.
>>>>>> As I said, you can write everything using "goto" and "if".
>>>>>>
>>>>>> ...But why you do not like the original example of this thread?
>>>>> Please post again.  I don't seem to recall any detailed example.
>>>>>
>>>>> --bb
>>>> http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=83506
>>> You should use a struct there!   Your code does not show you doing
>>> anything that would even remotely suggest using a class is worthwhile.
>>>  You're doing value operations on value types.  That's what structs
>>> are for.
>>  	
>> Why?
>>
>> What if I have not substantiated the fact that c1 is a class I should
>> use there a structure?
>>
>> Used in D a model of placement classes only in heap have a rule "if you
>> made the class and trying to pass it by value is somewhere in your code,
>> there is a design error?
> 
> Explains why the question is given in this form:
> 
> I am received or wrote a classes. Is it right to overload the operator
> opAdd and use them? I think yes.
> But why not allow this operation at the same speed that allows C++?

Actually, in D, it's really difficult to give a class value semantics.
If a, b are members of some class, consider
(1) a = a + b;
(2) a += b;
It is nearly impossible to efficiently make both of these have the same 
effect!!
You can only do it with either copy-on-write, ie, even case (2) always 
allocates; or by using a proxy class.
This is something which I consider to be a serious problem.
Much more serious than the speed issue.
February 23, 2009
Re: Old problem with performance
Weed Wrote:

> Weed �����:
> > Bill Baxter �����:
> >> On Sun, Feb 22, 2009 at 1:02 AM, Weed <resume755@mail.ru> wrote:
> >>> Bill Baxter �����:
> >>>> 2009/2/21 Weed <resume755@mail.ru>:
> >>>>> Weed �����:
> >>>>>> Bill Baxter �����:
> >>>>>>
> >>>>>>> Why don't you just show us the class in the way you would like to
> >>>>>>> write it in C++, and we'll show you how to write it in D, or finally
> >>>>>>> agree with you that it's not possible.   But as long as you continue
> >>>>>>> to be hand-wavy about "common base classes" we're at a bit of an
> >>>>>>> impasse.  So far everyone thinks D can do what you want it to do based
> >>>>>>> on your vague descriptions.
> >>>>> As I said, you can write everything using "goto" and "if".
> >>>>>
> >>>>> ...But why you do not like the original example of this thread?
> >>>> Please post again.  I don't seem to recall any detailed example.
> >>>>
> >>>> --bb
> >>> http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=83506
> >> You should use a struct there!   Your code does not show you doing
> >> anything that would even remotely suggest using a class is worthwhile.
> >>  You're doing value operations on value types.  That's what structs
> >> are for.
> >  	
> > Why?
> > 
> > What if I have not substantiated the fact that c1 is a class I should
> > use there a structure?
> > 
> > Used in D a model of placement classes only in heap have a rule "if you
> > made the class and trying to pass it by value is somewhere in your code,
> > there is a design error?
> 
> Explains why the question is given in this form:
> 
> I am received or wrote a classes. Is it right to overload the operator
> opAdd and use them? I think yes.
> 
> But why not allow this operation at the same speed that allows C++?

If you pass it by value you'll lose polymorphism. That must mean that you inherit that class only to avoid duplicating code. And that is easily done with template mixins.
February 23, 2009
Re: Old problem with performance
naryl пишет:

>>>>>> --bb
>>>>> http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=83506
>>>> You should use a struct there!   Your code does not show you doing
>>>> anything that would even remotely suggest using a class is worthwhile.
>>>>  You're doing value operations on value types.  That's what structs
>>>> are for.
>>>  	
>>> Why?
>>>
>>> What if I have not substantiated the fact that c1 is a class I should
>>> use there a structure?
>>>
>>> Used in D a model of placement classes only in heap have a rule "if you
>>> made the class and trying to pass it by value is somewhere in your code,
>>> there is a design error?
>> Explains why the question is given in this form:
>>
>> I am received or wrote a classes. Is it right to overload the operator
>> opAdd and use them? I think yes.
>>
>> But why not allow this operation at the same speed that allows C++?
> 
> If you pass it by value you'll lose polymorphism.

Debatable

By the way, in my example the transfer class by value is not
important(!), it is important to create a temporary class on the stack.
Then we can pass a reference without problem (C++ allows this through
the "&")

> That must mean that you inherit that class only to avoid duplicating code. And that is easily done with template mixins.

It is possible that this polymorphism is not needed and should be
prohibited for operations by value. The class is ready, why it should
not be used and have to redo it?

Can not think about changing when the class is ready - he could have
enormous complexity and hierarchy.
February 23, 2009
Re: Old problem with performance
Weed wrote:
> naryl пишет:
> 
>>>>>>> --bb
>>>>>> http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=83506
>>>>> You should use a struct there!   Your code does not show you doing
>>>>> anything that would even remotely suggest using a class is worthwhile.
>>>>>  You're doing value operations on value types.  That's what structs
>>>>> are for.
>>>>  	
>>>> Why?
>>>>
>>>> What if I have not substantiated the fact that c1 is a class I should
>>>> use there a structure?
>>>>
>>>> Used in D a model of placement classes only in heap have a rule "if you
>>>> made the class and trying to pass it by value is somewhere in your code,
>>>> there is a design error?
>>> Explains why the question is given in this form:
>>>
>>> I am received or wrote a classes. Is it right to overload the operator
>>> opAdd and use them? I think yes.
>>>
>>> But why not allow this operation at the same speed that allows C++?
>> If you pass it by value you'll lose polymorphism.
> 
> Debatable
> 
> By the way, in my example the transfer class by value is not
> important(!), it is important to create a temporary class on the stack.
> Then we can pass a reference without problem (C++ allows this through
> the "&")

Sure, but you're not using polymorphism.

> 
>> That must mean that you inherit that class only to avoid duplicating code. And that is easily done with template mixins.
> 
> It is possible that this polymorphism is not needed and should be
> prohibited for operations by value. The class is ready, why it should
> not be used and have to redo it?
> 
> Can not think about changing when the class is ready - he could have
> enormous complexity and hierarchy.

This is the fundamental tradeoff at the heart of the matter.
In D, the choice of whether an object will use polymorphism or not is 
considered a fundamental design decision. D gets significant benefits 
from this. C++ allows you to defer the decision, but it doesn't come for 
free.
(Generally speaking, statically typed languages do force you to make 
more decisions at design time). Notice that you have little gotchas in 
C++, such as the need to declare a virtual destructor on every struct 
you think you might "someday" use polymorphically. It sticks around, 
even if it never gets used.
One of the nice things about a D struct, compared to a C++ struct, is 
that you *know* it's simple, it never has that kind of baggage.

D does choose different trade-offs from C++. If it was always the same, 
it'd be the same language!

BTW, if you're concerned about performance, you'd do well to use 
compile-time polymorphism rather than run-time, when possible. D's 
metaprogramming support leaves C++ for dead.
February 24, 2009
Re: Old problem with performance
Weed Wrote:

> >> But why not allow this operation at the same speed that allows C++?
> > 
> > If you pass it by value you'll lose polymorphism.
> 
> It is possible that this polymorphism is not needed and should be
> prohibited for operations by value. The class is ready, why it should
> not be used and have to redo it?
> 
> Can not think about changing when the class is ready - he could have
> enormous complexity and hierarchy.

It's probably already designed to be reference-type. I think, you'll have big troubles trying to change its semantic to value-type and effectively you'll have to redo it.
February 24, 2009
Re: Old problem with performance
Kagamin wrote:
> Weed Wrote:
> 
>>>> But why not allow this operation at the same speed that allows C++?
>>> If you pass it by value you'll lose polymorphism.
>> It is possible that this polymorphism is not needed and should be
>> prohibited for operations by value. The class is ready, why it should
>> not be used and have to redo it?
>>
>> Can not think about changing when the class is ready - he could have
>> enormous complexity and hierarchy.
> 
> It's probably already designed to be reference-type. I think, you'll have big troubles trying to change its semantic to value-type and effectively you'll have to redo it.

In other words, it is not always easy to translate C++ to D. It's 
probably not worthwhile to translate any large codebase into another 
programming language, in most cases, and if you do, you will probably 
need to rewrite large portions of it anyway.
February 24, 2009
Re: Old problem with performance
Don пишет:
> Weed wrote:
>> naryl пишет:
>>
>>>>>>>> --bb
>>>>>>> http://www.digitalmars.com/pnews/read.php?server=news.digitalmars.com&group=digitalmars.D&artnum=83506
>>>>>>>
>>>>>> You should use a struct there!   Your code does not show you doing
>>>>>> anything that would even remotely suggest using a class is
>>>>>> worthwhile.
>>>>>>  You're doing value operations on value types.  That's what structs
>>>>>> are for.
>>>>>     
>>>>> Why?
>>>>>
>>>>> What if I have not substantiated the fact that c1 is a class I should
>>>>> use there a structure?
>>>>>
>>>>> Used in D a model of placement classes only in heap have a rule "if
>>>>> you
>>>>> made the class and trying to pass it by value is somewhere in your
>>>>> code,
>>>>> there is a design error?
>>>> Explains why the question is given in this form:
>>>>
>>>> I am received or wrote a classes. Is it right to overload the operator
>>>> opAdd and use them? I think yes.
>>>>
>>>> But why not allow this operation at the same speed that allows C++?
>>> If you pass it by value you'll lose polymorphism.
>>
>> Debatable
>>
>> By the way, in my example the transfer class by value is not
>> important(!), it is important to create a temporary class on the stack.
>> Then we can pass a reference without problem (C++ allows this through
>> the "&")
> 
> Sure, but you're not using polymorphism.
> 
	
There is no, but the same object can be used in other places, including
the polymorphism

>>
>>> That must mean that you inherit that class only to avoid duplicating
>>> code. And that is easily done with template mixins.
>>
>> It is possible that this polymorphism is not needed and should be
>> prohibited for operations by value. The class is ready, why it should
>> not be used and have to redo it?
>>
>> Can not think about changing when the class is ready - he could have
>> enormous complexity and hierarchy.
> 
> This is the fundamental tradeoff at the heart of the matter.
> In D, the choice of whether an object will use polymorphism or not is
> considered a fundamental design decision.

This is awful! Much easier to give to the application programmer to
decide how it will use the derived class somewhere. Such a division
should be a basic principle of designing a complex program.

> D gets significant benefits
> from this.

Benefits:

1. Disappeared slicing
2. ?

> C++ allows you to defer the decision, but it doesn't come for
> free.

Clarify what you mean?

> (Generally speaking, statically typed languages do force you to make
> more decisions at design time). Notice that you have little gotchas in
> C++, such as the need to declare a virtual destructor on every struct
> you think you might "someday" use polymorphically. It sticks around,
> even if it never gets used.
> One of the nice things about a D struct, compared to a C++ struct, is
> that you *know* it's simple, it never has that kind of baggage.
> 
> D does choose different trade-offs from C++. If it was always the same,
> it'd be the same language!

There is no need to compare the structs from C++ and D. In fact, in C++
classes and structures are the same.

I like D's idea of POD structs + without them you can not ensure
compatibility with C, but it is very important.

Now we are talking about classes.

In C++ classes with the same problems, such as what they do not have a
common base class (Object in D). Passing classes by value is not a
problem - is an advantage.

As I said earlier, the presence of pointers is also an advantage,
although they are dangerous and can lead to complex bugs.

> 
> BTW, if you're concerned about performance, you'd do well to use
> compile-time polymorphism rather than run-time, when possible. D's
> metaprogramming support leaves C++ for dead.
> 

I think if there was a language with all the features as in D but with
the model objects from C++, he would have won great popularity. Mainly
it would have moved C++ developers who are satisfied with the model
classes of C++ but not satisfied with the absence of other modern
features: metaprogramming, contracts, delegates, closures etc.
February 24, 2009
Re: Old problem with performance
Christopher Wright пишет:
> Kagamin wrote:
>> Weed Wrote:
>>
>>>>> But why not allow this operation at the same speed that allows C++?
>>>> If you pass it by value you'll lose polymorphism.
>>> It is possible that this polymorphism is not needed and should be
>>> prohibited for operations by value. The class is ready, why it should
>>> not be used and have to redo it?
>>>
>>> Can not think about changing when the class is ready - he could have
>>> enormous complexity and hierarchy.
>>
>> It's probably already designed to be reference-type. I think, you'll
>> have big troubles trying to change its semantic to value-type and
>> effectively you'll have to redo it.
> 
> In other words, it is not always easy to translate C++ to D. It's
> probably not worthwhile to translate any large codebase into another
> programming language, in most cases, and if you do, you will probably
> need to rewrite large portions of it anyway.

As a result, classes will be slow, or require more code to achieve
speeds comparable to C++.

That is actually a model of classes D harder than C++.
1 2 3 4
Top | Discussion index | About this forum | D home