Commit 559858c
authored
Improve performance of unpack() with nameless repetitions (#18803)
We can avoid creating temporary strings, and then reparsing them into
numbers with zend_symtable_update() by using zend_hash_index_update()
directly.
For the following benchmark on an i7-4790:
```php
$file = str_repeat('A', 100000);
for ($i=0;$i<100;$i++) unpack('C*',$file);
```
I get:
```
Benchmark 1: ./sapi/cli/php y.php
Time (mean ± σ): 85.8 ms ± 1.8 ms [User: 74.5 ms, System: 10.4 ms]
Range (min … max): 83.8 ms … 92.4 ms 33 runs
Benchmark 2: ./sapi/cli/php_old y.php
Time (mean ± σ): 318.3 ms ± 2.7 ms [User: 306.7 ms, System: 9.9 ms]
Range (min … max): 314.9 ms … 321.6 ms 10 runs
Summary
./sapi/cli/php y.php ran
3.71 ± 0.08 times faster than ./sapi/cli/php_old y.php
```
On an i7-1185G7 I get:
```
Benchmark 1: ./sapi/cli/php test.php
Time (mean ± σ): 60.1 ms ± 0.7 ms [User: 47.8 ms, System: 12.0 ms]
Range (min … max): 59.2 ms … 63.8 ms 48 runs
Warning: Statistical outliers were detected. Consider re-running this benchmark on a quiet system without any interferences from other programs. It might help to use the '--warmup' or '--prepare' options.
Benchmark 2: ./sapi/cli/php_old test.php
Time (mean ± σ): 220.8 ms ± 2.2 ms [User: 209.6 ms, System: 10.7 ms]
Range (min … max): 218.5 ms … 224.5 ms 13 runs
Summary
./sapi/cli/php test.php ran
3.67 ± 0.06 times faster than ./sapi/cli/php_old test.php
```1 parent 0a95b2f commit 559858c
2 files changed
+23
-19
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
582 | 582 | | |
583 | 583 | | |
584 | 584 | | |
| 585 | + | |
| 586 | + | |
585 | 587 | | |
586 | 588 | | |
587 | 589 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
885 | 885 | | |
886 | 886 | | |
887 | 887 | | |
| 888 | + | |
888 | 889 | | |
889 | 890 | | |
890 | | - | |
| 891 | + | |
| 892 | + | |
| 893 | + | |
| 894 | + | |
891 | 895 | | |
892 | 896 | | |
893 | | - | |
894 | 897 | | |
895 | 898 | | |
896 | 899 | | |
| |||
912 | 915 | | |
913 | 916 | | |
914 | 917 | | |
915 | | - | |
916 | 918 | | |
917 | 919 | | |
918 | 920 | | |
| |||
939 | 941 | | |
940 | 942 | | |
941 | 943 | | |
942 | | - | |
943 | 944 | | |
944 | 945 | | |
945 | 946 | | |
| |||
964 | 965 | | |
965 | 966 | | |
966 | 967 | | |
967 | | - | |
968 | 968 | | |
969 | 969 | | |
970 | 970 | | |
| |||
979 | 979 | | |
980 | 980 | | |
981 | 981 | | |
982 | | - | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
983 | 985 | | |
984 | 986 | | |
985 | 987 | | |
| |||
1016 | 1018 | | |
1017 | 1019 | | |
1018 | 1020 | | |
1019 | | - | |
1020 | 1021 | | |
1021 | 1022 | | |
1022 | 1023 | | |
| |||
1026 | 1027 | | |
1027 | 1028 | | |
1028 | 1029 | | |
1029 | | - | |
1030 | 1030 | | |
1031 | 1031 | | |
1032 | 1032 | | |
| |||
1046 | 1046 | | |
1047 | 1047 | | |
1048 | 1048 | | |
1049 | | - | |
1050 | 1049 | | |
1051 | 1050 | | |
1052 | 1051 | | |
| |||
1062 | 1061 | | |
1063 | 1062 | | |
1064 | 1063 | | |
1065 | | - | |
1066 | 1064 | | |
1067 | 1065 | | |
1068 | 1066 | | |
| |||
1082 | 1080 | | |
1083 | 1081 | | |
1084 | 1082 | | |
1085 | | - | |
1086 | | - | |
1087 | 1083 | | |
1088 | 1084 | | |
1089 | 1085 | | |
| |||
1104 | 1100 | | |
1105 | 1101 | | |
1106 | 1102 | | |
1107 | | - | |
1108 | 1103 | | |
1109 | 1104 | | |
1110 | 1105 | | |
| |||
1124 | 1119 | | |
1125 | 1120 | | |
1126 | 1121 | | |
1127 | | - | |
1128 | 1122 | | |
1129 | 1123 | | |
1130 | 1124 | | |
| |||
1143 | 1137 | | |
1144 | 1138 | | |
1145 | 1139 | | |
1146 | | - | |
1147 | 1140 | | |
1148 | 1141 | | |
1149 | 1142 | | |
1150 | 1143 | | |
1151 | 1144 | | |
1152 | | - | |
| 1145 | + | |
1153 | 1146 | | |
1154 | 1147 | | |
1155 | 1148 | | |
| |||
1160 | 1153 | | |
1161 | 1154 | | |
1162 | 1155 | | |
1163 | | - | |
| 1156 | + | |
1164 | 1157 | | |
1165 | 1158 | | |
1166 | 1159 | | |
| |||
1170 | 1163 | | |
1171 | 1164 | | |
1172 | 1165 | | |
1173 | | - | |
| 1166 | + | |
1174 | 1167 | | |
1175 | 1168 | | |
1176 | | - | |
| 1169 | + | |
| 1170 | + | |
| 1171 | + | |
| 1172 | + | |
| 1173 | + | |
| 1174 | + | |
| 1175 | + | |
| 1176 | + | |
| 1177 | + | |
| 1178 | + | |
1177 | 1179 | | |
1178 | 1180 | | |
1179 | 1181 | | |
| |||
0 commit comments