Skip to content

Conversation

@TimWolla
Copy link
Contributor

For the following test script:

<?php

require('vendor/autoload.php');

for ($i = 0; $i < 10000000; $i++) {
        \ParagonIE\ConstantTime\Hex::encode('foo');
}

Hyperfine reports:

hyperfine 'php -d opcache.enable_cli=1 constant_time_encoding/test.php' 'php -d opcache.enable_cli=1 constant_time_encoding_before/test.php'
Benchmark 1: php -d opcache.enable_cli=1 constant_time_encoding/test.php
  Time (mean ± σ):     423.1 ms ±  19.6 ms    [User: 405.6 ms, System: 17.0 ms]
  Range (min … max):   403.7 ms … 475.7 ms    10 runs
 
Benchmark 2: php -d opcache.enable_cli=1 constant_time_encoding_before/test.php
  Time (mean ± σ):     745.7 ms ±  10.3 ms    [User: 725.9 ms, System: 18.8 ms]
  Range (min … max):   731.3 ms … 759.1 ms    10 runs
 
Summary
  php -d opcache.enable_cli=1 constant_time_encoding/test.php ran
    1.76 ± 0.09 times faster than php -d opcache.enable_cli=1 constant_time_encoding_before/test.php

This allows OPcache to elide the condition at compile-time if it knows that
ext/sodium is or is not available.
Similarly this allows OPcache to inline the constants.
This avoids a lookup to see if the functions exist in the
`ParagonIE\ConstantTime` namespace and ensures that there is no “userland” fake
implementation taking precedence.
@paragonie-security
Copy link
Contributor

Good catch. Does a use statement accomplish the same perf benefit as the \ prefix?

@TimWolla
Copy link
Contributor Author

TimWolla commented Sep 23, 2025

Does a use statement accomplish the same perf benefit as the \ prefix?

Yes.

I have opted for the \ prefix, since that is what is done for all the other functions (e.g. \substr()) already.

@paragonie-security paragonie-security merged commit 7bb257c into paragonie:master Sep 24, 2025
14 checks passed
@TimWolla TimWolla deleted the improve-sodium-check branch October 5, 2025 12:13
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.

4 participants