Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Checks: >-
performance-move-constructor-init,
performance-no-automatic-move,
performance-unnecessary-copy-initialization,
performance-unnecessary-value-param,
readability-identifier-naming
WarningsAsErrors: '*'
HeaderFilterRegex: '(gecode|test|examples|tools)/'
Expand All @@ -39,6 +40,9 @@ FormatStyle: file
CheckOptions:
# Explicit discards document intentional ownership handoffs.
bugprone-unused-return-value.AllowCastToVoid: true
# Shallow handles stay passed by value: a copy duplicates a few pointers or
# ids, with no reference count or allocation. See docs/code-style.md.
performance-unnecessary-value-param.AllowedTypes: '^Gecode::(Home|Group|PropagatorGroup|BrancherGroup|Pos|Reify)$;^Gecode::(Int|Bool|Set|Float)Var$;^Gecode::(Int|Bool|Set|Float)VarArray$;^Gecode::(VarArray|ViewArray)(<.*>)?$;^Gecode::(OpenIntVarList|OpenBoolVarList|OpenVarList|OpenViewList)(<.*>)?$;^Gecode::(VarImpView|DerivedView|ConstView)(<.*>)?$;^Gecode::(Int|Set|Float)::[A-Za-z]*View(<.*>)?$;^Gecode::.*::VarType$'
readability-identifier-naming.FunctionCase: lower_case
readability-identifier-naming.MethodCase: lower_case
readability-identifier-naming.VariableCase: lower_case
Expand Down
23 changes: 23 additions & 0 deletions changelog.in
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,29 @@ output while flushing complete solutions, progress, and protocol messages.
Expand clang-tidy checks for hidden copies, container operations, missed moves,
stream flushing, and correctness errors.

[ENTRY]
Module: other
What: change
Rank: major
[DESCRIPTION]
Pass owned and reference-counted parameters, and FloatVal, by const
reference instead of by value. Values that a function keeps and whose type
has a real move constructor, such as callbacks and branching configurations,
stay by value and are moved into place. This changes the exact signatures
and the ABI of many public functions: branch and assign with their
value-selection arguments, the variable- and value-selection functions and
constructors taking Rnd, AFC, Action, CHB, or merit functions, the Action
and CHB constructors taking merit functions, relax, trace, extensional with
a DFA, element with an IntSharedArray, and floating-point posting functions
and expressions taking a FloatVal. Calls compile unchanged, except that an
argument passed by value and moved must now be movable: a type with a
deleted move constructor no longer compiles there. Propagators that override
solution_distribution or domain_size_sum must take their SendMarginal or
InDecision argument by const reference.
[MORE]
Enable clang-tidy's performance-unnecessary-value-param check. Variables,
views, variable arrays, and other shallow handles stay passed by value.

[ENTRY]
Module: other
What: change
Expand Down
20 changes: 16 additions & 4 deletions docs/code-style.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,22 @@ printer should not flush each row of a multi-line solution.
The unused-return-value check accepts explicit `(void)` discards for reviewed
operations such as ownership handoff after successful engine construction.

`performance-unnecessary-value-param` is reserved for manual performance audits.
Its suggestions mix Gecode's small, intentionally copied variable/view handles
with ownership-sensitive callbacks and argument arrays. Review actual copy
cost and lifetime requirements before changing those public signatures.
`performance-unnecessary-value-param` reports parameters that are copied but
only read. Take such a parameter by `const&`. When the callee keeps the value
and the type has a real move constructor, such as `std::function`,
`std::string`, `std::vector`, or a branching configuration, take it by value
and `std::move` it into place instead.

The check's `AllowedTypes` keeps Gecode's shallow handles passed by value:
`Home`, the groups, `Pos`, `Reify`, the variables (`IntVar` and so on), the
variable arrays and `ViewArray`, the open variable and view lists, the views,
and `View::VarType`. Copying one copies a few pointers or ids, with no
reference count or allocation, and Gecode code relies on their value
semantics. Owned and reference-counted types are not exempt: `SharedHandle`
types such as `IntSet`, `TupleSet`, `DFA`, `Rnd`, `Action`, and `CHB`,
`SharedArray`, the `*Args` arrays, `REG`, strings, and callbacks. Their copies
touch a reference count or allocate, so they follow the rule above. `FloatVal`
is not exempt either: it is two doubles, not a handle.

