From 95b849d05d05ae9e55eb839508536d5af314d331 Mon Sep 17 00:00:00 2001 From: Mark Rowe Date: Wed, 10 Dec 2025 15:20:25 -0800 Subject: 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. --- genericrange.h | 15 +++++++-------- highlevelilinstruction.cpp | 8 ++++---- highlevelilinstruction.h | 19 +++++++++++-------- lowlevelilinstruction.cpp | 4 ++-- lowlevelilinstruction.h | 12 +++++++----- mediumlevelilinstruction.cpp | 4 ++-- mediumlevelilinstruction.h | 12 +++++++----- 7 files changed, 40 insertions(+), 34 deletions(-) diff --git a/genericrange.h b/genericrange.h index 91854d16..413955a8 100644 --- a/genericrange.h +++ b/genericrange.h @@ -21,6 +21,8 @@ #pragma once +#include "base/function_ref.h" + #ifdef BINARYNINJACORE_LIBRARY #include "binaryninjacore_global.h" namespace BinaryNinjaCore @@ -157,16 +159,13 @@ using namespace std; populateRangeMap(); } - GenericRangeMap(const vector>& ranges, std::function&)> orderingStrategy) + GenericRangeMap(const vector>& ranges, bn::base::function_ref&)> orderingStrategy) { m_sourceRanges = ranges; m_flattenedRanges = ranges; flatten(m_flattenedRanges); - if (orderingStrategy) - { - for (auto& i : m_flattenedRanges) - orderingStrategy(i.GetMutableItems()); - } + for (auto& i : m_flattenedRanges) + orderingStrategy(i.GetMutableItems()); populateRangeMap(); } @@ -206,7 +205,7 @@ using namespace std; throw std::out_of_range("GenericRangeMap::GetMutableGenericRangeAt - Address not found in any range!"); } - std::optional> GetNextValidRange(uint64_t addr, std::function&)> predicate) const + std::optional> GetNextValidRange(uint64_t addr, bn::base::function_ref&)> predicate) const { auto itr = m_rangeMap.upper_bound(addr); if (itr != m_rangeMap.begin()) @@ -222,7 +221,7 @@ using namespace std; return std::nullopt; } - std::optional> GetPreviousValidRange(uint64_t addr, std::function&)> predicate) const + std::optional> GetPreviousValidRange(uint64_t addr, bn::base::function_ref&)> predicate) const { auto itr = m_rangeMap.upper_bound(addr); if (itr != m_rangeMap.begin()) diff --git a/highlevelilinstruction.cpp b/highlevelilinstruction.cpp index 55df8d21..37504253 100644 --- a/highlevelilinstruction.cpp +++ b/highlevelilinstruction.cpp @@ -1342,7 +1342,7 @@ void HighLevelILInstruction::CollectSubExprs(stack& toProcess) const } -void HighLevelILInstruction::VisitExprs(const std::function& func) const +void HighLevelILInstruction::VisitExprs(bn::base::function_ref func) const { stack toProcess; toProcess.push(exprIndex); @@ -1357,8 +1357,8 @@ void HighLevelILInstruction::VisitExprs(const std::function& preFunc, - const std::function& postFunc) const +void HighLevelILInstruction::VisitExprs(bn::base::function_ref preFunc, + bn::base::function_ref postFunc) const { stack>> toProcess; HighLevelILInstruction cur = *this; @@ -1404,7 +1404,7 @@ ExprId HighLevelILInstruction::CopyTo(HighLevelILFunction* dest, const ILSourceL ExprId HighLevelILInstruction::CopyTo( - HighLevelILFunction* dest, const std::function& subExprHandler, const ILSourceLocation& sourceLocation) const + HighLevelILFunction* dest, bn::base::function_ref subExprHandler, const ILSourceLocation& sourceLocation) const { vector output, params; diff --git a/highlevelilinstruction.h b/highlevelilinstruction.h index 84afeb98..083fed5d 100644 --- a/highlevelilinstruction.h +++ b/highlevelilinstruction.h @@ -20,16 +20,19 @@ #pragma once -#include -#include -#include +#include "base/function_ref.h" +#include "mediumlevelilinstruction.h" + #ifdef BINARYNINJACORE_LIBRARY #include "variable.h" #include "ilsourcelocation.h" #else #include "binaryninjaapi.h" #endif -#include "mediumlevelilinstruction.h" + +#include +#include + #include #ifdef BINARYNINJACORE_LIBRARY @@ -480,13 +483,13 @@ namespace BinaryNinja HighLevelILInstruction(const HighLevelILInstructionBase& instr); void CollectSubExprs(_STD_STACK& toProcess) const; - void VisitExprs(const std::function& func) const; - void VisitExprs(const std::function& preFunc, - const std::function& postFunc) const; + void VisitExprs(bn::base::function_ref func) const; + void VisitExprs(bn::base::function_ref preFunc, + bn::base::function_ref postFunc) const; ExprId CopyTo(HighLevelILFunction* dest, const ILSourceLocation& sourceLocation = {}) const; ExprId CopyTo(HighLevelILFunction* dest, - const std::function& subExprHandler, + bn::base::function_ref subExprHandler, const ILSourceLocation& sourceLocation = {}) const; bool operator<(const HighLevelILInstruction& other) const; diff --git a/lowlevelilinstruction.cpp b/lowlevelilinstruction.cpp index 036ef093..975720c1 100644 --- a/lowlevelilinstruction.cpp +++ b/lowlevelilinstruction.cpp @@ -1885,7 +1885,7 @@ void LowLevelILInstructionBase::ClearAttribute(BNILInstructionAttribute attribut } -void LowLevelILInstruction::VisitExprs(const std::function& func) const +void LowLevelILInstruction::VisitExprs(bn::base::function_ref func) const { if (!func(*this)) return; @@ -2096,7 +2096,7 @@ ExprId LowLevelILInstruction::CopyTo(LowLevelILFunction* dest, const ILSourceLoc ExprId LowLevelILInstruction::CopyTo( - LowLevelILFunction* dest, const std::function& subExprHandler, const ILSourceLocation& sourceLocation) const + LowLevelILFunction* dest, bn::base::function_ref subExprHandler, const ILSourceLocation& sourceLocation) const { vector params; BNLowLevelILLabel* labelA; diff --git a/lowlevelilinstruction.h b/lowlevelilinstruction.h index 37d0d30a..da2de8dc 100644 --- a/lowlevelilinstruction.h +++ b/lowlevelilinstruction.h @@ -20,9 +20,6 @@ #pragma once -#include -#include -#include #ifdef BINARYNINJACORE_LIBRARY #include "ilsourcelocation.h" #include "type.h" @@ -30,6 +27,11 @@ #include "binaryninjaapi.h" #endif +#include "base/function_ref.h" + +#include +#include + #ifdef BINARYNINJACORE_LIBRARY namespace BinaryNinjaCore #else @@ -866,11 +868,11 @@ namespace BinaryNinja LowLevelILFunction* func, const BNLowLevelILInstruction& instr, size_t expr, size_t instrIdx); LowLevelILInstruction(const LowLevelILInstructionBase& instr); - void VisitExprs(const std::function& func) const; + void VisitExprs(bn::base::function_ref func) const; ExprId CopyTo(LowLevelILFunction* dest, const ILSourceLocation& sourceLocation = {}) const; ExprId CopyTo(LowLevelILFunction* dest, - const std::function& subExprHandler, + bn::base::function_ref subExprHandler, const ILSourceLocation& sourceLocation = {}) const; // Templated accessors for instruction operands, use these for efficient access to a known instruction diff --git a/mediumlevelilinstruction.cpp b/mediumlevelilinstruction.cpp index b1b9d7d6..00189313 100644 --- a/mediumlevelilinstruction.cpp +++ b/mediumlevelilinstruction.cpp @@ -1350,7 +1350,7 @@ void MediumLevelILInstructionBase::ClearAttribute(BNILInstructionAttribute attri } -void MediumLevelILInstruction::VisitExprs(const std::function& func) const +void MediumLevelILInstruction::VisitExprs(bn::base::function_ref func) const { if (!func(*this)) return; @@ -1569,7 +1569,7 @@ ExprId MediumLevelILInstruction::CopyTo(MediumLevelILFunction* dest, const ILSou ExprId MediumLevelILInstruction::CopyTo(MediumLevelILFunction* dest, - const std::function& subExprHandler, + bn::base::function_ref subExprHandler, const ILSourceLocation& sourceLocation) const { vector params; diff --git a/mediumlevelilinstruction.h b/mediumlevelilinstruction.h index b3965a63..97e44a81 100644 --- a/mediumlevelilinstruction.h +++ b/mediumlevelilinstruction.h @@ -20,9 +20,6 @@ #pragma once -#include -#include -#include #ifdef BINARYNINJACORE_LIBRARY #include "constantdata.h" #include "variable.h" @@ -31,6 +28,11 @@ #include "binaryninjaapi.h" #endif +#include "base/function_ref.h" + +#include +#include + #ifdef BINARYNINJACORE_LIBRARY namespace BinaryNinjaCore #else @@ -617,11 +619,11 @@ namespace BinaryNinja MediumLevelILFunction* func, const BNMediumLevelILInstruction& instr, size_t expr, size_t instrIdx); MediumLevelILInstruction(const MediumLevelILInstructionBase& instr); - void VisitExprs(const std::function& func) const; + void VisitExprs(bn::base::function_ref func) const; ExprId CopyTo(MediumLevelILFunction* dest, const ILSourceLocation& sourceLocation = {}) const; ExprId CopyTo(MediumLevelILFunction* dest, - const std::function& subExprHandler, + bn::base::function_ref subExprHandler, const ILSourceLocation& sourceLocation = {}) const; // Templated accessors for instruction operands, use these for efficient access to a known instruction -- cgit v1.3.1