Skip to content
Merged
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
12 changes: 12 additions & 0 deletions changelog.in
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,18 @@ Rank: minor
Fix Windows ARM64 builds under MSVC failing with C1128 (too many sections)
in gecode/int/extensional-tuple-set.cpp by passing /bigobj to C++ compiles.

[ENTRY]
Module: flatzinc
What: performance
Rank: major
[DESCRIPTION]
Share one syntax tree among all references to an array parameter in the
FlatZinc parser, instead of expanding the array again for every constraint
or annotation that names it. All constraint syntax trees are alive until
posting, so a parameter used by many constraints was held many times over:
190 table constraints over one 480,000-element tuple array took 2.1 GB to
parse, and now take 73 MB.

[RELEASE]
Version: 6.6.1
Date: 2026-11-29
Expand Down
1 change: 1 addition & 0 deletions cmake/GecodeSources.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ set(GECODE_TEST_SOURCES
test/flatzinc/sat_cmp_reif.cpp
test/flatzinc/sat_eq_reif.cpp
test/flatzinc/shared_array_element.cpp
test/flatzinc/shared_value_arrays.cpp
test/flatzinc/simple_sat.cpp
test/flatzinc/singHoist2.cpp
test/flatzinc/steiner_triples.cpp
Expand Down
71 changes: 65 additions & 6 deletions gecode/flatzinc/ast.hh
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ namespace Gecode::FlatZinc::AST {
public:
/// Destructor
virtual ~Node();
/**
* \brief Give up one owner's hold on this node
*
* A node normally has a single owner, and releasing it deletes it.
* A SharedArray has several owners and is deleted when the last one
* releases it. Owners release their child nodes through
* release_node instead of deleting them.
*/
virtual void release();

/// Append \a n to an array node
void append(Node* n);
Expand Down Expand Up @@ -262,20 +271,57 @@ namespace Gecode::FlatZinc::AST {
}
os << "]";
}
~Array() override {
for (int i = a.size(); i--;)
delete a[i];
~Array() override;
};

/**
* \brief %Array node shared by all references to an array parameter
*
* The parser expands a reference to an array parameter into an array
* node. A parameter named by many constraints (for example a tuple set
* shared by several table constraints) would otherwise be copied once
* per reference, and all copies stay alive until the constraints are
* posted. The parser instead builds one SharedArray per parameter
* and hands out counted references to it. Its elements must not be
* changed.
*/
class GECODE_VTABLE_EXPORT SharedArray : public Array {
protected:
/// Number of owners
unsigned int refs;

public:
/// Construct with \a n elements and one owner
SharedArray(int n) : Array(n), refs(1) {}
/// Add an owner and return this node
SharedArray*
share() {
refs++;
return this;
}
/// Remove an owner, deleting the node when none is left
void
release() override {
if (--refs == 0)
delete this;
}
};

/// Release \a n if it is not null (see Node::release)
inline void
release_node(Node* n) {
if (n != nullptr)
n->release();
}

/// %Node representing a function call
class GECODE_VTABLE_EXPORT Call : public Node {
public:
std::string id;
Node* args;
Call(const std::string& id0, Node* args0) : id(id0), args(args0) {}
~Call() override {
delete args;
release_node(args);
}
void
print(std::ostream& os) override {
Expand All @@ -299,8 +345,8 @@ namespace Gecode::FlatZinc::AST {
Node* idx;
ArrayAccess(Node* a0, Node* idx0) : a(a0), idx(idx0) {}
~ArrayAccess() override {
delete a;
delete idx;
release_node(a);
release_node(idx);
}
void
print(std::ostream& os) override {
Expand Down Expand Up @@ -335,6 +381,16 @@ namespace Gecode::FlatZinc::AST {

inline Node::~Node() = default;

inline void
Node::release() {
delete this;
}

inline Array::~Array() {
for (int i = a.size(); i--;)
release_node(a[i]);
}

inline void
Node::append(Node* new_node) {
Array* a = dynamic_cast<Array*>(this);
Expand Down Expand Up @@ -539,6 +595,9 @@ namespace Gecode::FlatZinc::AST {

inline Node*
extract_singleton(Node* n) {
// A shared array belongs to other owners too, so it is left intact.
if (dynamic_cast<SharedArray*>(n) != nullptr)
return n;
if (Array* a = dynamic_cast<Array*>(n)) {
if (a->a.size() == 1) {
Node* ret = a->a[0];
Expand Down
4 changes: 2 additions & 2 deletions gecode/flatzinc/conexpr.hh
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ namespace Gecode::FlatZinc {
}

forceinline ConExpr::~ConExpr() {
delete args;
delete ann;
AST::release_node(args);
AST::release_node(ann);
}

} // namespace Gecode::FlatZinc
Expand Down
4 changes: 4 additions & 0 deletions gecode/flatzinc/parser.hh
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ extern "C" int isatty(int);

#include <string>
#include <vector>
#include <unordered_map>
#include <array>
#include <iostream>
#include <algorithm>
Expand Down Expand Up @@ -220,6 +221,9 @@ namespace Gecode::FlatZinc {
std::vector<NamedVarSpec> set_vars;
std::vector<NamedVarSpec> float_vars;
std::vector<int> arrays;
/// One shared node per array parameter that has been referenced, by
/// position in \a arrays (see shared_value_array in parser.yxx)
std::unordered_map<int, AST::SharedArray*> shared_value_arrays;
std::vector<AST::SetLit> set_values;
std::vector<double> float_values;
std::vector<ConExpr*> constraints;
Expand Down
Loading