Other performance checks need focused review: `performance-enum-size` changes
layouts and ABI, and `performance-no-int-to-ptr` conflicts with deliberate
Expand Down
2 changes: 1 addition & 1 deletion examples/job-shop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ class JobShopProbe : public JobShopBase {
post_no_overload();
}
void
branch(unsigned int p, Rnd r) {
branch(unsigned int p, const Rnd& r) {
switch (p) {
case 0U:
Gecode::branch(*this, start, INT_VAR_MIN_MIN(), INT_VAL_MIN());
Expand Down
4 changes: 2 additions & 2 deletions examples/pentominoes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ class Pentominoes : public Script {
/// Returns the regular expression for placing a specific tile \a
/// tile in a specific rotation.
REG
tile_reg(int twidth, int theight, const char* tile, REG mark, REG other, REG eol) const {
tile_reg(int twidth, int theight, const char* tile, const REG& mark, REG other, const REG& eol) const {
REG oe = other | eol;
REG res = *oe;
REG color[] = {other, mark};
Expand All @@ -317,7 +317,7 @@ class Pentominoes : public Script {
/// Returns the regular expression for placing tile number \a t in
/// any rotation.
REG
get_constraint(int t, REG mark, REG other, REG eol) {
get_constraint(int t, const REG& mark, const REG& other, const REG& eol) {
// This should be done for all rotations
REG res;
char* t2 = new char[width * height];
Expand Down
9 changes: 5 additions & 4 deletions gecode/flatzinc/blackbox-propagator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include <cstdint>
#include <exception>
#include <string>
#include <utility>
#include <vector>

namespace Gecode { namespace FlatZinc {
Expand Down Expand Up @@ -104,7 +105,7 @@ namespace Gecode { namespace FlatZinc {

void
BlackBoxContextHandle::fail(std::exception_ptr e) const {
static_cast<BlackBoxContext*>(object())->fail(e);
static_cast<BlackBoxContext*>(object())->fail(std::move(e));
}

bool
Expand Down Expand Up @@ -143,7 +144,7 @@ namespace Gecode { namespace FlatZinc {
BlackBoxContext::fail(std::exception_ptr e) {
Support::Lock lock(mutex);
if (!error_recorded.load(std::memory_order_relaxed)) {
exception = e;
exception = std::move(e);
error_recorded.store(true, std::memory_order_release);
}
}
Expand Down Expand Up @@ -283,9 +284,9 @@ namespace Gecode { namespace FlatZinc {
#ifdef GECODE_HAS_FLOAT_VARS
ViewArray<Float::FloatView>& fvar0,
#endif
SharedArray<bool> sub_int0,
const SharedArray<bool>& sub_int0,
#ifdef GECODE_HAS_FLOAT_VARS
SharedArray<bool> sub_float0,
const SharedArray<bool>& sub_float0,
#endif
const BlackBoxHandle& backend0, const BlackBoxContextHandle& context0)
: Propagator(home), ivar(ivar0),
Expand Down
2 changes: 1 addition & 1 deletion gecode/flatzinc/branch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ namespace Gecode { namespace FlatZinc {
}

void
branch(Home home, const IntVarArgs& x, const BoolVarArgs& y, IntBoolVarBranch vars, IntValBranch vals) {
branch(Home home, const IntVarArgs& x, const BoolVarArgs& y, IntBoolVarBranch vars, const IntValBranch& vals) {
if (home.failed())
return;
vars.expand(home, x, y);
Expand Down
20 changes: 10 additions & 10 deletions gecode/flatzinc/branch.hh
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ namespace Gecode { namespace FlatZinc {
/// Initialize with selection strategy \a s and decay factor \a d
IntBoolVarBranch(Select s, double d);
/// Initialize with selection strategy \a s and AFC \a i and \a b
IntBoolVarBranch(Select s, IntAFC i, BoolAFC b);
IntBoolVarBranch(Select s, const IntAFC& i, const BoolAFC& b);
/// Initialize with selection strategy \a s and action \a i and \a b
IntBoolVarBranch(Select s, IntAction i, BoolAction b);
IntBoolVarBranch(Select s, const IntAction& i, const BoolAction& b);
/// Initialize with selection strategy \a s and CHB \a i and \a b
IntBoolVarBranch(Select s, IntCHB i, BoolCHB b);
IntBoolVarBranch(Select s, const IntCHB& i, const BoolCHB& b);
/// Return selection strategy
Select select(void) const;
/// Return integer AFC
Expand All @@ -100,27 +100,27 @@ namespace Gecode { namespace FlatZinc {
/// Select variable with largest accumulated failure count
IntBoolVarBranch int_bool_var_afc_max(double d = 1.0);
/// Select variable with largest accumulated failure count
IntBoolVarBranch int_bool_var_afc_max(IntAFC ia, BoolAFC ba);
IntBoolVarBranch int_bool_var_afc_max(const IntAFC& ia, const BoolAFC& ba);
/// Select variable with highest action
IntBoolVarBranch int_bool_var_action_max(double d = 1.0);
/// Select variable with highest action
IntBoolVarBranch int_bool_var_action_max(IntAction ia, BoolAction ba);
IntBoolVarBranch int_bool_var_action_max(const IntAction& ia, const BoolAction& ba);
/// Select variable with largest CHB Q-score
IntBoolVarBranch int_bool_var_chb_max(double d = 1.0);
/// Select variable with largest CHB Q-score
IntBoolVarBranch int_bool_var_chb_max(IntCHB ic, BoolCHB bc);
IntBoolVarBranch int_bool_var_chb_max(const IntCHB& ic, const BoolCHB& bc);
/// Select variable with largest accumulated failure count divided by domain size
IntBoolVarBranch int_bool_var_afc_size_max(double d = 1.0);
/// Select variable with largest accumulated failure count divided by domain size
IntBoolVarBranch int_bool_var_afc_size_max(IntAFC ia, BoolAFC ba);
IntBoolVarBranch int_bool_var_afc_size_max(const IntAFC& ia, const BoolAFC& ba);
/// Select variable with largest action divided by domain size
IntBoolVarBranch int_bool_var_action_size_max(double d = 1.0);
/// Select variable with largest action divided by domain size
IntBoolVarBranch int_bool_var_action_size_max(IntAction ia, BoolAction ba);
IntBoolVarBranch int_bool_var_action_size_max(const IntAction& ia, const BoolAction& ba);
/// Select variable with largest CHB Q-score divided by domain size
IntBoolVarBranch int_bool_var_chb_size_max(double d = 1.0);
/// Select variable with largest CHB Q-score divided by domain size
IntBoolVarBranch int_bool_var_chb_size_max(IntCHB ic, BoolCHB bc);
IntBoolVarBranch int_bool_var_chb_size_max(const IntCHB& ic, const BoolCHB& bc);
//@}

/// Select by maximal AFC
Expand Down Expand Up @@ -335,7 +335,7 @@ namespace Gecode { namespace FlatZinc {

/// Branch function for integer and Boolean variables
GECODE_FLATZINC_EXPORT void branch(Home home, const IntVarArgs& x, const BoolVarArgs& y, IntBoolVarBranch vars,
IntValBranch vals);
const IntValBranch& vals);

}} // namespace Gecode::FlatZinc

Expand Down
18 changes: 9 additions & 9 deletions gecode/flatzinc/branch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ namespace Gecode { namespace FlatZinc {
: VarBranch<IntVar>(d, nullptr), s(s0) {}

forceinline
IntBoolVarBranch::IntBoolVarBranch(Select s0, IntAFC i, BoolAFC b)
IntBoolVarBranch::IntBoolVarBranch(Select s0, const IntAFC& i, const BoolAFC& b)
: s(s0), int_afc_data(i), bool_afc_data(b) {}

forceinline
IntBoolVarBranch::IntBoolVarBranch(Select s0, IntAction i, BoolAction b)
IntBoolVarBranch::IntBoolVarBranch(Select s0, const IntAction& i, const BoolAction& b)
: s(s0), int_action_data(i), bool_action_data(b) {}

forceinline
IntBoolVarBranch::IntBoolVarBranch(Select s0, IntCHB i, BoolCHB b)
IntBoolVarBranch::IntBoolVarBranch(Select s0, const IntCHB& i, const BoolCHB& b)
: s(s0), int_chb_data(i), bool_chb_data(b) {}

forceinline IntBoolVarBranch::Select
Expand Down Expand Up @@ -112,47 +112,47 @@ namespace Gecode { namespace FlatZinc {
return IntBoolVarBranch(IntBoolVarBranch::SEL_AFC_MAX, d);
}
inline IntBoolVarBranch
int_bool_var_afc_max(IntAFC ia, BoolAFC ba) {
int_bool_var_afc_max(const IntAFC& ia, const BoolAFC& ba) {
return IntBoolVarBranch(IntBoolVarBranch::SEL_AFC_MAX, ia, ba);
}
inline IntBoolVarBranch
int_bool_var_action_max(double d) {
return IntBoolVarBranch(IntBoolVarBranch::SEL_ACTION_MAX, d);
}
inline IntBoolVarBranch
int_bool_var_action_max(IntAction ia, BoolAction ba) {
int_bool_var_action_max(const IntAction& ia, const BoolAction& ba) {
return IntBoolVarBranch(IntBoolVarBranch::SEL_ACTION_MAX, ia, ba);
}
inline IntBoolVarBranch
int_bool_var_chb_max(double d) {
return IntBoolVarBranch(IntBoolVarBranch::SEL_CHB_MAX, d);
}
inline IntBoolVarBranch
int_bool_var_chb_max(IntCHB ic, BoolCHB bc) {
int_bool_var_chb_max(const IntCHB& ic, const BoolCHB& bc) {
return IntBoolVarBranch(IntBoolVarBranch::SEL_CHB_MAX, ic, bc);
}
inline IntBoolVarBranch
int_bool_var_afc_size_max(double d) {
return IntBoolVarBranch(IntBoolVarBranch::SEL_AFC_SIZE_MAX, d);
}
inline IntBoolVarBranch
int_bool_var_afc_size_max(IntAFC ia, BoolAFC ba) {
int_bool_var_afc_size_max(const IntAFC& ia, const BoolAFC& ba) {
return IntBoolVarBranch(IntBoolVarBranch::SEL_AFC_SIZE_MAX, ia, ba);
}
inline IntBoolVarBranch
int_bool_var_action_size_max(double d) {
return IntBoolVarBranch(IntBoolVarBranch::SEL_ACTION_SIZE_MAX, d);
}
inline IntBoolVarBranch
int_bool_var_action_size_max(IntAction ia, BoolAction ba) {
int_bool_var_action_size_max(const IntAction& ia, const BoolAction& ba) {
return IntBoolVarBranch(IntBoolVarBranch::SEL_ACTION_SIZE_MAX, ia, ba);
}
inline IntBoolVarBranch
int_bool_var_chb_size_max(double d) {
return IntBoolVarBranch(IntBoolVarBranch::SEL_CHB_SIZE_MAX, d);
}
inline IntBoolVarBranch
int_bool_var_chb_size_max(IntCHB ic, BoolCHB bc) {
int_bool_var_chb_size_max(const IntCHB& ic, const BoolCHB& bc) {
return IntBoolVarBranch(IntBoolVarBranch::SEL_CHB_SIZE_MAX, ic, bc);
}

Expand Down
Loading
Loading