Skip to content

Commit e9fdcd7

Browse files
CST: Use a buffer to print instead of string concatenation (#300)
Using a buffer to write string is faster than standard string concatenation
1 parent b4db59b commit e9fdcd7

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

batteries/syntax/printer.luau

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,30 +72,51 @@ local function printInterpolatedString(expr: luau.AstExprInterpString): string
7272
end
7373

7474
type PrintVisitor = visitor.Visitor & {
75-
result: string,
75+
result: buffer,
76+
cursor: number,
7677
}
7778

7879
local function printVisitor()
7980
local printer = visitor.createVisitor() :: PrintVisitor
80-
printer.result = ""
81+
82+
printer.result = buffer.create(1024)
83+
printer.cursor = 0
84+
85+
local function write(str: string)
86+
local totalSize = printer.cursor + #str
87+
local bufferSize = buffer.len(printer.result)
88+
89+
if totalSize >= bufferSize then
90+
repeat
91+
bufferSize *= 2
92+
until bufferSize >= totalSize
93+
94+
local newBuffer = buffer.create(bufferSize)
95+
buffer.copy(newBuffer, 0, printer.result)
96+
printer.result = newBuffer
97+
end
98+
99+
buffer.writestring(printer.result, printer.cursor, str)
100+
printer.cursor = totalSize
101+
end
81102

82103
printer.visitToken = function(node: luau.Token)
83-
printer.result ..= printToken(node)
104+
write(printToken(node))
84105
return false
85106
end
86107

87108
printer.visitString = function(node: luau.AstExprConstantString)
88-
printer.result ..= printString(node)
109+
write(printString(node))
89110
return false
90111
end
91112

92113
printer.visitTypeString = function(node: luau.AstTypeSingletonString)
93-
printer.result ..= printString(node)
114+
write(printString(node))
94115
return false
95116
end
96117

97118
printer.visitInterpolatedString = function(node: luau.AstExprInterpString)
98-
printer.result ..= printInterpolatedString(node)
119+
write(printInterpolatedString(node))
99120
return false
100121
end
101122

@@ -106,21 +127,21 @@ end
106127
local function printBlock(block: luau.AstStatBlock): string
107128
local printer = printVisitor()
108129
visitor.visitBlock(block, printer)
109-
return printer.result
130+
return buffer.readstring(printer.result, 0, printer.cursor)
110131
end
111132

112133
--- Returns a string representation of an AstExpr
113134
function printExpr(block: luau.AstExpr): string
114135
local printer = printVisitor()
115136
visitor.visitExpression(block, printer)
116-
return printer.result
137+
return buffer.readstring(printer.result, 0, printer.cursor)
117138
end
118139

119140
function printFile(result: { root: luau.AstStatBlock, eof: luau.Eof }): string
120141
local printer = printVisitor()
121142
visitor.visitBlock(result.root, printer)
122143
visitor.visitToken(result.eof, printer)
123-
return printer.result
144+
return buffer.readstring(printer.result, 0, printer.cursor)
124145
end
125146

126147
return {

0 commit comments

Comments
 (0)