ENH: reduce allocations/sorting when setting time (#2461)

- use DynamicList instead of List in the cache, which reduces the
  number of allocations occuring each time.

- since the cached times are stored in sorted order, first check if the
  new time is greater than the last list entry. Can then simply append
  without performing a binary search and can obviously also skip any
  subsequent sorting.

STYLE: add noexcept to Instant methods, declare in header (like Tuple2)
This commit is contained in:
Mark Olesen 2022-05-10 16:35:33 +02:00
parent 525f77f8bb
commit dd560a6e3c
8 changed files with 198 additions and 261 deletions

View File

@ -1,121 +0,0 @@
/*---------------------------------------------------------------------------*\
========= |
\\ / F ield | OpenFOAM: The Open Source CFD Toolbox
\\ / O peration |
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2018 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
OpenFOAM is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenFOAM is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with OpenFOAM. If not, see <http://www.gnu.org/licenses/>.
\*---------------------------------------------------------------------------*/
#include "Instant.H"
// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
template<class T>
Foam::Instant<T>::Instant()
:
val_(0),
key_()
{}
template<class T>
Foam::Instant<T>::Instant::Instant(scalar val, const T& key)
:
val_(val),
key_(key)
{}
template<class T>
Foam::Instant<T>::Instant::Instant(scalar val, T&& key)
:
val_(val),
key_(std::move(key))
{}
// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
template<class T>
bool Foam::Instant<T>::equal(scalar val) const
{
return ((val_ > val - SMALL) && (val_ < val + SMALL));
}
template<class T>
template<class T2>
bool Foam::Instant<T>::equal(const Instant<T2>& other) const
{
return this->equal(other.value());
}
// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
template<class T1, class T2>
bool Foam::operator==(const Instant<T1>& a, const Instant<T2>& b)
{
return a.equal(b.value());
}
template<class T1, class T2>
bool Foam::operator!=(const Instant<T1>& a, const Instant<T2>& b)
{
return !a.equal(b.value());
}
template<class T1, class T2>
bool Foam::operator<(const Instant<T1>& a, const Instant<T2>& b)
{
return a.value() < b.value();
}
template<class T1, class T2>
bool Foam::operator>(const Instant<T1>& a, const Instant<T2>& b)
{
return a.value() > b.value();
}
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
template<class T>
Foam::Istream& Foam::operator>>(Istream& is, Instant<T>& inst)
{
is >> inst.value() >> inst.name();
return is;
}
template<class T>
Foam::Ostream& Foam::operator<<(Ostream& os, const Instant<T>& inst)
{
os << inst.value() << '\t' << inst.name();
return os;
}
// ************************************************************************* //

View File

@ -5,7 +5,7 @@
\\ / A nd | www.openfoam.com
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2018 OpenCFD Ltd.
Copyright (C) 2018-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -27,19 +27,17 @@ Class
Foam::Instant
Description
A tuple of value and key.
A tuple of scalar value and key.
The value often corresponds to a time value, thus the naming of the class.
The key will usually be a time name or a file name etc.
SourceFiles
Instant.C
\*---------------------------------------------------------------------------*/
#ifndef Instant_H
#define Instant_H
#ifndef Foam_Instant_H
#define Foam_Instant_H
#include "scalar.H"
#include <utility> // std::move
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -64,28 +62,19 @@ class Instant
public:
// Public classes
// Public Classes
//- Less function for sorting
struct less
{
bool operator()(const Instant& a, const Instant& b) const
bool operator()(const Instant& a, const Instant& b) const noexcept
{
return a.value() < b.value();
}
};
// Constructors
//- Construct null, with value = 0.
Instant();
//- Copy construct from components
Instant(scalar val, const T& key);
//- Move construct from components
Instant(scalar val, T&& key);
// Generated Methods
//- Copy construct
Instant(const Instant&) = default;
@ -93,87 +82,132 @@ public:
//- Move construct
Instant(Instant&&) = default;
// Member Functions
//- The value (const access)
scalar value() const
{
return val_;
}
//- The value (non-const access)
scalar& value()
{
return val_;
}
//- The name/key (const access)
const T& name() const
{
return key_;
}
//- The name/key (non-const access)
T& name()
{
return key_;
}
//- True if values are equal (includes SMALL for rounding)
bool equal(scalar val) const;
//- True if values are equal (includes SMALL for rounding)
template<class T2>
bool equal(const Instant<T2>& other) const;
// Operators
//- Copy assignment
Instant& operator=(const Instant&) = default;
//- Move assignment
Instant& operator=(Instant&&) = default;
// Constructors
//- Default construct, with value = 0 and empty name
Instant()
:
val_(0),
key_()
{}
//- Copy construct from components
Instant(scalar val, const T& key)
:
val_(val),
key_(key)
{}
//- Move construct from components
Instant(scalar val, T&& key)
:
val_(val),
key_(std::move(key))
{}
// Member Functions
//- The value (const access)
scalar value() const noexcept
{
return val_;
}
//- The value (non-const access)
scalar& value() noexcept
{
return val_;
}
//- The name/key (const access)
const T& name() const noexcept
{
return key_;
}
//- The name/key (non-const access)
T& name() noexcept
{
return key_;
}
//- True if values are equal (includes SMALL for rounding)
bool equal(scalar val) const noexcept
{
return ((val_ > val - SMALL) && (val_ < val + SMALL));
}
//- True if values are equal (includes SMALL for rounding)
template<class T2>
bool equal(const Instant<T2>& other) const noexcept
{
return (*this).equal(other.value());
}
};
// IOstream Operators
// * * * * * * * * * * * * * * * Global Operators * * * * * * * * * * * * * //
template<class T> Istream& operator>>(Istream& is, Instant<T>& inst);
template<class T> Ostream& operator<<(Ostream& os, const Instant<T>& inst);
// Global Operators
//- Compare instant values for equality
template<class T1, class T2>
bool operator==(const Instant<T1>& a, const Instant<T2>& b);
inline bool operator==(const Instant<T1>& a, const Instant<T2>& b) noexcept
{
return a.equal(b.value());
}
//- Compare instant values for inequality
template<class T1, class T2>
bool operator!=(const Instant<T1>& a, const Instant<T2>& b);
//- Compare instant values for less-than
template<class T1, class T2>
bool operator<(const Instant<T1>& a, const Instant<T2>& b);
inline bool operator!=(const Instant<T1>& a, const Instant<T2>& b) noexcept
{
return !a.equal(b.value());
}
//- Compare instant values for greater-than
template<class T1, class T2>
bool operator>(const Instant<T1>& a, const Instant<T2>& b);
inline bool operator<(const Instant<T1>& a, const Instant<T2>& b) noexcept
{
return (a.value() < b.value());
}
template<class T1, class T2>
inline bool operator>(const Instant<T1>& a, const Instant<T2>& b) noexcept
{
return (b.value() < a.value());
}
// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * //
//- Read instant tuple from Istream
template<class T>
inline Istream& operator>>(Istream& is, Instant<T>& inst)
{
is >> inst.value() >> inst.name();
return is;
}
//- Write instant tuple to Ostream
template<class T>
inline Ostream& operator<<(Ostream& os, const Instant<T>& inst)
{
os << inst.value() << '\t' << inst.name();
return os;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#ifdef NoRepository
#include "Instant.C"
#endif
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
#endif

View File

@ -31,17 +31,20 @@ Description
\*---------------------------------------------------------------------------*/
#ifndef fileNameInstant_H
#define fileNameInstant_H
#ifndef Foam_fileNameInstant_H
#define Foam_fileNameInstant_H
#include "fileName.H"
#include "Instant.H"
#include "instant.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
namespace Foam
{
typedef Instant<fileName> fileNameInstant;
typedef
Instant<fileName>
fileNameInstant;
} // End namespace Foam

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2018 OpenCFD Ltd.
Copyright (C) 2018-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -28,7 +28,8 @@ License
#include "instant.H"
#include "Time.H"
#include <cstdlib>
#include <cstdlib> // std::atof
#include <utility> // std::move
// * * * * * * * * * * * * * * Static Data Members * * * * * * * * * * * * * //
@ -45,14 +46,18 @@ Foam::instant::instant(scalar timeValue)
Foam::instant::instant(const word& timeName)
:
Instant<word>(std::atof(timeName.c_str()), timeName)
{}
Instant<word>(0, timeName)
{
value() = std::atof(name().c_str());
}
Foam::instant::instant(word&& timeName)
:
Instant<word>(std::atof(timeName.c_str()), std::move(timeName))
{}
Instant<word>(0, std::move(timeName))
{
value() = std::atof(name().c_str());
}
// ************************************************************************* //

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2011-2017 OpenFOAM Foundation
Copyright (C) 2018 OpenCFD Ltd.
Copyright (C) 2018-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -29,17 +29,18 @@ Class
Description
An instant of time. Contains the time value and name.
Uses Foam::Time when formatting the name.
SourceFiles
instant.C
instants.C
\*---------------------------------------------------------------------------*/
#ifndef instant_H
#define instant_H
#ifndef Foam_instant_H
#define Foam_instant_H
#include "word.H"
#include "Instant.H"
#include "word.H"
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
@ -58,15 +59,13 @@ public:
// Static Data Members
//- The type name (eg, for pTraits)
static const char* const typeName;
// Constructors
// Generated Methods
//- Copy and move construct from components
using Instant<word>::Instant;
//- Construct null, with time-value = 0.
//- Default construct, with value = 0 and empty name
instant() = default;
//- Copy construct
@ -75,26 +74,29 @@ public:
//- Move construct
instant(instant&&) = default;
//- Construct from timeValue, auto generating the name
explicit instant(scalar timeValue);
//- Construct from timeName, parsing timeName for a value
explicit instant(const word& timeName);
//- Construct from timeName, parsing timeName for a value
explicit instant(word&& timeName);
// Operators
//- Copy assignment
instant& operator=(const instant&) = default;
//- Move assignment
instant& operator=(instant&&) = default;
// Constructors
//- Inherit all constructors (eg, copy/move construct from components)
using Instant<word>::Instant;
//- Construct from timeValue, auto generating the name
explicit instant(scalar timeValue);
//- Copy construct from timeName, parsing timeName for a value
explicit instant(const word& timeName);
//- Move construct from timeName, parsing timeName for a value
explicit instant(word&& timeName);
};
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
} // End namespace Foam

