Skip to content

Commit 9c0ec48

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 a45466f commit 9c0ec48

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
@@ -9952,8 +9952,7 @@ namespace BinaryNinja {
99529952
class AnalysisContext :
99539953
public CoreRefCountObject<BNAnalysisContext, BNNewAnalysisContextReference, BNFreeAnalysisContext>
99549954
{
9955-
std::unique_ptr<Json::CharReader> m_reader;
9956-
Json::StreamWriterBuilder m_builder;
9955+
static Json::StreamWriterBuilder& JsonBuilder();
99579956

99589957
public:
99599958
AnalysisContext(BNAnalysisContext* analysisContext);
@@ -10037,7 +10036,7 @@ namespace BinaryNinja {
1003710036
}},
1003810037
arg);
1003910038

10040-
return Inform(Json::writeString(m_builder, request));
10039+
return Inform(Json::writeString(JsonBuilder(), request));
1004110040
}
1004210041
#endif
1004310042
};

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)