News for hackers. Stuff that matters.
Re:C++ is faster and safer than Rust (2020) by frozenport on Thursday October 07, 2021 @01:32AM (#28782461)
also ++C is faster
Re:C++ is faster and safer than Rust (2020) by willbudd on Thursday October 07, 2021 @01:51AM (#28782604)
You jest, but I always found it puzzling that there was a time (supposedly?) that compilers were dumb enough to assemble ++c and c++ differently in cases where they simply occur as stand-alone expressions, despite their obvious semantic equivalence.

Even stranger: people that insist on writing their (loops; like; ++this) in 2021 as if doing so is in any way superior or something.

Re:C++ is faster and safer than Rust (2020) by vbezhenar on Thursday October 07, 2021 @01:54AM (#28782625)
Optimizing integer increment might be easy. Hard thing is optimizing overloaded increment operator. ++c is naturally faster, because it does not have to make a copy of its previous value. Especially if that operator implementation is inside another library. So it makes sense to use fast-by-default approach.
Re:C++ is faster and safer than Rust (2020) by willbudd on Thursday October 07, 2021 @05:48AM (#28784216)
Yeah, I should have specified that I was referring to plain-old-data types such as integers. Custom objects overloading their operators certainly allow any number of differentiated rabbit holes to be dug.
Re:C++ is faster and safer than Rust (2020) by jcelerier on Thursday October 07, 2021 @02:07AM (#28782735)
With complex iterator types that carry additional state themselves (for example think iterators to sparse data or generators), it definitely makes a difference
Re:C++ is faster and safer than Rust (2020) by makapuf on Thursday October 07, 2021 @02:17AM (#28782816)
a++; means do nothing then increment a, ++a means increment a then do nothing, what should be the difference? (Of course you can define operator++ to format the hard drive. )
Re:C++ is faster and safer than Rust (2020) by IcePic on Thursday October 07, 2021 @02:25AM (#28782891)
If you have a single-pass compiler (for speed) then it will not know on pass #2 that the old value is to be used for something or not and can drop it.

In a single-pass run, you must generate code to keep the previous value in case the line actually was foo = ++a; or foo = a++; so that foo gets the pre-inc or post-inc value.

The small part that creates the code for the small "a++" part of the line will then produce a dead store of the pre-inc value, and no 2nd pass will remove it.

Re:C++ is faster and safer than Rust (2020) by ohgodplsno on Thursday October 07, 2021 @03:08AM (#28783157)
Allow me introduce operator overloading, where I have suddenly defined a ++ operator on my god object that contains a 50MB pool. This operator increments a single field in it, and I really need it because I do it so often.

++godobject will simply increment that value.

godobject++ will make a copy of that object, returning it to you along with allocating another 50MB pool, purely to increment the value.

Re:C++ is faster and safer than Rust (2020) by makapuf on Friday October 08, 2021 @01:26AM (#28796027)
That's what I meant with operator++ being redefined to do anything.
Re:C++ is faster and safer than Rust (2020) by jcelerier on Friday October 08, 2021 @07:34AM (#28798055)
but when you write generic algorithms (as you should), you don't know beforehand with which "operator++" your a will be called. So you have to go for the one which will give the best performance.
Re:C++ is faster and safer than Rust (2020) by jcelerier on Thursday October 07, 2021 @04:24PM (#28791604)
> a++; means do nothing

only if a is a very simple thing. plenty of "advanced" containers actually do things ; in the "a++" case they have to save the previous state to return a copy, in the "++a" case they don't. If your iterator for instance contains a std::vector<int> to maintain some internal state for a multidimensional dynamic matrix, then in one case you'll get a simple increment, in the other a whole dynamic allocation / free which may be optimized out by the compiler in release mode and definitely won't in debug mode

Re:C++ is faster and safer than Rust (2020) by makapuf on Saturday October 09, 2021 @04:15AM (#28807975)
That is increment a part, for whatever is being defined as incrementing for this class. If you define different things for post and pre increment they will cost differently, sure. If the compiler decides to do more than what I told to, it will be longer. But will a++ always cost more than ++a?
Re:C++ is faster and safer than Rust (2020) by jcelerier on Sunday October 10, 2021 @06:29AM (#28817294)
> If you define different things for post and pre increment

They are defined as different things. One returns the current value and increments, the second increments the current value and returns it. In the first case you have to cache the previous value, in the second you don't. If your operators don't do that they are bugged and wrong.

Re:C++ is faster and safer than Rust (2020) by zarzavat on Thursday October 07, 2021 @01:40AM (#28782528)
Only C++ programmers will appreciate this joke.
Re:C++ is faster and safer than Rust (2020) by savolai on Thursday October 07, 2021 @01:42AM (#28782539)
Thank you for laughter.