You said itmar wrote:If you suggest that creating temporaries all over the place plus passing an extra parameter just to work around this is plain stupid.
Move time and compiler optimization
Moderator: Ras
-
lucasart
- Posts: 3242
- Joined: Mon May 31, 2010 1:29 pm
- Full name: lucasart
Re: Move time and compiler optimization
Theory and practice sometimes clash. And when that happens, theory loses. Every single time.
-
mar
- Posts: 2668
- Joined: Fri Nov 26, 2010 2:00 pm
- Location: Czech Republic
- Full name: Martin Sedlak
Re: Move time and compiler optimization
I just composed a sentence that doesn't make much sense, my english sucks.lucasart wrote:You said itmar wrote:If you suggest that creating temporaries all over the place plus passing an extra parameter just to work around this is plain stupid.
The point is of course that what you propose is stupid.
If you expose a void method, then the caller has to calculate the parameter (which is the size of private vector), which is bad as it requires the caller to do more work than necessary.
Or you could split addNode into a private method that gets called recursively and one private method.
In both cases that results in more code. Less is more and talk is cheap, remember?
-
lucasart
- Posts: 3242
- Joined: Mon May 31, 2010 1:29 pm
- Full name: lucasart
Re: Move time and compiler optimization
That would be a better design indeed.mar wrote: Or you could split addNode into a private method that gets called recursively and one private method.
My point is always the same: if you have a function that has a side effect on something which you assign the result of the function to, then your design is broken. So fix your broken design instead of looking for clever ways to get around the compiler optimization that breaks your code. Even if you find a workaround the compiler optimization, your design is still broken and your code is still counter intuitive and incomprehensible (not just for the compiler for for humans too).
Theory and practice sometimes clash. And when that happens, theory loses. Every single time.
-
mar
- Posts: 2668
- Joined: Fri Nov 26, 2010 2:00 pm
- Location: Czech Republic
- Full name: Martin Sedlak
Re: Move time and compiler optimization
Yes that would be more complicated and would result in spaghetti codelucasart wrote: That would be a better design indeed.
My point is always the same: if you have a function that has a side effect on something which you assign the result of the function to, then your design is broken. So fix your broken design instead of looking for clever ways to get around the compiler optimization that breaks your code. Even if you find a workaround the compiler optimization, your design is still broken and your code is still counter intuitive and incomprehensible (not just for the compiler for for humans too).
The rest is your opinion so please please don't speak for other humans unless you meant yourself and Marco.
-
mcostalba
- Posts: 2684
- Joined: Sat Jun 14, 2008 9:17 pm
Re: Move time and compiler optimization
I proposed my solutionmar wrote:Yes that would be more complicated and would result in spaghetti codelucasart wrote: That would be a better design indeed.
My point is always the same: if you have a function that has a side effect on something which you assign the result of the function to, then your design is broken. So fix your broken design instead of looking for clever ways to get around the compiler optimization that breaks your code. Even if you find a workaround the compiler optimization, your design is still broken and your code is still counter intuitive and incomprehensible (not just for the compiler for for humans too).
The rest is your opinion so please please don't speak for other humans unless you meant yourself and Marco.
Code: Select all
addNode( n->front, nodes, res );to be continued...
-
lucasart
- Posts: 3242
- Joined: Mon May 31, 2010 1:29 pm
- Full name: lucasart
Re: Move time and compiler optimization
Your design is broken. That's not a point of view. It's a fact. A function takes arguments and returns a value.mar wrote:Yes that would be more complicated and would result in spaghetti codelucasart wrote: That would be a better design indeed.
My point is always the same: if you have a function that has a side effect on something which you assign the result of the function to, then your design is broken. So fix your broken design instead of looking for clever ways to get around the compiler optimization that breaks your code. Even if you find a workaround the compiler optimization, your design is still broken and your code is still counter intuitive and incomprehensible (not just for the compiler for for humans too).
The rest is your opinion so please please don't speak for other humans unless you meant yourself and Marco.
The fact that compiler optimizations break your code should already tell you that your code is broken. Instead of listening you find a "clever" trick to allow your broken code to work anyway. You are starting to sound like Bob Hyatt, when he's 200% wrong but still tries to have the last word. Is it so hard to admit that your design is broken?
A function calling another is not spaghetti code. It's called modularity and is a good thing. Your code is a bunch of spaghettis.
As for the last part about not speaking for humans but only myself and Marco:
- you're a professional programmer, right?
- that means you write code that other people have to modify and vice versa?
- imagine someone else looking at this code:
Code: Select all
int tmp = addNode( n->front );
nodes[res].index = tmp;
And regarding more code, again you're wrong. In reality, you would have to spend several lines of comment explaining your ugly hack intended to prevent compiler optimization. If your code was correctly written and designed, it could be understood without comments.
Theory and practice sometimes clash. And when that happens, theory loses. Every single time.
-
mar
- Posts: 2668
- Joined: Fri Nov 26, 2010 2:00 pm
- Location: Czech Republic
- Full name: Martin Sedlak
Re: Move time and compiler optimization
I never claimed that the code wasn't broken, it was simply because the assignment operator is not a sequence point.lucasart wrote:Your design is broken. That's not a point of view. It's a fact. A function takes arguments and returns a value.
The fact that compiler optimizations break your code should already tell you that your code is broken. Instead of listening you find a "clever" trick to allow your broken code to work anyway. You are starting to sound like Bob Hyatt, when he's 200% wrong but still tries to have the last word. Is it so hard to admit that your design is broken?
A function calling another is not spaghetti code. It's called modularity and is a good thing. Your code is a bunch of spaghettis.
As for the last part about not speaking for humans but only myself and Marco:
- you're a professional programmer, right?
- that means you write code that other people have to modify and vice versa?
- imagine someone else looking at this code:Most likely, they'll just remove the tmp, which seems useless, and break the code in very subtle ways. The result is that the optimized code will be broken but not the debug code. That can be painful to debug.Code: Select all
int tmp = addNode( n->front ); nodes[res].index = tmp;
And regarding more code, again you're wrong. In reality, you would have to spend several lines of comment explaining your ugly hack intended to prevent compiler optimization. If your code was correctly written and designed, it could be understood without comments.
So instead of talking (which you are obviously good at) I hereby challenge you to show me your solution that you claim would be better.
As you probably realized, passing parent index as argument would not be enough.
-
mcostalba
- Posts: 2684
- Joined: Sat Jun 14, 2008 9:17 pm
Re: Move time and compiler optimization
This is my attempt:mcostalba wrote: to be continued...
Code: Select all
struct BSPNode
{
BSPNode *front, *back;
};
struct MapNode
{
int front, back;
};
class Map
{
std::vector< MapNode > nodes;
public:
int addNode(const BSPNode *node, int &idx);
};
int Map::addNode(const BSPNode *node, int &idx )
{
if ( !node )
return -1;
idx++;
nodes.push_back(MapNode{ addNode( node->front, idx ), addNode( node->back, idx ) });
return idx;
}
void test() {
Map map;
BSPNode* root = 0;
int index = - 1;
map.addNode( root, index );
}
Note that now root is stored at the end of the vector, don't know if for you is ok.
P.S: Not tested !
-
mar
- Posts: 2668
- Joined: Fri Nov 26, 2010 2:00 pm
- Location: Czech Republic
- Full name: Martin Sedlak
Re: Move time and compiler optimization
Yes this is a nice solution. I prefer root to be first but yes, this code doesn't contain the hack.mcostalba wrote:This is my attempt:mcostalba wrote: to be continued...
Code: Select all
struct BSPNode { BSPNode *front, *back; }; struct MapNode { int front, back; }; class Map { std::vector< MapNode > nodes; public: int addNode(const BSPNode *node, int &idx); }; int Map::addNode(const BSPNode *node, int &idx ) { if ( !node ) return -1; idx++; nodes.push_back(MapNode{ addNode( node->front, idx ), addNode( node->back, idx ) }); return idx; } void test() { Map map; BSPNode* root = 0; int index = - 1; map.addNode( root, index ); }
Note that now root is stored at the end of the vector, don't know if for you is ok.
P.S: Not tested !
I came up with this in the meantime:
Code: Select all
int Map::addNode( const BSPNode *node )
{
if ( !node )
return -1;
int res = (int)nodes.size();
MapNode mn;
nodes.push_back(mn);
mn.front = addNode( node->front );
mn.back = addNode( node->back );
nodes[res] = mn;
return res;
}
-
syzygy
- Posts: 5792
- Joined: Tue Feb 28, 2012 11:56 pm
Re: Move time and compiler optimization
Nobody needs to admit anything only because Lucas thinks he can lecture people about "broken designs".lucasart wrote:Your design is broken. That's not a point of view. It's a fact. A function takes arguments and returns a value.mar wrote:Yes that would be more complicated and would result in spaghetti codelucasart wrote: That would be a better design indeed.
My point is always the same: if you have a function that has a side effect on something which you assign the result of the function to, then your design is broken. So fix your broken design instead of looking for clever ways to get around the compiler optimization that breaks your code. Even if you find a workaround the compiler optimization, your design is still broken and your code is still counter intuitive and incomprehensible (not just for the compiler for for humans too).
The rest is your opinion so please please don't speak for other humans unless you meant yourself and Marco.
The fact that compiler optimizations break your code should already tell you that your code is broken. Instead of listening you find a "clever" trick to allow your broken code to work anyway. You are starting to sound like Bob Hyatt, when he's 200% wrong but still tries to have the last word. Is it so hard to admit that your design is broken?
Martin was so kind to give an example. No need to start a religious war. He is not complaining that the compiler is broken and should have understood what he meant. If you think you can rewrite it in a better way, then just show to do it. No need to be impolite.