Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize stripcslashes #4196

Merged
merged 1 commit into from
Aug 12, 2024
Merged

Optimize stripcslashes #4196

merged 1 commit into from
Aug 12, 2024

Conversation

ruudk
Copy link
Contributor

@ruudk ruudk commented Aug 12, 2024

See #4176 (comment) by @smnandre

# Old
Benchmark 1: php bench.php
  Time (mean ± σ):      1.192 s ±  0.004 s    [User: 1.163 s, System: 0.009 s]
  Range (min … max):    1.186 s …  1.198 s    10 runs

# New
Benchmark 1: php bench.php
  Time (mean ± σ):     356.5 ms ±   1.1 ms    [User: 342.0 ms, System: 7.6 ms]
  Range (min … max):   354.6 ms … 358.2 ms    10 runs

Using bench.php:

<?php

require_once __DIR__.'/vendor/autoload.php';

$string = str_repeat("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut a tincidunt turpis. \\App\\Entity\\Product \World \065 App\#{var} \'quoted\' Donec pharetra enim quis erat pharetra, dignissim molestie erat laoreet. Vivamus auctor purus sed lorem vestibulum rhoncus.", 10);
$loader = new Twig\Loader\ArrayLoader([
    'index.twig' => <<<EOF
{{ "$string" }}
EOF
    ,
]);

$twig = new Twig\Environment($loader);

for ($i = 0; $i < 10000; $i++) {
    $twig->tokenize(new Twig\Source($twig->getLoader()->getSourceContext('index.twig')->getCode(), 'index.twig'));
}

Copy link
Contributor

@fabpot fabpot left a comment

Choose a reason for hiding this comment

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

I suppose that we can remove the str_contains as it's not needed anymore (the check at the beginning of the while is equivalent).

src/Lexer.php Outdated Show resolved Hide resolved
@ruudk
Copy link
Contributor Author

ruudk commented Aug 12, 2024

I suppose that we can remove the str_contains as it's not needed anymore (the check at the beginning of the while is equivalent).

@fabpot Done + updated the bench

# Old
Benchmark 1: php bench.php
  Time (mean ± σ):      1.204 s ±  0.012 s    [User: 1.185 s, System: 0.011 s]
  Range (min … max):    1.191 s …  1.225 s    10 runs

# New
Benchmark 1: php bench.php
  Time (mean ± σ):     368.3 ms ±   4.8 ms    [User: 354.2 ms, System: 10.1 ms]
  Range (min … max):   362.9 ms … 375.6 ms    10 runs

@smnandre
Copy link
Contributor

Nice & thank you :)

@fabpot
Copy link
Contributor

fabpot commented Aug 12, 2024

Thank you @ruudk.

@fabpot fabpot merged commit 93e66ad into twigphp:3.x Aug 12, 2024
8 checks passed
@ruudk ruudk deleted the optimize-stripcslashes branch August 12, 2024 07:25
@ruudk
Copy link
Contributor Author

ruudk commented Aug 12, 2024

@fabpot Could you sync this back to v4 so that I can continue?

@fabpot
Copy link
Contributor

fabpot commented Aug 12, 2024

@fabpot Could you sync this back to v4 so that I can continue?

I've just merged 3.x into 4.x.

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

Successfully merging this pull request may close these issues.

3 participants