[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [lmi] [lmi-commits] valyuta/005 4483b09 1/9: Avoid passing 'currency
From: |
Vadim Zeitlin |
Subject: |
Re: [lmi] [lmi-commits] valyuta/005 4483b09 1/9: Avoid passing 'currency' by const reference |
Date: |
Wed, 20 Jan 2021 22:55:27 +0100 |
On Wed, 20 Jan 2021 18:09:02 +0000 Greg Chicares <gchicares@sbcglobal.net>
wrote:
GC> > so I'm surprised that the results are not exactly the same, but I just
GC> > can't explain how could they differ in this direction. I may look at the
GC> > generated assembly, if you'd like, because this is just too strange.
GC>
GC> Good idea--that's better than measuring how long even a tiny
GC> unit test takes.
In my simple (but still contrived enough to prevent the compiler from just
computing the value at compile-time and generating a single line of
assembly on output) example the generated assembly is exactly the same:
---------------------------------- >8 --------------------------------------
#include <cstdlib>
#ifndef PASS_BY
#define PASS_BY
#endif
struct value_holder
{
explicit value_holder(double PASS_BY x) : x_{x} {}
value_holder& operator+=(value_holder PASS_BY z) {x_ += z.x_; return *this;
}
double x_;
};
inline bool operator==(value_holder PASS_BY lhs, value_holder PASS_BY rhs)
{ return lhs.x_ == rhs.x_; }
int main(int argc, char* argv[])
{
auto const count = std::atoi(argc == 1 ? "10" : argv[1]);
value_holder v0{0.0};
value_holder v1{0.1};
for (int n = 0; n < count; ++n)
v0 += v1;
return v0 == value_holder{1.0} ? EXIT_SUCCESS : EXIT_FAILURE;
}
---------------------------------- >8 --------------------------------------
Compiling with -DPASS_BY='const&' and without it yields absolutely
identical .s files with -O2. But the code is quite different (and shorter
for the pass-by-value version) when using -O0.
GC> Provided that the generated machine code is the same, would you
GC> favor retaining this change, e.g.:
GC> +inline bool operator==(currency lhs, currency rhs)
GC> -inline bool operator==(currency const& lhs, currency const& rhs)
I think passing by value is preferable as it's at worst as efficient (and
I often use debug, i.e. unoptimized, build of lmi in which it would be a
bit more efficient), shorter, and indicates that currency is a simple,
small value-semantics type, which might be not that obvious otherwise.
Regards,
VZ
pgp0f87tAUg2L.pgp
Description: PGP signature