Skip to content

Commit 082a987

Browse files
committed
[SourceKit] Reserve 4KB for the syntax tree buffer
Heuristics have shown that this prevents several new mallocs and speeds incremental syntax tree transfer up.
1 parent 15b1559 commit 082a987

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

tools/SourceKit/tools/sourcekitd/lib/API/Requests.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2423,9 +2423,14 @@ void serializeSyntaxTreeAsJson(
24232423
const swift::syntax::SourceFileSyntax &SyntaxTree,
24242424
std::unordered_set<unsigned> ReusedNodeIds,
24252425
ResponseBuilder::Dictionary &Dict) {
2426+
// 4096 is a heuristic buffer size that appears to usually be able to fit an
2427+
// incremental syntax tree
2428+
size_t ReserveBufferSize = 4096;
24262429
std::string SyntaxTreeString;
2430+
SyntaxTreeString.reserve(ReserveBufferSize);
24272431
{
24282432
llvm::raw_string_ostream SyntaxTreeStream(SyntaxTreeString);
2433+
SyntaxTreeStream.SetBufferSize(ReserveBufferSize);
24292434
swift::json::Output::UserInfoMap JsonUserInfo;
24302435
JsonUserInfo[swift::json::OmitNodesUserInfoKey] = &ReusedNodeIds;
24312436
swift::json::Output SyntaxTreeOutput(SyntaxTreeStream, JsonUserInfo,

0 commit comments

Comments
 (0)