Skip to content

Commit afacb96

Browse files
committed
Use bn::base::function_ref instead of std::function for parameters that are not stored
This avoids the overhead of constructing a `std::function`, which may require a heap allocation, when calling functions that immediately invokes a function object.
1 parent 1a4a6ff commit afacb96

File tree

7 files changed

+40
-34
lines changed

7 files changed

+40
-34
lines changed

genericrange.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121

2222
#pragma once
2323

24+
#include "base/function_ref.h"
25+
2426
#ifdef BINARYNINJACORE_LIBRARY
2527
#include "binaryninjacore_global.h"
2628
namespace BinaryNinjaCore
@@ -157,16 +159,13 @@ using namespace std;
157159
populateRangeMap();
158160
}
159161

160-
GenericRangeMap(const vector<GenericRange<T>>& ranges, std::function<void(vector<T>&)> orderingStrategy)
162+
GenericRangeMap(const vector<GenericRange<T>>& ranges, bn::base::function_ref<void(vector<T>&)> orderingStrategy)
161163
{
162164
m_sourceRanges = ranges;
163165
m_flattenedRanges = ranges;
164166
flatten(m_flattenedRanges);
165-
if (orderingStrategy)
166-
{
167-
for (auto& i : m_flattenedRanges)
168-
orderingStrategy(i.GetMutableItems());
169-
}
167+
for (auto& i : m_flattenedRanges)
168+
orderingStrategy(i.GetMutableItems());
170169
populateRangeMap();
171170
}
172171

@@ -206,7 +205,7 @@ using namespace std;
206205
throw std::out_of_range("GenericRangeMap::GetMutableGenericRangeAt - Address not found in any range!");
207206
}
208207

209-
std::optional<std::pair<uint64_t, uint64_t>> GetNextValidRange(uint64_t addr, std::function<bool(const GenericRange<T>&)> predicate) const
208+
std::optional<std::pair<uint64_t, uint64_t>> GetNextValidRange(uint64_t addr, bn::base::function_ref<bool(const GenericRange<T>&)> predicate) const
210209
{
211210
auto itr = m_rangeMap.upper_bound(addr);
212211
if (itr != m_rangeMap.begin())
@@ -222,7 +221,7 @@ using namespace std;
222221
return std::nullopt;
223222
}
224223

225-
std::optional<std::pair<uint64_t, uint64_t>> GetPreviousValidRange(uint64_t addr, std::function<bool(const GenericRange<T>&)> predicate) const
224+
std::optional<std::pair<uint64_t, uint64_t>> GetPreviousValidRange(uint64_t addr, bn::base::function_ref<bool(const GenericRange<T>&)> predicate) const
226225
{
227226
auto itr = m_rangeMap.upper_bound(addr);
228227
if (itr != m_rangeMap.begin())

highlevelilinstruction.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,7 +1342,7 @@ void HighLevelILInstruction::CollectSubExprs(stack<size_t>& toProcess) const
13421342
}
13431343

13441344