View File

@ -31,8 +31,8 @@ Description
\*---------------------------------------------------------------------------*/
#ifndef instantList_H
#define instantList_H
#ifndef Foam_instantList_H
#define Foam_instantList_H
#include "instant.H"
#include "List.H"
@ -41,7 +41,11 @@ Description
namespace Foam
{
typedef List<instant> instantList;
typedef
List<instant>
instantList;
} // End namespace Foam
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

View File

@ -2234,9 +2234,9 @@ Foam::instantList Foam::fileOperations::masterUncollatedFileOperation::findTimes
if (debug)
{
Pout<< "masterUncollatedFileOperation::findTimes :"
<< " Found " << iter()->size() << " cached times" << endl;
<< " Found " << iter.val()->size() << " cached times" << endl;
}
return *iter();
return *(iter.val());
}
else
{
@ -2252,19 +2252,20 @@ Foam::instantList Foam::fileOperations::masterUncollatedFileOperation::findTimes
}
Pstream::broadcast(times); //, comm_);
// Note: do we also cache if no times have been found since it might
// indicate a directory that is being filled later on ...
instantList* tPtr = new instantList(std::move(times));
times_.set(directory, tPtr);
if (debug)
{
Pout<< "masterUncollatedFileOperation::findTimes :"
<< " Caching times:" << *tPtr << nl
<< " Caching times:" << times << nl
<< " for directory:" << directory << endl;
}
// Note: do we also cache if no times have been found since it might
// indicate a directory that is being filled later on ...
auto* tPtr = new DynamicList<instant>(std::move(times));
times_.set(directory, tPtr);
return *tPtr;
}
}
@ -2280,33 +2281,40 @@ void Foam::fileOperations::masterUncollatedFileOperation::setTime
return;
}
// Mutable access to instantList for modification and sorting
// Mutable access to instant list for modification and sorting
// - cannot use auto type deduction here
HashPtrTable<instantList>::iterator iter = times_.find(tm.path());
HashPtrTable<DynamicList<instant>>::iterator iter = times_.find(tm.path());
if (iter.found())
{
instantList& times = *iter();
DynamicList<instant>& times = *(iter.val());
const instant timeNow(tm.value(), tm.timeName());
// Exclude constant when checking and sorting
const label skipConst =
// The start index for checking and sorting (excluding "constant")
const label startIdx =
(
(!times.empty() && times[0].name() == tm.constant())
? 1
: 0
(times.empty() || times[0].name() != tm.constant())
? 0
: 1
);
if
// This routine always results in a sorted list of times, so first
// check if the new time is greater than the latest existing time.
// Can then simply append without extra searching or sorting
if (times.size() <= startIdx || times.last() < timeNow)
{
times.append(timeNow);
}
else if
(
findSortedIndex
(
SubList<instant>(times, times.size()-skipConst, skipConst),
SubList<instant>(times, times.size()-startIdx, startIdx),
timeNow
)
== -1
) < 0
)
{
if (debug)
@ -2317,9 +2325,10 @@ void Foam::fileOperations::masterUncollatedFileOperation::setTime
}
times.append(timeNow);
SubList<instant> realTimes
(
times, times.size()-skipConst, skipConst
times, times.size()-startIdx, startIdx
);
Foam::stableSort(realTimes);
}

View File

@ -6,7 +6,7 @@
\\/ M anipulation |
-------------------------------------------------------------------------------
Copyright (C) 2017 OpenFOAM Foundation
Copyright (C) 2019-2021 OpenCFD Ltd.
Copyright (C) 2019-2022 OpenCFD Ltd.
-------------------------------------------------------------------------------
License
This file is part of OpenFOAM.
@ -65,6 +65,7 @@ Description
#include "fileOperation.H"
#include "OSspecific.H"
#include "HashPtrTable.H"
#include "DynamicList.H"
#include "List.H"
#include "unthreadedInitialise.H"
@ -95,7 +96,7 @@ protected:
const label myComm_;
//- Cached times for a given directory
mutable HashPtrTable<instantList> times_;
mutable HashPtrTable<DynamicList<instant>> times_;
// Protected Operation Functors
@ -756,7 +757,7 @@ public:
virtual void flush() const;
//- Return cached times
const HashPtrTable<instantList>& times() const
const HashPtrTable<DynamicList<instant>>& times() const noexcept
{
return times_;
}