Reserve memory before inserting into a flat set

This commit is contained in:
seelabs
2019-04-03 14:17:27 -04:00
parent 80e535a13c
commit 61bd06177f
3 changed files with 54 additions and 8 deletions

View File

@@ -17,6 +17,7 @@
*/
//==============================================================================
#include <ripple/app/paths/impl/FlatSets.h>
#include <ripple/app/paths/impl/Steps.h>
#include <ripple/app/paths/Credit.h>
#include <ripple/app/paths/NodeDirectory.h>
@@ -729,8 +730,7 @@ BookStep<TIn, TOut, TDerived>::revImp (
auto const r = forEachOffer (sb, afView, prevStepRedeems, eachOffer);
boost::container::flat_set<uint256> toRm = std::move(std::get<0>(r));
std::uint32_t const offersConsumed = std::get<1>(r);
ofrsToRm.insert (boost::container::ordered_unique_range_t{},
toRm.begin (), toRm.end ());
SetUnion(ofrsToRm, toRm);
if (offersConsumed >= maxOffersToConsume_)
{
@@ -891,8 +891,7 @@ BookStep<TIn, TOut, TDerived>::fwdImp (
auto const r = forEachOffer (sb, afView, prevStepRedeems, eachOffer);
boost::container::flat_set<uint256> toRm = std::move(std::get<0>(r));
std::uint32_t const offersConsumed = std::get<1>(r);
ofrsToRm.insert (boost::container::ordered_unique_range_t{},
toRm.begin (), toRm.end ());
SetUnion(ofrsToRm, toRm);
if (offersConsumed >= maxOffersToConsume_)
{

View File

@@ -0,0 +1,48 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2019 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================
#ifndef RIPPLE_APP_PATHS_IMPL_FLAT_SETS_H_INCLUDED
#define RIPPLE_APP_PATHS_IMPL_FLAT_SETS_H_INCLUDED
#include <boost/container/flat_set.hpp>
namespace ripple {
/** Given two flat sets dst and src, compute dst = dst union src
@param dst set to store the resulting union, and also a source of elements for the union
@param src second source of elements for the union
*/
template <class T>
void
SetUnion(
boost::container::flat_set<T>& dst,
boost::container::flat_set<T> const& src)
{
if (src.empty())
return;
dst.reserve(dst.size() + src.size());
dst.insert(
boost::container::ordered_unique_range_t{}, src.begin(), src.end());
}
} // namespace ripple
#endif

View File

@@ -23,6 +23,7 @@
#include <ripple/app/paths/Credit.h>
#include <ripple/app/paths/Flow.h>
#include <ripple/app/paths/impl/AmountSpec.h>
#include <ripple/app/paths/impl/FlatSets.h>
#include <ripple/app/paths/impl/FlowDebugInfo.h>
#include <ripple/app/paths/impl/Steps.h>
#include <ripple/basics/Log.h>
@@ -506,8 +507,7 @@ flow (PaymentSandbox const& baseView,
sb, *strand, remainingIn, remainingOut, j);
// rm bad offers even if the strand fails
ofrsToRm.insert (boost::container::ordered_unique_range_t{},
f.ofrsToRm.begin (), f.ofrsToRm.end ());
SetUnion(ofrsToRm, f.ofrsToRm);
if (f.ter != tesSUCCESS || f.out == beast::zero)
continue;
@@ -589,8 +589,7 @@ flow (PaymentSandbox const& baseView,
// view
if (!ofrsToRm.empty ())
{
ofrsToRmOnFail.insert (boost::container::ordered_unique_range_t{},
ofrsToRm.begin (), ofrsToRm.end ());
SetUnion(ofrsToRmOnFail, ofrsToRm);
for (auto const& o : ofrsToRm)
{
if (auto ok = sb.peek (keylet::offer (o)))