Skip to content

Conversation

gharlan
Copy link
Contributor

@gharlan gharlan commented Jul 6, 2025

ShortUuid::encode can be called quite often when having many urls on a page.

On a random page in my project this pr gives this improvement (measured via blackfire):

{
$this->alphabet = $alphabet;
$this->alphabetLength = count($alphabet);
$this->alphabetLength = BigInteger::of(count($alphabet));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

brick/math converts this value in all operations into BigInteger objects. Now we are doing it here once, so it does not have to be converted again and again.

$previousNumber = clone $number;
$number = $number->dividedBy($this->alphabetLength, RoundingMode::DOWN);
$digit = $previousNumber->mod($this->alphabetLength);
[$number, $digit] = $number->quotientAndRemainder($this->alphabetLength);
Copy link
Contributor Author

@gharlan gharlan Jul 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.. but this has the bigger impact on the performance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant