Skip to content
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

Fix performance issue with boost::range::join #225

Merged
merged 3 commits into from
Nov 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
119 changes: 119 additions & 0 deletions include/powsybl/stdcxx/flattened.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/**
* Copyright (c) 2020, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#ifndef POWSYBL_STDCXX_FLATTENED_HPP
#define POWSYBL_STDCXX_FLATTENED_HPP

#include <boost/iterator_adaptors.hpp>
#include <boost/range/iterator.hpp>
#include <boost/range/iterator_range.hpp>

namespace stdcxx {

/**
* The FlatteningIterator is used to iterate over a range of ranges:
* - the OuterIterator is a forward iterator for the outer range
* - the InnerIterator is a forward iterator for the inner ranges.
*
* This iterator SHOULD not be used directly. Use the pipe operator instead.
*/
template <typename Iterator>
class FlatteningIterator :
public boost::iterator_adaptor<
FlatteningIterator<Iterator>,
Iterator,
typename Iterator::value_type::iterator::value_type,
boost::forward_traversal_tag,
typename Iterator::value_type::iterator::value_type> {
public:
using OuterIterator = Iterator;
using InnerIterator = typename Iterator::value_type::iterator;
using Value = typename Iterator::value_type::iterator::value_type;

using base = boost::iterator_adaptor<FlatteningIterator<Iterator>, Iterator, Value, boost::forward_traversal_tag, Value>;

public:
FlatteningIterator() = default;

FlatteningIterator(Iterator it) :
base(it),
m_outer_end(it),
m_inner_begin(),
m_inner_end() {
}

FlatteningIterator(Iterator begin, Iterator end) :
base(begin),
m_outer_end(end) {

if (begin != end) {
m_inner_begin = (*begin).begin(),
m_inner_end = (*begin).end();
skipEmptyRanges();
}
}

private:
void increment() {
if (this->base_reference() == m_outer_end) {
return;
}

++m_inner_begin;
skipEmptyRanges();
}

Value dereference() const {
return *m_inner_begin;
}

void skipEmptyRanges() {
while ((this->base_reference() != m_outer_end) && (m_inner_begin == m_inner_end)) {
++this->base_reference();
if (this->base_reference() != m_outer_end) {
m_inner_begin = (*this->base_reference()).begin();
m_inner_end = (*this->base_reference()).end();
}
}
}

friend class boost::iterator_core_access;

private:
OuterIterator m_outer_end;
InnerIterator m_inner_begin;
InnerIterator m_inner_end;
};

struct FlattenForwarder {};

namespace {

const FlattenForwarder flattened;

} // namespace

template <typename SinglePassRange>
using FlattenRange = boost::iterator_range<FlatteningIterator<typename boost::range_iterator<SinglePassRange>::type>>;

template <typename SinglePassRange>
FlattenRange<SinglePassRange> operator|(SinglePassRange& range, FlattenForwarder) {
using Iterator = typename boost::range_iterator<SinglePassRange>::type;

return boost::make_iterator_range(FlatteningIterator<Iterator>(boost::begin(range), boost::end(range)), FlatteningIterator<Iterator>(boost::end(range)));
}

template <typename SinglePassRange>
FlattenRange<SinglePassRange> operator|(const SinglePassRange& range, FlattenForwarder) {
using Iterator = typename boost::range_iterator<const SinglePassRange>::type;

return boost::make_iterator_range(FlatteningIterator<Iterator>(boost::begin(range), boost::end(range)), FlatteningIterator<Iterator>(boost::end(range)));
}

} // namespace stdcxx

#endif // POWSYBL_STDCXX_FLATTENED_HPP
25 changes: 10 additions & 15 deletions src/iidm/BusBreakerVoltageLevel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@

#include <cassert>

#include <boost/range/join.hpp>
#include <boost/range/adaptor/transformed.hpp>

#include <powsybl/iidm/Substation.hpp>
#include <powsybl/iidm/Switch.hpp>
#include <powsybl/iidm/ValidationUtils.hpp>
#include <powsybl/stdcxx/flattened.hpp>

#include "BusTerminal.hpp"

Expand Down Expand Up @@ -243,25 +244,19 @@ stdcxx::range<Switch> BusBreakerVoltageLevel::getSwitches() {
}

stdcxx::const_range<Terminal> BusBreakerVoltageLevel::getTerminals() const {
stdcxx::const_range<Terminal> terminals;
for (const auto& bus : m_graph.getVertexObjects()) {
if (bus) {
terminals = boost::range::join(terminals, bus.get().getTerminals());
}
}
const auto& mapper = [](const stdcxx::Reference<ConfiguredBus>& bus) {
return bus.get().getTerminals();
};

return terminals;
return m_graph.getVertexObjects() | boost::adaptors::transformed(mapper) | stdcxx::flattened;
}

stdcxx::range<Terminal> BusBreakerVoltageLevel::getTerminals() {
stdcxx::range<Terminal> terminals;
for (const auto& bus : m_graph.getVertexObjects()) {
if (bus) {
terminals = boost::range::join(terminals, bus.get().getTerminals());
}
}
const auto& mapper = [](const stdcxx::Reference<ConfiguredBus>& bus) {
return bus.get().getTerminals();
};

return terminals;
return m_graph.getVertexObjects() | boost::adaptors::transformed(mapper) | stdcxx::flattened;
}

