dwavesystems / dwave-optimization

Enables the formulation of nonlinear models for industrial optimization problems.
https://docs.ocean.dwavesys.com/en/stable/docs_optimization/index.html#index-optimization
Apache License 2.0
7 stars 17 forks source link

Consider interning small constants #84

Open arcondello opened 1 month ago

arcondello commented 1 month ago

E.g. something like

class Graph {
    // existing emplacement method
    template <class NodeType, class... Args>
    NodeType* emplace_node(Args&&... args);

    template <std::same_as<ConstantNode> NodeType, std::integral T>
    NodeType* emplace_node(T constant) {
        // if out of range just call the default
        if (constant < 5 || constant > 10) return emplace_node<NodeType>(static_cast<double>(constant));

        // if it's already been created, don't add a new one
        if (interned_constants_[constant - 5] != nullptr) return interned_constants_[constant - 5];

        // create it and add it to interned_constants_
        ...
    }

    std::array<ConstantNode*, 16> interned_constants_;
};