Commit 76c008f
committed
main/streams/streams: optimized code path for unfiltered regular files
If we know the exact file size already, we can allocate the right
buffer size and issue only one read() system call. This eliminates
unnecessary buffer allocations (file size plus 8 kB), eliminates
buffer reallocations via zend_string_truncate(), and eliminates the
last read() system call.
Benchmark:
echo Hello World >/tmp/hello.txt
<?php for ($i = 1; $i <= 1000000; $i++) file_get_contents('/tmp/hello.txt');
Previously:
openat(AT_FDCWD, "/tmp/hello.txt", O_RDONLY) = 3
newfstatat(3, "", {st_mode=S_IFREG|0600, st_size=12, ...}, AT_EMPTY_PATH) = 0
lseek(3, 0, SEEK_CUR) = 0
newfstatat(3, "", {st_mode=S_IFREG|0600, st_size=12, ...}, AT_EMPTY_PATH) = 0
read(3, "Hello World\n", 8204) = 12
read(3, "", 8192) = 0
close(3) = 0
4,036.55 msec task-clock:u # 0.995 CPUs utilized
4,683,289,286 cycles:u # 1.160 GHz
7,954,269,313 instructions:u # 1.70 insn per cycle
1,165,461,174 branches:u # 288.727 M/sec
1,431,759 branch-misses:u # 0.12% of all branches
Optimized:
openat(AT_FDCWD, "/tmp/hello.txt", O_RDONLY) = 3
newfstatat(3, "", {st_mode=S_IFREG|0600, st_size=12, ...}, AT_EMPTY_PATH) = 0
lseek(3, 0, SEEK_CUR) = 0
newfstatat(3, "", {st_mode=S_IFREG|0600, st_size=12, ...}, AT_EMPTY_PATH) = 0
read(3, "Hello World\n", 12) = 12
close(3) = 0
3,473.51 msec task-clock:u # 0.997 CPUs utilized
4,132,407,666 cycles:u # 1.190 GHz
6,841,273,246 instructions:u # 1.66 insn per cycle
1,024,464,752 branches:u # 294.936 M/sec
1,478,403 branch-misses:u # 0.14% of all branches
For this micro-benchmark, the improvement is 14%.
This patch needs to adjust the unit test "bug54946.phpt" because the
EBADF warning is no longer logged.1 parent 7879353 commit 76c008f
File tree
2 files changed
+20
-5
lines changed- ext/standard/tests/streams
- main/streams
2 files changed
+20
-5
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
34 | | - | |
35 | 34 | | |
36 | | - | |
37 | | - | |
38 | 35 | | |
39 | | - | |
40 | | - | |
41 | 36 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1474 | 1474 | | |
1475 | 1475 | | |
1476 | 1476 | | |
| 1477 | + | |
| 1478 | + | |
| 1479 | + | |
| 1480 | + | |
| 1481 | + | |
| 1482 | + | |
| 1483 | + | |
| 1484 | + | |
| 1485 | + | |
| 1486 | + | |
| 1487 | + | |
| 1488 | + | |
| 1489 | + | |
| 1490 | + | |
| 1491 | + | |
| 1492 | + | |
| 1493 | + | |
| 1494 | + | |
| 1495 | + | |
| 1496 | + | |
1477 | 1497 | | |
1478 | 1498 | | |
1479 | 1499 | | |
| |||
0 commit comments