const TopologyKind& BusBreakerVoltageLevel::getTopologyKind() const {
Expand Down
23 changes: 10 additions & 13 deletions src/iidm/MergedBus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@
#include <cassert>
#include <cmath>

#include <boost/range/join.hpp>
#include <boost/range/adaptor/transformed.hpp>

#include <powsybl/PowsyblException.hpp>
#include <powsybl/iidm/Terminal.hpp>
#include <powsybl/iidm/VoltageLevel.hpp>
#include <powsybl/stdcxx/flattened.hpp>
#include <powsybl/stdcxx/math.hpp>

namespace powsybl {
Expand Down Expand Up @@ -76,25 +77,21 @@ unsigned long MergedBus::getConnectedTerminalCount() const {
stdcxx::const_range<Terminal> MergedBus::getConnectedTerminals() const {
checkValidity();

stdcxx::const_range<Terminal> range;
const auto& mapper = [](const std::reference_wrapper<ConfiguredBus>& bus) {
return bus.get().getConnectedTerminals();
};

for (const auto& bus : m_buses) {
range = boost::range::join(range, bus.get().getConnectedTerminals());
}

return range;
return m_buses | boost::adaptors::transformed(mapper) | stdcxx::flattened;
}

stdcxx::range<Terminal> MergedBus::getConnectedTerminals() {
checkValidity();

stdcxx::range<Terminal> range;

for (const auto& bus : m_buses) {
range = boost::range::join(range, bus.get().getConnectedTerminals());
}
const auto& mapper = [](const std::reference_wrapper<ConfiguredBus>& bus) {
return bus.get().getConnectedTerminals();
};

return range;
return m_buses | boost::adaptors::transformed(mapper) | stdcxx::flattened;
}

stdcxx::CReference<Component> MergedBus::getSynchronousComponent() const {
Expand Down
63 changes: 26 additions & 37 deletions src/iidm/NetworkViews.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@

#include <powsybl/iidm/NetworkViews.hpp>

#include <boost/range/join.hpp>
#include <boost/range/adaptor/transformed.hpp>

#include <powsybl/iidm/Bus.hpp>
#include <powsybl/iidm/Network.hpp>
#include <powsybl/iidm/Switch.hpp>
#include <powsybl/iidm/VoltageLevel.hpp>
#include <powsybl/stdcxx/flattened.hpp>

#include "BusBreakerVoltageLevelViews.hpp"

Expand Down Expand Up @@ -47,23 +48,19 @@ stdcxx::Reference<Bus> BusBreakerView::getBus(const std::string& id) {
}

stdcxx::const_range<Bus> BusBreakerView::getBuses() const {
stdcxx::const_range<Bus> range;
const auto& mapper = [](const VoltageLevel& voltageLevel) {
return voltageLevel.getBusBreakerView().getBuses();
};

for (const auto& vl : m_network.getVoltageLevels()) {
range = boost::range::join(range, vl.getBusBreakerView().getBuses());
}

return range;
return m_network.getVoltageLevels() | boost::adaptors::transformed(mapper) | stdcxx::flattened;
}

stdcxx::range<Bus> BusBreakerView::getBuses() {
stdcxx::range<Bus> range;

for (auto& vl : m_network.getVoltageLevels()) {
range = boost::range::join(range, vl.getBusBreakerView().getBuses());
}
const auto& mapper = [](const VoltageLevel& voltageLevel) {
return voltageLevel.getBusBreakerView().getBuses();
};

return range;
return m_network.getVoltageLevels() | boost::adaptors::transformed(mapper) | stdcxx::flattened;
}

unsigned long BusBreakerView::getSwitchCount() const {
Expand All @@ -77,23 +74,19 @@ unsigned long BusBreakerView::getSwitchCount() const {
}

stdcxx::const_range<Switch> BusBreakerView::getSwitches() const {
stdcxx::const_range<Switch> range;

for (const auto& vl : m_network.getVoltageLevels()) {
range = boost::range::join(range, vl.getBusBreakerView().getSwitches());
}
const auto& mapper = [](const VoltageLevel& voltageLevel) {
return voltageLevel.getBusBreakerView().getSwitches();
};

return range;
return m_network.getVoltageLevels() | boost::adaptors::transformed(mapper) | stdcxx::flattened;
}

stdcxx::range<Switch> BusBreakerView::getSwitches() {
stdcxx::range<Switch> range;
const auto& mapper = [](const VoltageLevel& voltageLevel) {
return voltageLevel.getBusBreakerView().getSwitches();
};

for (auto& vl : m_network.getVoltageLevels()) {
range = boost::range::join(range, vl.getBusBreakerView().getSwitches());
}

return range;
return m_network.getVoltageLevels() | boost::adaptors::transformed(mapper) | stdcxx::flattened;
}

BusView::BusView(Network& network) :
Expand Down Expand Up @@ -121,23 +114,19 @@ stdcxx::Reference<Bus> BusView::getBus(const std::string& id) {
}

stdcxx::const_range<Bus> BusView::getBuses() const {
stdcxx::const_range<Bus> range;
const auto& mapper = [](const VoltageLevel& voltageLevel) {
return voltageLevel.getBusView().getBuses();
};

for (const auto& vl : m_network.getVoltageLevels()) {
range = boost::range::join(range, vl.getBusView().getBuses());
}

return range;
return m_network.getVoltageLevels() | boost::adaptors::transformed(mapper) | stdcxx::flattened;
}

stdcxx::range<Bus> BusView::getBuses() {
stdcxx::range<Bus> range;

for (auto& vl : m_network.getVoltageLevels()) {
range = boost::range::join(range, vl.getBusView().getBuses());
}
const auto& mapper = [](const VoltageLevel& voltageLevel) {
return voltageLevel.getBusView().getBuses();
};

return range;
return m_network.getVoltageLevels() | boost::adaptors::transformed(mapper) | stdcxx::flattened;
}

stdcxx::const_range<Component> BusView::getConnectedComponents() const {
Expand Down
Loading