Skip to content

Commit

Permalink
More micro-optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
colinodell committed Jul 3, 2021
1 parent 5c01d73 commit cf2261d
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 34 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Updates should follow the [Keep a CHANGELOG](https://keepachangelog.com/) princi
- Any leading UTF-8 BOM will be stripped from the input
- Mark the `getEnvironment()` method of `CommonMarkConverter` and `GithubFlavoredMarkdownConverter` as always returning the concrete, configurable `Environment` for upgrading convenience
- Optimized AST iteration
- Lots of small micro-optimizations

## [2.0.0-beta2]

Expand Down
5 changes: 4 additions & 1 deletion src/Extension/CommonMark/Parser/Block/FencedCodeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,10 @@ public function tryContinue(Cursor $cursor, BlockContinueParserInterface $active
}

// Skip optional spaces of fence offset
$cursor->match('/^ {0,' . $this->block->getOffset() . '}/');
// Optimization: don't attempt to match if we're at a non-space position
if ($cursor->getNextNonSpacePosition() > $cursor->getPosition()) {
$cursor->match('/^ {0,' . $this->block->getOffset() . '}/');
}

return BlockContinue::at($cursor);
}
Expand Down
12 changes: 3 additions & 9 deletions src/Extension/CommonMark/Parser/Block/FencedCodeStartParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,7 @@ final class FencedCodeStartParser implements BlockStartParserInterface
{
public function tryStart(Cursor $cursor, MarkdownParserStateInterface $parserState): ?BlockStart
{
if ($cursor->isIndented()) {
return BlockStart::none();
}

$c = $cursor->getCurrentCharacter();
if ($c !== ' ' && $c !== "\t" && $c !== '`' && $c !== '~') {
if ($cursor->isIndented() || ! \in_array($cursor->getNextNonSpaceCharacter(), ['`', '~'], true)) {
return BlockStart::none();
}

Expand All @@ -38,9 +33,8 @@ public function tryStart(Cursor $cursor, MarkdownParserStateInterface $parserSta
}

// fenced code block
$fence = \ltrim($fence, " \t");
$fenceLength = \strlen($fence);
$fence = \ltrim($fence, " \t");

return BlockStart::of(new FencedCodeParser($fenceLength, $fence[0], $indent))->at($cursor);
return BlockStart::of(new FencedCodeParser(\strlen($fence), $fence[0], $indent))->at($cursor);
}
}
6 changes: 3 additions & 3 deletions src/Extension/CommonMark/Parser/Block/HeadingStartParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class HeadingStartParser implements BlockStartParserInterface
{
public function tryStart(Cursor $cursor, MarkdownParserStateInterface $parserState): ?BlockStart
{
if ($cursor->isIndented()) {
if ($cursor->isIndented() || ! \in_array($cursor->getNextNonSpaceCharacter(), ['#', '-', '='], true)) {
return BlockStart::none();
}

Expand All @@ -50,7 +50,7 @@ public function tryStart(Cursor $cursor, MarkdownParserStateInterface $parserSta

private static function getAtxHeader(Cursor $cursor): ?HeadingParser
{
$match = RegexHelper::matchFirst('/^#{1,6}(?:[ \t]+|$)/', $cursor->getLine(), $cursor->getNextNonSpacePosition());
$match = RegexHelper::matchFirst('/^#{1,6}(?:[ \t]+|$)/', $cursor->getRemainder());
if (! $match) {
return null;
}
Expand All @@ -70,7 +70,7 @@ private static function getAtxHeader(Cursor $cursor): ?HeadingParser

private static function getSetextHeadingLevel(Cursor $cursor): int
{
$match = RegexHelper::matchFirst('/^(?:=+|-+)[ \t]*$/', $cursor->getLine(), $cursor->getNextNonSpacePosition());
$match = RegexHelper::matchFirst('/^(?:=+|-+)[ \t]*$/', $cursor->getRemainder());
if ($match === null) {
return 0;
}
Expand Down
1 change: 1 addition & 0 deletions src/Extension/CommonMark/Parser/Block/ListItemParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ public function tryContinue(Cursor $cursor, BlockContinueParserInterface $active
return BlockContinue::at($cursor);
}

// Note: We'll hit this case for lazy continuation lines, they will get added later.
return BlockContinue::none();
}
}
12 changes: 2 additions & 10 deletions src/Parser/Cursor.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,7 @@ public function getCharacter(?int $index = null): ?string
}

if ($this->isMultibyte) {
if (isset($this->charCache[$index])) {
return $this->charCache[$index];
}

return $this->charCache[$index] = \mb_substr($this->line, $index, 1, 'UTF-8');
return $this->charCache[$index] ??= \mb_substr($this->line, $index, 1, 'UTF-8');
}

return $this->line[$index];
Expand All @@ -162,11 +158,7 @@ public function getCurrentCharacter(): ?string
}

if ($this->isMultibyte) {
if (isset($this->charCache[$this->currentPosition])) {
return $this->charCache[$this->currentPosition];
}

return $this->charCache[$this->currentPosition] = \mb_substr($this->line, $this->currentPosition, 1, 'UTF-8');
return $this->charCache[$this->currentPosition] ??= \mb_substr($this->line, $this->currentPosition, 1, 'UTF-8');
}

return $this->line[$this->currentPosition];
Expand Down
2 changes: 1 addition & 1 deletion src/Parser/InlineParserEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private function matchParsers(string $contents): array
\assert(\is_int($offset));

// Remove the offsets, keeping only the matched text
$m = \array_map(static fn (array $s): string => (string) $s[0], $match);
$m = \array_column($match, 0);

if ($m === []) {
continue;
Expand Down
6 changes: 1 addition & 5 deletions src/Reference/ReferenceMap.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,7 @@ public function get(string $label): ?ReferenceInterface
{
$label = $this->normalizer->normalize($label);

if (! isset($this->references[$label])) {
return null;
}

return $this->references[$label];
return $this->references[$label] ?? null;
}

/**
Expand Down
6 changes: 1 addition & 5 deletions src/Util/HtmlElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,7 @@ public function getAllAttributes(): array
*/
public function getAttribute(string $key)
{
if (! isset($this->attributes[$key])) {
return null;
}

return $this->attributes[$key];
return $this->attributes[$key] ?? null;
}

/**
Expand Down

0 comments on commit cf2261d

Please sign in to comment.