1345-
void HighLevelILInstruction::VisitExprs(const std::function<bool(const HighLevelILInstruction& expr)>& func) const
1345+
void HighLevelILInstruction::VisitExprs(bn::base::function_ref<bool(const HighLevelILInstruction& expr)> func) const
13461346
{
13471347
stack<size_t> toProcess;
13481348
toProcess.push(exprIndex);
@@ -1357,8 +1357,8 @@ void HighLevelILInstruction::VisitExprs(const std::function<bool(const HighLevel
13571357
}
13581358

13591359

1360-
void HighLevelILInstruction::VisitExprs(const std::function<bool(const HighLevelILInstruction& expr)>& preFunc,
1361-
const std::function<void(const HighLevelILInstruction& expr)>& postFunc) const
1360+
void HighLevelILInstruction::VisitExprs(bn::base::function_ref<bool(const HighLevelILInstruction& expr)> preFunc,
1361+
bn::base::function_ref<void(const HighLevelILInstruction& expr)> postFunc) const
13621362
{
13631363
stack<std::pair<HighLevelILInstruction, stack<size_t>>> toProcess;
13641364
HighLevelILInstruction cur = *this;
@@ -1404,7 +1404,7 @@ ExprId HighLevelILInstruction::CopyTo(HighLevelILFunction* dest, const ILSourceL
14041404

14051405

14061406
ExprId HighLevelILInstruction::CopyTo(
1407-
HighLevelILFunction* dest, const std::function<ExprId(const HighLevelILInstruction& subExpr)>& subExprHandler, const ILSourceLocation& sourceLocation) const
1407+
HighLevelILFunction* dest, bn::base::function_ref<ExprId(const HighLevelILInstruction& subExpr)> subExprHandler, const ILSourceLocation& sourceLocation) const
14081408
{
14091409
vector<ExprId> output, params;
14101410

highlevelilinstruction.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,19 @@
2020

2121
#pragma once
2222

23-
#include <functional>
24-
#include <unordered_map>
25-
#include <vector>
23+
#include "base/function_ref.h"
24+
#include "mediumlevelilinstruction.h"
25+
2626
#ifdef BINARYNINJACORE_LIBRARY
2727
#include "variable.h"
2828
#include "ilsourcelocation.h"
2929
#else
3030
#include "binaryninjaapi.h"
3131
#endif
32-
#include "mediumlevelilinstruction.h"
32+
33+
#include <unordered_map>
34+
#include <vector>
35+
3336
#include <fmt/core.h>
3437

3538
#ifdef BINARYNINJACORE_LIBRARY
@@ -480,13 +483,13 @@ namespace BinaryNinja
480483
HighLevelILInstruction(const HighLevelILInstructionBase& instr);
481484

482485
void CollectSubExprs(_STD_STACK<size_t>& toProcess) const;
483-
void VisitExprs(const std::function<bool(const HighLevelILInstruction& expr)>& func) const;
484-
void VisitExprs(const std::function<bool(const HighLevelILInstruction& expr)>& preFunc,
485-
const std::function<void(const HighLevelILInstruction& expr)>& postFunc) const;
486+
void VisitExprs(bn::base::function_ref<bool(const HighLevelILInstruction& expr)> func) const;
487+
void VisitExprs(bn::base::function_ref<bool(const HighLevelILInstruction& expr)> preFunc,
488+
bn::base::function_ref<void(const HighLevelILInstruction& expr)> postFunc) const;
486489

487490
ExprId CopyTo(HighLevelILFunction* dest, const ILSourceLocation& sourceLocation = {}) const;
488491
ExprId CopyTo(HighLevelILFunction* dest,
489-
const std::function<ExprId(const HighLevelILInstruction& subExpr)>& subExprHandler,
492+
bn::base::function_ref<ExprId(const HighLevelILInstruction& subExpr)> subExprHandler,
490493
const ILSourceLocation& sourceLocation = {}) const;
491494

492495
bool operator<(const HighLevelILInstruction& other) const;

lowlevelilinstruction.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,7 +1885,7 @@ void LowLevelILInstructionBase::ClearAttribute(BNILInstructionAttribute attribut
18851885
}
18861886

18871887

1888-
void LowLevelILInstruction::VisitExprs(const std::function<bool(const LowLevelILInstruction& expr)>& func) const
1888+
void LowLevelILInstruction::VisitExprs(bn::base::function_ref<bool(const LowLevelILInstruction& expr)> func) const
18891889
{
18901890
if (!func(*this))
18911891
return;
@@ -2096,7 +2096,7 @@ ExprId LowLevelILInstruction::CopyTo(LowLevelILFunction* dest, const ILSourceLoc
20962096

20972097

20982098
ExprId LowLevelILInstruction::CopyTo(
2099-
LowLevelILFunction* dest, const std::function<ExprId(const LowLevelILInstruction& subExpr)>& subExprHandler, const ILSourceLocation& sourceLocation) const
2099+
LowLevelILFunction* dest, bn::base::function_ref<ExprId(const LowLevelILInstruction& subExpr)> subExprHandler, const ILSourceLocation& sourceLocation) const
21002100
{
21012101
vector<ExprId> params;
21022102
BNLowLevelILLabel* labelA;

lowlevelilinstruction.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,18 @@
2020

2121
#pragma once
2222

23-
#include <functional>
24-
#include <unordered_map>
25-
#include <vector>
2623
#ifdef BINARYNINJACORE_LIBRARY
2724
#include "ilsourcelocation.h"
2825
#include "type.h"
2926
#else
3027
#include "binaryninjaapi.h"
3128
#endif
3229

30+
#include "base/function_ref.h"
31+
32+
#include <unordered_map>
33+
#include <vector>
34+
3335
#ifdef BINARYNINJACORE_LIBRARY
3436
namespace BinaryNinjaCore
3537
#else
@@ -866,11 +868,11 @@ namespace BinaryNinja
866868
LowLevelILFunction* func, const BNLowLevelILInstruction& instr, size_t expr, size_t instrIdx);
867869
LowLevelILInstruction(const LowLevelILInstructionBase& instr);
868870

869-
void VisitExprs(const std::function<bool(const LowLevelILInstruction& expr)>& func) const;
871+
void VisitExprs(bn::base::function_ref<bool(const LowLevelILInstruction& expr)> func) const;
870872

871873
ExprId CopyTo(LowLevelILFunction* dest, const ILSourceLocation& sourceLocation = {}) const;
872874
ExprId CopyTo(LowLevelILFunction* dest,
873-
const std::function<ExprId(const LowLevelILInstruction& subExpr)>& subExprHandler,
875+
bn::base::function_ref<ExprId(const LowLevelILInstruction& subExpr)> subExprHandler,
874876
const ILSourceLocation& sourceLocation = {}) const;
875877

876878
// Templated accessors for instruction operands, use these for efficient access to a known instruction

mediumlevelilinstruction.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,7 +1350,7 @@ void MediumLevelILInstructionBase::ClearAttribute(BNILInstructionAttribute attri
13501350
}
13511351

13521352

1353-
void MediumLevelILInstruction::VisitExprs(const std::function<bool(const MediumLevelILInstruction& expr)>& func) const
1353+
void MediumLevelILInstruction::VisitExprs(bn::base::function_ref<bool(const MediumLevelILInstruction& expr)> func) const
13541354
{
13551355
if (!func(*this))
13561356
return;
@@ -1569,7 +1569,7 @@ ExprId MediumLevelILInstruction::CopyTo(MediumLevelILFunction* dest, const ILSou
15691569

15701570

15711571
ExprId MediumLevelILInstruction::CopyTo(MediumLevelILFunction* dest,
1572-
const std::function<ExprId(const MediumLevelILInstruction& subExpr)>& subExprHandler,
1572+
bn::base::function_ref<ExprId(const MediumLevelILInstruction& subExpr)> subExprHandler,
15731573
const ILSourceLocation& sourceLocation) const
15741574
{
15751575
vector<ExprId> params;

mediumlevelilinstruction.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@
2020

2121
#pragma once
2222

23-
#include <functional>
24-
#include <unordered_map>
25-
#include <vector>
2623
#ifdef BINARYNINJACORE_LIBRARY
2724
#include "constantdata.h"
2825
#include "variable.h"
@@ -31,6 +28,11 @@
3128
#include "binaryninjaapi.h"
3229
#endif
3330

31+
#include "base/function_ref.h"
32+
33+
#include <unordered_map>
34+
#include <vector>
35+
3436
#ifdef BINARYNINJACORE_LIBRARY
3537
namespace BinaryNinjaCore
3638
#else
@@ -617,11 +619,11 @@ namespace BinaryNinja
617619
MediumLevelILFunction* func, const BNMediumLevelILInstruction& instr, size_t expr, size_t instrIdx);
618620
MediumLevelILInstruction(const MediumLevelILInstructionBase& instr);
619621

620-
void VisitExprs(const std::function<bool(const MediumLevelILInstruction& expr)>& func) const;
622+
void VisitExprs(bn::base::function_ref<bool(const MediumLevelILInstruction& expr)> func) const;
621623

622624
ExprId CopyTo(MediumLevelILFunction* dest, const ILSourceLocation& sourceLocation = {}) const;
623625
ExprId CopyTo(MediumLevelILFunction* dest,
624-
const std::function<ExprId(const MediumLevelILInstruction& subExpr)>& subExprHandler,
626+
bn::base::function_ref<ExprId(const MediumLevelILInstruction& subExpr)> subExprHandler,
625627
const ILSourceLocation& sourceLocation = {}) const;
626628

627629
// Templated accessors for instruction operands, use these for efficient access to a known instruction

0 commit comments

Comments
 (0)