Skip to content

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).

@ruudk ruudk force-pushed the optimize-stripcslashes branch from 377865e to bb04d84 Compare August 12, 2024 07:21
@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 fabpot force-pushed the optimize-stripcslashes branch from 6f7a043 to bc6e36d Compare August 12, 2024 07:25
@fabpot
Copy link
Contributor

fabpot commented Aug 12, 2024

Thank you @ruudk.

@fabpot fabpot merged commit 93e66ad into twigphp:3.x Aug 12, 2024
@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