-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make fewer copies of arrays #2114
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -272,12 +272,6 @@ template <class Curve> void GlobalBootstrap<Curve>::calculate() const { | |
return std::tan((y - lowerBounds_[i]) * M_PI / (upperBounds_[i] - lowerBounds_[i]) - M_PI_2); | ||
} | ||
|
||
Real value(const Array& x) const override { | ||
Array v = values(x); | ||
std::transform(v.begin(), v.end(), v.begin(), [](Real x) -> Real { return x*x; }); | ||
return std::sqrt(std::accumulate(v.begin(), v.end(), Real(0.0)) / static_cast<Real>(v.size())); | ||
} | ||
|
||
Array values(const Array& x) const override { | ||
for (Size i = 0; i < x.size(); ++i) { | ||
Traits::updateGuess(ts_->data_, transformDirect(x[i], i), i + 1); | ||
|
@@ -304,7 +298,8 @@ template <class Curve> void GlobalBootstrap<Curve>::calculate() const { | |
Curve *ts_; | ||
const std::vector<Real> lowerBounds_, upperBounds_; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This class is local to the function, instantiated and then destroyed at function end. You can declare There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, but note that constructor arguments for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be an unnecessary precaution. This function is the only user of TargetFunction, nobody is going to pass temporary objects. But it's ok either way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's safe at the moment, but we might refractor the code in the future to, say, use std ranges. I'd rather have a compile time error than debug random numbers coming out from bootstrapping. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. My two cents is that const reference is better syntactically and stylistically, and that any regression from future refactoring will be caught right away by the address sanitizer CI job as long as there are adequate unit tests in place. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, Google C++ style prohibits non-const references and requires using pointers to mutate arguments, so using pointers for retained arguments is very natural. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Too bad the interface of Problem requires this to inherit from CostFunction. Otherwise, the whole thing could be a lambda and just capture what it needs. Oh well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. We can make a templated implementation of CostFunction that wraps a functor. Then we can write this as a lambda. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How's this #2117? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great, thanks. |
||
}; | ||
TargetFunction cost(firstHelper_, numberHelpers_, additionalErrors_, ts_, lowerBounds, upperBounds); | ||
TargetFunction cost(firstHelper_, numberHelpers_, additionalErrors_, ts_, | ||
std::move(lowerBounds), std::move(upperBounds)); | ||
|
||
// setup guess | ||
Array guess(numberBounds); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They shouldn't have been in the public interface but they are. I would deprecate them (and forward their calls to the private ones, of course).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done