Skip to content

Commit d1b79cc

Browse files
committed
Remove m_reader / m_builder members from AnalysisContext
Constructing these objects are very expensive compared to the cost of creating the `AnalysisContext`. `m_reader` appears to be entirely unused so it is simply removed. `m_builder` is only used within `Inform`. It may be appropriate to move it to a local variable within that function, but I'm not sure how often `Inform` is called. Given that the `Json::StreamWriterBuilder` is rather expensive to construct I've instead opted to make it static so it is initialized only on first use and then reused by later calls.
1 parent e4b71a2 commit d1b79cc

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

binaryninjaapi.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -9911,8 +9911,7 @@ namespace BinaryNinja {
99119911
class AnalysisContext :
99129912
public CoreRefCountObject<BNAnalysisContext, BNNewAnalysisContextReference, BNFreeAnalysisContext>
99139913
{
9914-
std::unique_ptr<Json::CharReader> m_reader;
9915-
Json::StreamWriterBuilder m_builder;
9914+
static Json::StreamWriterBuilder& JsonBuilder();
99169915

99179916
public:
99189917
AnalysisContext(BNAnalysisContext* analysisContext);
@@ -9996,7 +9995,7 @@ namespace BinaryNinja {
99969995
}},
99979996
arg);
99989997

9999-
return Inform(Json::writeString(m_builder, request));
9998+
return Inform(Json::writeString(JsonBuilder(), request));
100009999
}
1000110000
#endif
1000210001
};

workflow.cpp

+12-3
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,21 @@ using namespace BinaryNinja;
88
using namespace std;
99

1010

11-
AnalysisContext::AnalysisContext(BNAnalysisContext* analysisContext) :
12-
m_reader(Json::CharReaderBuilder().newCharReader())
11+
// static
12+
Json::StreamWriterBuilder& AnalysisContext::JsonBuilder()
13+
{
14+
static Json::StreamWriterBuilder* builder = [] {
15+
auto builder = new Json::StreamWriterBuilder;
16+
(*builder)["indentation"] = "";
17+
return builder;
18+
}();
19+
return *builder;
20+
}
21+
22+
AnalysisContext::AnalysisContext(BNAnalysisContext* analysisContext)
1323
{
1424
// LogError("API-Side AnalysisContext Constructed!");
1525
m_object = analysisContext;
16-
m_builder["indentation"] = "";
1726
}
1827

1928

0 commit comments

Comments
 (0)