Skip to content

Commit 01d3545

Browse files
committed
OutputFormatter space cache
1 parent 4745d6f commit 01d3545

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/OutputFormatter.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ class OutputFormatter
1212
*/
1313
private $oFormat;
1414

15+
/**
16+
* @var array<string, string>
17+
*/
18+
private $preparedSpacesCache = [];
19+
1520
public function __construct(OutputFormat $oFormat)
1621
{
1722
$this->oFormat = $oFormat;
@@ -241,7 +246,13 @@ public function comments(Commentable $oCommentable)
241246
*/
242247
private function prepareSpace($sSpaceString)
243248
{
244-
return str_replace("\n", "\n" . $this->indent(), $sSpaceString);
249+
$cacheKey = $this->oFormat->level() . $sSpaceString;
250+
251+
if (isset($this->preparedSpacesCache[$cacheKey])) {
252+
return $this->preparedSpacesCache[$cacheKey];
253+
}
254+
255+
return $this->preparedSpacesCache[$cacheKey] = str_replace("\n", "\n" . $this->indent(), $sSpaceString);
245256
}
246257

247258
/**

0 commit comments

Comments
 (0)