Skip to content

Commit 2f13d73

Browse files
committed
[Translator] Improve performance, cache $this->message->length() to prevent unnecessary computations
1 parent b582090 commit 2f13d73

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/Translator/src/Intl/IntlMessageParser.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
class IntlMessageParser
2424
{
2525
private AbstractString $message;
26+
// Minor optimization, this avoid a lot of calls to `$this->message->length()`
27+
private int $messageLength;
28+
2629
private Position $position;
2730
private bool $ignoreTag;
2831
private bool $requiresOtherClause;
@@ -31,6 +34,7 @@ public function __construct(
3134
string $message,
3235
) {
3336
$this->message = s($message);
37+
$this->messageLength = $this->message->length();
3438
$this->position = new Position(0, 1, 1);
3539
$this->ignoreTag = true;
3640
$this->requiresOtherClause = true;
@@ -868,7 +872,7 @@ private function tryParseDecimalInteger(
868872

869873
private function isEOF(): bool
870874
{
871-
return $this->position->offset === $this->message->length();
875+
return $this->position->offset === $this->messageLength;
872876
}
873877

874878
/**
@@ -880,7 +884,7 @@ private function isEOF(): bool
880884
private function char(): int
881885
{
882886
$offset = $this->position->offset;
883-
if ($offset >= $this->message->length()) {
887+
if ($offset >= $this->messageLength) {
884888
throw new \OutOfBoundsException();
885889
}
886890

@@ -959,7 +963,7 @@ private function bumpUntil(string $pattern): bool
959963

960964
return true;
961965
} else {
962-
$this->bumpTo($this->message->length());
966+
$this->bumpTo($this->messageLength);
963967

964968
return false;
965969
}
@@ -977,7 +981,7 @@ private function bumpTo(int $targetOffset)
977981
throw new \Exception(\sprintf('targetOffset %s must be greater than or equal to the current offset %d', $targetOffset, $this->position->offset));
978982
}
979983

980-
$targetOffset = min($targetOffset, $this->message->length());
984+
$targetOffset = min($targetOffset, $this->messageLength);
981985
while (true) {
982986
$offset = $this->position->offset;
983987
if ($offset === $targetOffset) {

0 commit comments

Comments
 (0)