File: getBytesFromString_fast_path.phpt

package info (click to toggle)
php8.4 8.4.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 208,108 kB
  • sloc: ansic: 1,060,628; php: 35,345; sh: 11,866; cpp: 7,201; pascal: 4,913; javascript: 3,091; asm: 2,810; yacc: 2,411; makefile: 689; xml: 446; python: 301; awk: 148
file content (162 lines) | stat: -rw-r--r-- 3,020 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
--TEST--
Random: Randomizer: getBytesFromString(): Fast Path Masking
--FILE--
<?php

use Random\Engine\Test\TestWrapperEngine;
use Random\Engine\Xoshiro256StarStar;
use Random\Randomizer;

require __DIR__ . "/../../engines.inc";

$allBytes = implode('', array_map(
    fn ($byte) => chr($byte),
    range(0x00, 0xff)
));

// Xoshiro256** is the fastest engine available.
$xoshiro = new Xoshiro256StarStar();

var_dump(strlen($allBytes));
echo PHP_EOL;

// Fast path: Inputs less than or equal to 256.
for ($i = 1; $i <= strlen($allBytes); $i *= 2) {
    echo "{$i}:", PHP_EOL;

    $wrapper = new TestWrapperEngine($xoshiro);
    $r = new Randomizer($wrapper);
    $result = $r->getBytesFromString(substr($allBytes, 0, $i), 20000);

    // Xoshiro256** is a 64 Bit engine and thus generates 8 bytes at once.
    // For powers of two we expect no rejections and thus exactly
    // 20000/8 = 2500 calls to the engine.
    var_dump($wrapper->getCount());

    $count = [];
    for ($j = 0; $j < strlen($result); $j++) {
        $b = $result[$j];
        $count[ord($b)] ??= 0;
        $count[ord($b)]++;
    }

    // We also expect that each possible value appears at least once, if
    // not is is very likely that some bits were erroneously masked away.
    var_dump(count($count));

    echo PHP_EOL;
}

// Test lengths that are one more than the powers of two. For these
// the maximum offset will be a power of two and thus a minimal number
// of bits will be set in the offset.
for ($i = 1; ($i + 1) <= strlen($allBytes); $i *= 2) {
    $oneMore = $i + 1;

    echo "{$oneMore}:", PHP_EOL;

    $wrapper = new TestWrapperEngine($xoshiro);
    $r = new Randomizer($wrapper);
    $result = $r->getBytesFromString(substr($allBytes, 0, $oneMore), 20000);

    $count = [];
    for ($j = 0; $j < strlen($result); $j++) {
        $b = $result[$j];
        $count[ord($b)] ??= 0;
        $count[ord($b)]++;
    }

    // We expect that each possible value appears at least once, if
    // not is is very likely that some bits were erroneously masked away.
    var_dump(count($count));

    echo PHP_EOL;
}

echo "Slow Path:", PHP_EOL;

$wrapper = new TestWrapperEngine($xoshiro);
$r = new Randomizer($wrapper);
$result = $r->getBytesFromString($allBytes . $allBytes, 20000);

// In the slow path we expect one call per byte, i.e. 20000
var_dump($wrapper->getCount());

$count = [];
for ($j = 0; $j < strlen($result); $j++) {
    $b = $result[$j];
    $count[ord($b)] ??= 0;
    $count[ord($b)]++;
}

// We also expect that each possible value appears at least once, if
// not is is very likely that some bits were erroneously masked away.
var_dump(count($count));

?>
--EXPECT--
int(256)

1:
int(2500)
int(1)

2:
int(2500)
int(2)

4:
int(2500)
int(4)

8:
int(2500)
int(8)

16:
int(2500)
int(16)

32:
int(2500)
int(32)

64:
int(2500)
int(64)

128:
int(2500)
int(128)

256:
int(2500)
int(256)

2:
int(2)

3:
int(3)

5:
int(5)

9:
int(9)

17:
int(17)

33:
int(33)

65:
int(65)

129:
int(129)

Slow Path:
int(20000)
int(256)