digitalmars.D - Post increment and decrement
- welkam (25/25) Mar 11 2015 Observation Nr. 1
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (7/30) Mar 11 2015 +5 for all your observations. (Ok, 5 each, 25 total. :p)
- welkam (3/3) Mar 11 2015 I should write regex and go trough http://code.dlang.org/ to see
- deadalnix (2/5) Mar 11 2015 No.
- mattcoder (4/6) Mar 11 2015 Believe or not I always used pre-increment. But yes, I see the
- mattcoder (3/8) Mar 11 2015 ... and I'm with you! :)
- Andrei Alexandrescu (8/31) Mar 11 2015 Observation Nr. 6
- Rikki Cattermole (4/41) Mar 11 2015 +1
- Don (4/58) Mar 12 2015 It doesn't even rely on the optimizer. This happens in the
- Rikki Cattermole (2/53) Mar 12 2015 In our implementation yes. But I'm emphasizing it doesn't have to.
- monarch_dodra (7/12) Mar 12 2015 The root of your reasoning stems from this "observation", which I
- Nick Treleaven (56/63) Mar 12 2015 Where the result is not needed, var++ is probably more common. I'd
- Kagamin (6/14) Mar 12 2015 That's how it works
- Jonathan M Davis via Digitalmars-d (11/36) Mar 13 2015 Well, much as I hate it when folks use postincrement when preincrement w...
- "Casper =?UTF-8?B?RsOmcmdlbWFuZCI=?= <shorttail hotmail.com> (3/3) Mar 14 2015 What exactly is the downside of i++? Suppose the compiler doesn't
- weaselcat (14/17) Mar 14 2015 AFAIK, there's no guarantee in C/C++ that i++ will be optimized
- deadalnix (8/11) Mar 14 2015 You need to store a temporary of the value pre increment to be
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (10/12) Mar 14 2015 If we are talking about C++, it is not possible to not take that copy
- Jonathan M Davis via Digitalmars-d (22/33) Mar 15 2015 Exactly. In C++, there isn't even a guarantee than an overloaded
- Kagamin (5/12) Mar 15 2015 If you want performance and can't rely on optimizations, you can
- ketmar (3/4) Mar 15 2015 wow! i wrote an excellect optimizer yesterday. it optimizes any code to=...
- Kagamin (2/5) Mar 15 2015 Why, rm didn't work for you?
- ketmar (2/7) Mar 15 2015 no. it removes code, and i want to optimize code.=
- welkam (38/38) Mar 20 2015 Here is some data.
- deadalnix (4/42) Mar 20 2015 Now end up you data gathering :
- welkam (8/11) Mar 20 2015 I do not know exact implementation of D compiler but I guess it
- deadalnix (4/17) Mar 20 2015 I'm waiting the numbers that support this assertion with great
- Andrei Alexandrescu (3/15) Mar 20 2015 Are you sure you're not missing the part where D's ++var and var++
- welkam (19/22) Mar 21 2015 No I am not missing it. I think I need to explain why I am doing
- Andrei Alexandrescu (5/15) Mar 21 2015 That's only for generic C++ code. At a minimum you're in the wrong
- Kagamin (2/4) Mar 23 2015 Do you have an example?
- Andrei Alexandrescu (4/5) Mar 20 2015 [snip]
- deadalnix (12/73) Mar 15 2015 Yeah, one can rationalize all he/she wants. Thing is:
- Walter Bright (4/10) Mar 22 2015 There's another reason why D specifies some arithmetic identities for
Observation Nr. 1 People prefer to write var++ instead of ++var. Observation Nr. 2 Because of observation Nr. 1 and other reasons compilers became good at removing code that is not needed making var++ and ++var to produce the same code if returned value is not used. Observation Nr. 3 Because of observation Nr. 2 more people use var++ in place where they really only need ++var. Observation Nr. 4 Because of observation Nr. 3 people learning to program may mistakenly learn that var++ is just incrementing. (I am included in that list) Observation Nr. 5 Because of observation Nr. 4 people can write slower than necessary code for classes with overloaded operator or even get bugs. Because of all this why not make only one increment/decrement operator and have post increment/decrement to be called by template name, because it is a template? template post_inc(T) { auto tmp = T; T++; return tmp; }
Mar 11 2015
On 03/11/2015 10:23 AM, welkam wrote:Observation Nr. 1 People prefer to write var++ instead of ++var. Observation Nr. 2 Because of observation Nr. 1 and other reasons compilers became good at removing code that is not needed making var++ and ++var to produce the same code if returned value is not used. Observation Nr. 3 Because of observation Nr. 2 more people use var++ in place where they really only need ++var. Observation Nr. 4 Because of observation Nr. 3 people learning to program may mistakenly learn that var++ is just incrementing. (I am included in that list) Observation Nr. 5 Because of observation Nr. 4 people can write slower than necessary code for classes with overloaded operator or even get bugs.+5 for all your observations. (Ok, 5 each, 25 total. :p) I cringe every time I see var++ and var--. That's why I advise against them: http://ddili.org/ders/d.en/arithmetic.html#ix_arithmetic.post-incrementBecause of all this why not make only one increment/decrement operator and have post increment/decrement to be called by template name, because it is a template? template post_inc(T) { auto tmp = T; T++; return tmp; }Agreed but that train has already sailed (: having a good time today); there are too many programs out there doing var++. Ali
Mar 11 2015
I should write regex and go trough http://code.dlang.org/ to see how many of them would break if we changed var++ implementation to ++var
Mar 11 2015
On Wednesday, 11 March 2015 at 19:57:15 UTC, welkam wrote:I should write regex and go trough http://code.dlang.org/ to see how many of them would break if we changed var++ implementation to ++varNo.
Mar 11 2015
On Wednesday, 11 March 2015 at 17:23:15 UTC, welkam wrote:Observation Nr. 1 People prefer to write var++ instead of ++var.Believe or not I always used pre-increment. But yes, I see the former in most of the codes out there. Matheus.
Mar 11 2015
On Wednesday, 11 March 2015 at 20:05:03 UTC, mattcoder wrote:On Wednesday, 11 March 2015 at 17:23:15 UTC, welkam wrote:... and I'm with you! :) Matheus.Observation Nr. 1 People prefer to write var++ instead of ++var.Believe or not I always used pre-increment. But yes, I see the former in most of the codes out there.
Mar 11 2015
On 3/11/15 10:23 AM, welkam wrote:Observation Nr. 1 People prefer to write var++ instead of ++var. Observation Nr. 2 Because of observation Nr. 1 and other reasons compilers became good at removing code that is not needed making var++ and ++var to produce the same code if returned value is not used. Observation Nr. 3 Because of observation Nr. 2 more people use var++ in place where they really only need ++var. Observation Nr. 4 Because of observation Nr. 3 people learning to program may mistakenly learn that var++ is just incrementing. (I am included in that list) Observation Nr. 5 Because of observation Nr. 4 people can write slower than necessary code for classes with overloaded operator or even get bugs. Because of all this why not make only one increment/decrement operator and have post increment/decrement to be called by template name, because it is a template? template post_inc(T) { auto tmp = T; T++; return tmp; }Observation Nr. 6 Somebody didn't Read The Fine Manual. Page 369: ========= If the result of a++ is not needed, the rewrite is ++a, which is subsequently rewritten to a.opUnary!"++"(). ========= Andrei
Mar 11 2015
On 12/03/2015 1:50 p.m., Andrei Alexandrescu wrote:On 3/11/15 10:23 AM, welkam wrote:+1 Compiler should work for you. This is one of those things it can rewrite to preference. During optimization.Observation Nr. 1 People prefer to write var++ instead of ++var. Observation Nr. 2 Because of observation Nr. 1 and other reasons compilers became good at removing code that is not needed making var++ and ++var to produce the same code if returned value is not used. Observation Nr. 3 Because of observation Nr. 2 more people use var++ in place where they really only need ++var. Observation Nr. 4 Because of observation Nr. 3 people learning to program may mistakenly learn that var++ is just incrementing. (I am included in that list) Observation Nr. 5 Because of observation Nr. 4 people can write slower than necessary code for classes with overloaded operator or even get bugs. Because of all this why not make only one increment/decrement operator and have post increment/decrement to be called by template name, because it is a template? template post_inc(T) { auto tmp = T; T++; return tmp; }Observation Nr. 6 Somebody didn't Read The Fine Manual. Page 369: ========= If the result of a++ is not needed, the rewrite is ++a, which is subsequently rewritten to a.opUnary!"++"(). ========= Andrei
Mar 11 2015
On Thursday, 12 March 2015 at 04:06:14 UTC, Rikki Cattermole wrote:On 12/03/2015 1:50 p.m., Andrei Alexandrescu wrote:It doesn't even rely on the optimizer. This happens in the front-end, in the semantic pass.On 3/11/15 10:23 AM, welkam wrote:+1 Compiler should work for you. This is one of those things it can rewrite to preference. During optimization.Observation Nr. 1 People prefer to write var++ instead of ++var. Observation Nr. 2 Because of observation Nr. 1 and other reasons compilers became good at removing code that is not needed making var++ and ++var to produce the same code if returned value is not used. Observation Nr. 3 Because of observation Nr. 2 more people use var++ in place where they really only need ++var. Observation Nr. 4 Because of observation Nr. 3 people learning to program may mistakenly learn that var++ is just incrementing. (I am included in that list) Observation Nr. 5 Because of observation Nr. 4 people can write slower than necessary code for classes with overloaded operator or even get bugs. Because of all this why not make only one increment/decrement operator and have post increment/decrement to be called by template name, because it is a template? template post_inc(T) { auto tmp = T; T++; return tmp; }Observation Nr. 6 Somebody didn't Read The Fine Manual. Page 369: ========= If the result of a++ is not needed, the rewrite is ++a, which is subsequently rewritten to a.opUnary!"++"(). ========= Andrei
Mar 12 2015
On 12/03/2015 9:12 p.m., Don wrote:On Thursday, 12 March 2015 at 04:06:14 UTC, Rikki Cattermole wrote:In our implementation yes. But I'm emphasizing it doesn't have to.On 12/03/2015 1:50 p.m., Andrei Alexandrescu wrote:It doesn't even rely on the optimizer. This happens in the front-end, in the semantic pass.On 3/11/15 10:23 AM, welkam wrote:+1 Compiler should work for you. This is one of those things it can rewrite to preference. During optimization.Observation Nr. 1 People prefer to write var++ instead of ++var. Observation Nr. 2 Because of observation Nr. 1 and other reasons compilers became good at removing code that is not needed making var++ and ++var to produce the same code if returned value is not used. Observation Nr. 3 Because of observation Nr. 2 more people use var++ in place where they really only need ++var. Observation Nr. 4 Because of observation Nr. 3 people learning to program may mistakenly learn that var++ is just incrementing. (I am included in that list) Observation Nr. 5 Because of observation Nr. 4 people can write slower than necessary code for classes with overloaded operator or even get bugs. Because of all this why not make only one increment/decrement operator and have post increment/decrement to be called by template name, because it is a template? template post_inc(T) { auto tmp = T; T++; return tmp; }Observation Nr. 6 Somebody didn't Read The Fine Manual. Page 369: ========= If the result of a++ is not needed, the rewrite is ++a, which is subsequently rewritten to a.opUnary!"++"(). ========= Andrei
Mar 12 2015
On Wednesday, 11 March 2015 at 17:23:15 UTC, welkam wrote:Observation Nr. 1 People prefer to write var++ instead of ++var.The root of your reasoning stems from this "observation", which I argue is wrong. The recommendation has always been to chose ++var, unless you have a reason to chose var++.Because of all this why not make only one increment/decrement operator and have post increment/decrement to be called by template name, because it is a template?Or, instead of creating a new semantic, simply use the existing one. It really isn't that complicated.
Mar 12 2015
On 12/03/2015 10:14, monarch_dodra wrote:On Wednesday, 11 March 2015 at 17:23:15 UTC, welkam wrote:Where the result is not needed, var++ is probably more common. I'd rather read post-increments. Grep -c phobos for simple for loops: ; word++): etc/c/zlib/crc32.c:9 etc/c/zlib/deflate.c:1 etc/c/zlib/gzwrite.c:2 etc/c/zlib/inftrees.c:6 etc/c/zlib/trees.c:25 etc/c/zlib/zutil.c:2 std/algorithm.d:2 std/bitmanip.d:5 std/concurrency.d:1 std/container/rbtree.d:2 std/digest/md.d:1 std/digest/ripemd.d:1 std/digest/sha.d:2 std/math.d:3 std/parallelism.d:1 std/path.d:2 std/random.d:1 std/regex/internal/backtracking.d:3 std/regex/internal/ir.d:1 std/regex/internal/kickstart.d:5 std/regex/internal/parser.d:2 std/regex/internal/tests.d:1 std/regex/package.d:1 std/socket.d:8 std/stream.d:1 std/string.d:2 std/uni.d:18 std/uri.d:11 std/utf.d:3 std/uuid.d:2 std/windows/charset.d:1 std/zip.d:2 std/zlib.d:3 unittest.d:1 ; ++word): std/algorithm.d:8 std/bitmanip.d:1 std/encoding.d:7 std/format.d:4 std/internal/math/biguintcore.d:9 std/internal/math/biguintnoasm.d:17 std/internal/math/biguintx86.d:14 std/internal/math/gammafunction.d:2 std/math.d:1 std/numeric.d:2 std/process.d:3 std/random.d:3 std/stream.d:2 std/utf.d:1 std/windows/registry.d:4 std/xml.d:1Observation Nr. 1 People prefer to write var++ instead of ++var.The root of your reasoning stems from this "observation", which I argue is wrong. The recommendation has always been to chose ++var, unless you have a reason to chose var++.
Mar 12 2015
On Wednesday, 11 March 2015 at 17:23:15 UTC, welkam wrote:Because of all this why not make only one increment/decrement operator and have post increment/decrement to be called by template name, because it is a template? template post_inc(T) { auto tmp = T; T++; return tmp; }That's how it works http://dlang.org/operatoroverloading.html#unary But I see more optimization opportunity here: the result of ++a may be not used too, but it still returns the value, which can be still suboptimal.
Mar 12 2015
On Wednesday, March 11, 2015 17:23:13 welkam via Digitalmars-d wrote:Observation Nr. 1 People prefer to write var++ instead of ++var. Observation Nr. 2 Because of observation Nr. 1 and other reasons compilers became good at removing code that is not needed making var++ and ++var to produce the same code if returned value is not used. Observation Nr. 3 Because of observation Nr. 2 more people use var++ in place where they really only need ++var. Observation Nr. 4 Because of observation Nr. 3 people learning to program may mistakenly learn that var++ is just incrementing. (I am included in that list) Observation Nr. 5 Because of observation Nr. 4 people can write slower than necessary code for classes with overloaded operator or even get bugs. Because of all this why not make only one increment/decrement operator and have post increment/decrement to be called by template name, because it is a template? template post_inc(T) { auto tmp = T; T++; return tmp; }Well, much as I hate it when folks use postincrement when preincrement will do, D solved the problem by making it so that they're overloaded with the same operator, so unlike in C++, in D, the compiler is _always_ able to replace a postincrement expression with a preincrement expression if it doesn't matter which is used. So, while I'm in the habit of correcting folks who use i++ when they should use ++i in C++, there really isn't any point in D except insomuch as it will make you a better programmer in other languages if you get into the habit of using preincrement when either will work. - Jonathan M Davis
Mar 13 2015
What exactly is the downside of i++? Suppose the compiler doesn't lower it and I use the expression alone, what is the potential damage?
Mar 14 2015
On Saturday, 14 March 2015 at 10:11:27 UTC, Casper Færgemand wrote:What exactly is the downside of i++? Suppose the compiler doesn't lower it and I use the expression alone, what is the potential damage?AFAIK, there's no guarantee in C/C++ that i++ will be optimized to ++i at all. i++ requires a temporary, this can be trivially optimized away in most cases but an overloaded post-increment can make this much more difficult an example is iterators in C++. Post increment requires you to make a copy of the iterator, increment the current one, and evaluate the copy. Pre-increment just requires you to increment the iterator then evaluate it.(This might not even be true anymore, compilers are pretty smart nowadays.) In D, I don't think it matters that much as already discussed. It's just good form, I believe.
Mar 14 2015
On Saturday, 14 March 2015 at 10:11:27 UTC, Casper Færgemand wrote:What exactly is the downside of i++? Suppose the compiler doesn't lower it and I use the expression alone, what is the potential damage?You need to store a temporary of the value pre increment to be returned. But it doesn't matter as any compiler will optimize it away if you don't use it. But, for some reason, the topic come up again and again from C++ devs that have no idea the optimization guys solved the issues for years now.
Mar 14 2015
On 03/14/2015 03:23 PM, deadalnix wrote:But, for some reason, the topic come up again and again from C++ devs that have no idea the optimization guys solved the issues for years now.If we are talking about C++, it is not possible to not take that copy for user-defined types. Due to separate compilation, the compiler does not even see the definition of operator++(int). And the compiler cannot replace calls to operator++(int) with calls to operator++() (when the return value is not used) because the programmer may have done different things than the canonical implmentation of post-increment. C++ needs a rule like D's, which will never be there. Ali
Mar 14 2015
On Saturday, March 14, 2015 15:35:24 Ali Çehreli via Digitalmars-d wrote:On 03/14/2015 03:23 PM, deadalnix wrote: > But, for some reason, the topic come up again and again from C++ devs > that have no idea the optimization guys solved the issues for years now. If we are talking about C++, it is not possible to not take that copy for user-defined types. Due to separate compilation, the compiler does not even see the definition of operator++(int). And the compiler cannot replace calls to operator++(int) with calls to operator++() (when the return value is not used) because the programmer may have done different things than the canonical implmentation of post-increment.Exactly. In C++, there isn't even a guarantee than an overloaded postincrement operator is even related to an overloaded preincrement operator. One could do addition while the other does subtraction - or nothing at all. Sure, bad code won't do that, but the compiler doesn't know whether you're being an idiot or not. So, it can't assume that overloaded preincrement and postincrement are at all related and therefore can't optimize a postincrement to a preincrement for overloaded operators. The result is that simply always using preincrement when you need to increment but don't specifically need to postincrement is a good habit to get into. Its impact is likely to be minimal in most cases, but it doesn't cost you anything, and you can't rely on the compiler's ability to optimize, because the C++ standards committee didn't restrict the overloaded increment (or decrement) operators enough to enable the compiler to optimize them properly.C++ needs a rule like D's, which will never be there.Yep. By making it so that you only overload a single operator for both versions of increment, we avoid the whole problem in D, similar to how having opCmp avoids bugs related to having to define each comparison operator individually as is the case in C++. It'll likely always bug me though when I see i++ when ++i would work, even if it doesn't matter in D. It's just too ingrained in me, I guess. :) - Jonathan M Davis
Mar 15 2015
On Sunday, 15 March 2015 at 08:45:55 UTC, Jonathan M Davis wrote:cost you anything, and you can't rely on the compiler's ability to optimizeIf you want performance and can't rely on optimizations, you can only use assembler.because the C++ standards committee didn't restrict the overloaded increment (or decrement) operators enough to enable the compiler to optimize them properly.Optimizer optimizes any code, it doesn't matter, what the code does.
Mar 15 2015
On Sun, 15 Mar 2015 19:34:11 +0000, Kagamin wrote:Optimizer optimizes any code, it doesn't matter, what the code does.wow! i wrote an excellect optimizer yesterday. it optimizes any code to=20 `nop`...=
Mar 15 2015
On Sunday, 15 March 2015 at 19:39:06 UTC, ketmar wrote:wow! i wrote an excellect optimizer yesterday. it optimizes any code to `nop`...Why, rm didn't work for you?
Mar 15 2015
On Sun, 15 Mar 2015 19:46:39 +0000, Kagamin wrote:On Sunday, 15 March 2015 at 19:39:06 UTC, ketmar wrote:no. it removes code, and i want to optimize code.=wow! i wrote an excellect optimizer yesterday. it optimizes any code to `nop`...=20 Why, rm didn't work for you?
Mar 15 2015
Here is some data. vibe.d foreach: 505 matches across 74 files ++var: 15 matches across 8 files var++: 168 matches across 37 files 20 would brake if changed meaning to ++var deadcode (IDE) foreach: 366 matches across 68 files ++var: 62 matches across 10 files var++: 111 matches across 30 files 17 would brake if changed meaning to ++var phobos foreach: 2007 matches across 77 files ++var: 508 matches across 59 files var++; 1579 matches across 90 files N/A would brake if changed meaning to ++var DMD foreach: 1338 matches across 219 files ++var: 564 matches across 139 files var++; 3285 matches across 292 files N/A would brake if changed meaning to ++var foreach number was obtained by using find string algorithm var++ number was obtained by using regex ([^\;\s\+\(\)\]\,\"]+)\+\+ ++var number was obtained by using regex \+\+([^\;\s\+\(\)\]\,\"]+)
Mar 20 2015
On Friday, 20 March 2015 at 21:56:44 UTC, welkam wrote:Here is some data. vibe.d foreach: 505 matches across 74 files ++var: 15 matches across 8 files var++: 168 matches across 37 files 20 would brake if changed meaning to ++var deadcode (IDE) foreach: 366 matches across 68 files ++var: 62 matches across 10 files var++: 111 matches across 30 files 17 would brake if changed meaning to ++var phobos foreach: 2007 matches across 77 files ++var: 508 matches across 59 files var++; 1579 matches across 90 files N/A would brake if changed meaning to ++var DMD foreach: 1338 matches across 219 files ++var: 564 matches across 139 files var++; 3285 matches across 292 files N/A would brake if changed meaning to ++var foreach number was obtained by using find string algorithm var++ number was obtained by using regex ([^\;\s\+\(\)\]\,\"]+)\+\+ ++var number was obtained by using regex \+\+([^\;\s\+\(\)\]\,\"]+)Now end up you data gathering : Turn them around (sed can do that for you), recompile, benchmark, and see that it does not change anything at the end.
Mar 20 2015
On Friday, 20 March 2015 at 22:01:35 UTC, deadalnix wrote:Now end up you data gathering : Turn them around (sed can do that for you), recompile, benchmark, and see that it does not change anything at the end.I do not know exact implementation of D compiler but I guess it generates code and then optimises it away. If thats the case then you slow down your compilation unnecessarily. Also having language construct that is being used only 15% of the time correctly is not great. And D has foreach. In C++ world this is even worse. Is this a big problem? Not really, but its still a problem.
Mar 20 2015
On Friday, 20 March 2015 at 22:52:54 UTC, welkam wrote:On Friday, 20 March 2015 at 22:01:35 UTC, deadalnix wrote:You can include measurement of compile time in your benchmark.Now end up you data gathering : Turn them around (sed can do that for you), recompile, benchmark, and see that it does not change anything at the end.I do not know exact implementation of D compiler but I guess it generates code and then optimises it away. If thats the case then you slow down your compilation unnecessarily.Also having language construct that is being used only 15% of the time correctly is not great. And D has foreach. In C++ world this is even worse. Is this a big problem? Not really, but its still a problem.I'm waiting the numbers that support this assertion with great impatience.
Mar 20 2015
On 3/20/15 3:52 PM, welkam wrote:On Friday, 20 March 2015 at 22:01:35 UTC, deadalnix wrote:Are you sure you're not missing the part where D's ++var and var++ generate identical code if the result isn't taken? -- AndreiNow end up you data gathering : Turn them around (sed can do that for you), recompile, benchmark, and see that it does not change anything at the end.I do not know exact implementation of D compiler but I guess it generates code and then optimises it away. If thats the case then you slow down your compilation unnecessarily. Also having language construct that is being used only 15% of the time correctly is not great. And D has foreach. In C++ world this is even worse. Is this a big problem? Not really, but its still a problem.
Mar 20 2015
On Saturday, 21 March 2015 at 00:42:22 UTC, Andrei Alexandrescu wrote:Are you sure you're not missing the part where D's ++var and var++ generate identical code if the result isn't taken? -- AndreiNo I am not missing it. I think I need to explain why I am doing all of this. I am currently looking for rules and programming styles that would reduce number of bugs and/or make my program run or at least compile faster. So I read that you should prefer pre increment to post increment. So I investigate and concluded that for the most part it doesn't matter. But that "most if the time" thing bugged me over a month. I investigated more and I was jumping constantly between it matters and it doesn't matter. Every time I jump to it matters the amount of matters shrinks. So now I am on the quest to quantify how much it matters and if it doesnt matter I could say to anybody who repeats it to eat a bowl of dicks. In C++ world always writing ++var can save some perf when using with overloaded operators. In D world it has no result on resulting binary and the use of these operators is reduced with foreach.
Mar 21 2015
On 3/21/15 9:59 AM, welkam wrote:On Saturday, 21 March 2015 at 00:42:22 UTC, Andrei Alexandrescu wrote:That's only for generic C++ code. At a minimum you're in the wrong forum. Also as I said, using post operations for integrals may be faster, so even in C++ it's not that clear cut. AndreiAre you sure you're not missing the part where D's ++var and var++ generate identical code if the result isn't taken? -- AndreiNo I am not missing it. I think I need to explain why I am doing all of this. I am currently looking for rules and programming styles that would reduce number of bugs and/or make my program run or at least compile faster. So I read that you should prefer pre increment to post increment.
Mar 21 2015
On Saturday, 21 March 2015 at 16:59:05 UTC, welkam wrote:In C++ world always writing ++var can save some perf when using with overloaded operators.Do you have an example?
Mar 23 2015
On Monday, 23 March 2015 at 08:20:59 UTC, Kagamin wrote:On Saturday, 21 March 2015 at 16:59:05 UTC, welkam wrote:The compiler cannot optimize away calls to external functions (such as operator new, as is my experience) that it cannot inline/see fully (I may be not 100% correct here, but I'm pretty sure it's in the right ballpark). Here's an example, compare foo() and bar() (note the -O3): http://goo.gl/H9tCaKIn C++ world always writing ++var can save some perf when using with overloaded operators.Do you have an example?
Mar 23 2015
Well, things like std::string are not cooperative in this regard. Though, it's copied only to be instantly destructed, theoretically it should be elided if the involved constructors are annotated with sufficient purity or complementary operators. Also the iterator pretends to be copyable, but it can't resume from the point where it was copied, can it?
Mar 23 2015
On Monday, 23 March 2015 at 12:10:32 UTC, Kagamin wrote:Well, things like std::string are not cooperative in this regard. Though, it's copied only to be instantly destructed, theoretically it should be elided if the involved constructors are annotated with sufficient purity or complementary operators.That's why using preincrementation is the preferred default - you simply can't know predict what the user's type will do. And sure, maybe the compiler should optimize it away in this case, but as you can see, the pragmatic solution is to simply avoid this problem entirely.Also the iterator pretends to be copyable, but it can't resume from the point where it was copied, can it?Depends on the iterator type, in this case: no, I don't believe that it can be resumed.
Mar 23 2015
On 3/20/15 2:56 PM, welkam wrote:Here is some data.[snip] Actually var++ is more efficient than ++var for integrals. I cover that in one of my optimization talks available publicly. -- Andrei
Mar 20 2015
On Sunday, 15 March 2015 at 08:45:55 UTC, Jonathan M Davis wrote:On Saturday, March 14, 2015 15:35:24 Ali Çehreli via Digitalmars-d wrote:Yeah, one can rationalize all he/she wants. Thing is: - Either the operator implementation is simple and the optimizer will see through it. - Or it is quite complex and the extra copy will barely show up in benchmark anyway. The only case where it makes sense is with separate compilation, as Ali mentioned. But in which case, same as above, either the thing is simple, and you loose way more in calling convention than you could save by optimizing a copy away, or the implementation is complex so that the calling convention or the extra copy do not matter.On 03/14/2015 03:23 PM, deadalnix wrote: > But, for some reason, the topic come up again and again from C++ devs > that have no idea the optimization guys solved the issues for years now. If we are talking about C++, it is not possible to not take that copy for user-defined types. Due to separate compilation, the compiler does not even see the definition of operator++(int). And the compiler cannot replace calls to operator++(int) with calls to operator++() (when the return value is not used) because the programmer may have done different things than the canonical implmentation of post-increment.Exactly. In C++, there isn't even a guarantee than an overloaded postincrement operator is even related to an overloaded preincrement operator. One could do addition while the other does subtraction - or nothing at all. Sure, bad code won't do that, but the compiler doesn't know whether you're being an idiot or not. So, it can't assume that overloaded preincrement and postincrement are at all related and therefore can't optimize a postincrement to a preincrement for overloaded operators. The result is that simply always using preincrement when you need to increment but don't specifically need to postincrement is a good habit to get into. Its impact is likely to be minimal in most cases, but it doesn't cost you anything, and you can't rely on the compiler's ability to optimize, because the C++ standards committee didn't restrict the overloaded increment (or decrement) operators enough to enable the compiler to optimize them properly.C++ needs a rule like D's, which will never be there.Yep. By making it so that you only overload a single operator for both versions of increment, we avoid the whole problem in D, similar to how having opCmp avoids bugs related to having to define each comparison operator individually as is the case in C++. It'll likely always bug me though when I see i++ when ++i would work, even if it doesn't matter in D. It's just too ingrained in me, I guess. :) - Jonathan M Davis
Mar 15 2015
On 3/15/2015 1:45 AM, Jonathan M Davis via Digitalmars-d wrote:On Saturday, March 14, 2015 15:35:24 Ali Çehreli via Digitalmars-d wrote:There's another reason why D specifies some arithmetic identities for overloading arithmetic operators. It's to make abominations like C++ iostreams that overload operators for non-arithmetic tasks much harder to do.C++ needs a rule like D's, which will never be there.Yep. By making it so that you only overload a single operator for both versions of increment, we avoid the whole problem in D, similar to how having opCmp avoids bugs related to having to define each comparison operator individually as is the case in C++.
Mar 22 2015