Skip to content

Commit 121abe0

Browse files
authored
Merge pull request #330 from WyriHaximus-secret-labs/function-name-look-up-performance-improvement
Improve performance by prefixing all global functions calls with \ to skip the look up and resolve process and go straight to the global function
2 parents 9d6e4cd + 91e0200 commit 121abe0

14 files changed

+114
-114
lines changed

src/Io/ChunkedDecoder.php

+15-15
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function handleData($data)
9797

9898
while ($this->buffer !== '') {
9999
if (!$this->headerCompleted) {
100-
$positionCrlf = strpos($this->buffer, static::CRLF);
100+
$positionCrlf = \strpos($this->buffer, static::CRLF);
101101

102102
if ($positionCrlf === false) {
103103
// Header shouldn't be bigger than 1024 bytes
@@ -107,43 +107,43 @@ public function handleData($data)
107107
return;
108108
}
109109

110-
$header = strtolower((string)substr($this->buffer, 0, $positionCrlf));
110+
$header = \strtolower((string)\substr($this->buffer, 0, $positionCrlf));
111111
$hexValue = $header;
112112

113-
if (strpos($header, ';') !== false) {
114-
$array = explode(';', $header);
113+
if (\strpos($header, ';') !== false) {
114+
$array = \explode(';', $header);
115115
$hexValue = $array[0];
116116
}
117117

118118
if ($hexValue !== '') {
119-
$hexValue = ltrim($hexValue, "0");
119+
$hexValue = \ltrim($hexValue, "0");
120120
if ($hexValue === '') {
121121
$hexValue = "0";
122122
}
123123
}
124124

125-
$this->chunkSize = hexdec($hexValue);
126-
if (dechex($this->chunkSize) !== $hexValue) {
125+
$this->chunkSize = \hexdec($hexValue);
126+
if (\dechex($this->chunkSize) !== $hexValue) {
127127
$this->handleError(new Exception($hexValue . ' is not a valid hexadecimal number'));
128128
return;
129129
}
130130

131-
$this->buffer = (string)substr($this->buffer, $positionCrlf + 2);
131+
$this->buffer = (string)\substr($this->buffer, $positionCrlf + 2);
132132
$this->headerCompleted = true;
133133
if ($this->buffer === '') {
134134
return;
135135
}
136136
}
137137

138-
$chunk = (string)substr($this->buffer, 0, $this->chunkSize - $this->transferredSize);
138+
$chunk = (string)\substr($this->buffer, 0, $this->chunkSize - $this->transferredSize);
139139

140140
if ($chunk !== '') {
141-
$this->transferredSize += strlen($chunk);
141+
$this->transferredSize += \strlen($chunk);
142142
$this->emit('data', array($chunk));
143-
$this->buffer = (string)substr($this->buffer, strlen($chunk));
143+
$this->buffer = (string)\substr($this->buffer, \strlen($chunk));
144144
}
145145

146-
$positionCrlf = strpos($this->buffer, static::CRLF);
146+
$positionCrlf = \strpos($this->buffer, static::CRLF);
147147

148148
if ($positionCrlf === 0) {
149149
if ($this->chunkSize === 0) {
@@ -154,16 +154,16 @@ public function handleData($data)
154154
$this->chunkSize = 0;
155155
$this->headerCompleted = false;
156156
$this->transferredSize = 0;
157-
$this->buffer = (string)substr($this->buffer, 2);
157+
$this->buffer = (string)\substr($this->buffer, 2);
158158
}
159159

160-
if ($positionCrlf !== 0 && $this->chunkSize === $this->transferredSize && strlen($this->buffer) > 2) {
160+
if ($positionCrlf !== 0 && $this->chunkSize === $this->transferredSize && \strlen($this->buffer) > 2) {
161161
// the first 2 characters are not CLRF, send error event
162162
$this->handleError(new Exception('Chunk does not end with a CLRF'));
163163
return;
164164
}
165165

166-
if ($positionCrlf !== 0 && strlen($this->buffer) < 2) {
166+
if ($positionCrlf !== 0 && \strlen($this->buffer) < 2) {
167167
// No CLRF found, wait for additional data which could be a CLRF
168168
return;
169169
}

src/Io/ChunkedEncoder.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public function handleEnd()
101101
*/
102102
private function createChunk($data)
103103
{
104-
$byteSize = dechex(strlen($data));
104+
$byteSize = \dechex(\strlen($data));
105105
$chunkBeginning = $byteSize . "\r\n";
106106

107107
return $chunkBeginning . $data . "\r\n";

src/Io/IniUtil.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@ final class IniUtil
1515
*/
1616
public static function iniSizeToBytes($size)
1717
{
18-
if (is_numeric($size)) {
18+
if (\is_numeric($size)) {
1919
return (int)$size;
2020
}
2121

22-
$suffix = strtoupper(substr($size, -1));
23-
$strippedSize = substr($size, 0, -1);
22+
$suffix = \strtoupper(\substr($size, -1));
23+
$strippedSize = \substr($size, 0, -1);
2424

25-
if (!is_numeric($strippedSize)) {
25+
if (!\is_numeric($strippedSize)) {
2626
throw new \InvalidArgumentException("$size is not a valid ini size");
2727
}
2828

src/Io/LengthLimitedStream.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,13 @@ public function close()
7272
/** @internal */
7373
public function handleData($data)
7474
{
75-
if (($this->transferredLength + strlen($data)) > $this->maxLength) {
75+
if (($this->transferredLength + \strlen($data)) > $this->maxLength) {
7676
// Only emit data until the value of 'Content-Length' is reached, the rest will be ignored
77-
$data = (string)substr($data, 0, $this->maxLength - $this->transferredLength);
77+
$data = (string)\substr($data, 0, $this->maxLength - $this->transferredLength);
7878
}
7979

8080
if ($data !== '') {
81-
$this->transferredLength += strlen($data);
81+
$this->transferredLength += \strlen($data);
8282
$this->emit('data', array($data));
8383
}
8484

src/Io/MiddlewareRunner.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ final class MiddlewareRunner
2323
*/
2424
public function __construct(array $middleware)
2525
{
26-
$this->middleware = array_values($middleware);
26+
$this->middleware = \array_values($middleware);
2727
}
2828

2929
/**

src/Io/MultipartParser.php

+30-30
Original file line numberDiff line numberDiff line change
@@ -73,27 +73,27 @@ final class MultipartParser
7373
*/
7474
public function __construct($uploadMaxFilesize = null, $maxFileUploads = null)
7575
{
76-
$var = ini_get('max_input_vars');
76+
$var = \ini_get('max_input_vars');
7777
if ($var !== false) {
7878
$this->maxInputVars = (int)$var;
7979
}
80-
$var = ini_get('max_input_nesting_level');
80+
$var = \ini_get('max_input_nesting_level');
8181
if ($var !== false) {
8282
$this->maxInputNestingLevel = (int)$var;
8383
}
8484

8585
if ($uploadMaxFilesize === null) {
86-
$uploadMaxFilesize = ini_get('upload_max_filesize');
86+
$uploadMaxFilesize = \ini_get('upload_max_filesize');
8787
}
8888

8989
$this->uploadMaxFilesize = IniUtil::iniSizeToBytes($uploadMaxFilesize);
90-
$this->maxFileUploads = $maxFileUploads === null ? (ini_get('file_uploads') === '' ? 0 : (int)ini_get('max_file_uploads')) : (int)$maxFileUploads;
90+
$this->maxFileUploads = $maxFileUploads === null ? (\ini_get('file_uploads') === '' ? 0 : (int)\ini_get('max_file_uploads')) : (int)$maxFileUploads;
9191
}
9292

9393
public function parse(ServerRequestInterface $request)
9494
{
9595
$contentType = $request->getHeaderLine('content-type');
96-
if(!preg_match('/boundary="?(.*)"?$/', $contentType, $matches)) {
96+
if(!\preg_match('/boundary="?(.*)"?$/', $contentType, $matches)) {
9797
return $request;
9898
}
9999

@@ -112,35 +112,35 @@ public function parse(ServerRequestInterface $request)
112112

113113
private function parseBody($boundary, $buffer)
114114
{
115-
$len = strlen($boundary);
115+
$len = \strlen($boundary);
116116

117117
// ignore everything before initial boundary (SHOULD be empty)
118-
$start = strpos($buffer, $boundary . "\r\n");
118+
$start = \strpos($buffer, $boundary . "\r\n");
119119

120120
while ($start !== false) {
121121
// search following boundary (preceded by newline)
122122
// ignore last if not followed by boundary (SHOULD end with "--")
123123
$start += $len + 2;
124-
$end = strpos($buffer, "\r\n" . $boundary, $start);
124+
$end = \strpos($buffer, "\r\n" . $boundary, $start);
125125
if ($end === false) {
126126
break;
127127
}
128128

129129
// parse one part and continue searching for next
130-
$this->parsePart(substr($buffer, $start, $end - $start));
130+
$this->parsePart(\substr($buffer, $start, $end - $start));
131131
$start = $end;
132132
}
133133
}
134134

135135
private function parsePart($chunk)
136136
{
137-
$pos = strpos($chunk, "\r\n\r\n");
137+
$pos = \strpos($chunk, "\r\n\r\n");
138138
if ($pos === false) {
139139
return;
140140
}
141141

142142
$headers = $this->parseHeaders((string)substr($chunk, 0, $pos));
143-
$body = (string)substr($chunk, $pos + 4);
143+
$body = (string)\substr($chunk, $pos + 4);
144144

145145
if (!isset($headers['content-disposition'])) {
146146
return;
@@ -180,7 +180,7 @@ private function parseFile($name, $filename, $contentType, $contents)
180180

181181
private function parseUploadedFile($filename, $contentType, $contents)
182182
{
183-
$size = strlen($contents);
183+
$size = \strlen($contents);
184184

185185
// no file selected (zero size and empty filename)
186186
if ($size === 0 && $filename === '') {
@@ -192,7 +192,7 @@ private function parseUploadedFile($filename, $contentType, $contents)
192192
return new UploadedFile(
193193
Psr7\stream_for(),
194194
$size,
195-
UPLOAD_ERR_NO_FILE,
195+
\UPLOAD_ERR_NO_FILE,
196196
$filename,
197197
$contentType
198198
);
@@ -208,7 +208,7 @@ private function parseUploadedFile($filename, $contentType, $contents)
208208
return new UploadedFile(
209209
Psr7\stream_for(),
210210
$size,
211-
UPLOAD_ERR_INI_SIZE,
211+
\UPLOAD_ERR_INI_SIZE,
212212
$filename,
213213
$contentType
214214
);
@@ -219,7 +219,7 @@ private function parseUploadedFile($filename, $contentType, $contents)
219219
return new UploadedFile(
220220
Psr7\stream_for(),
221221
$size,
222-
UPLOAD_ERR_FORM_SIZE,
222+
\UPLOAD_ERR_FORM_SIZE,
223223
$filename,
224224
$contentType
225225
);
@@ -228,7 +228,7 @@ private function parseUploadedFile($filename, $contentType, $contents)
228228
return new UploadedFile(
229229
Psr7\stream_for($contents),
230230
$size,
231-
UPLOAD_ERR_OK,
231+
\UPLOAD_ERR_OK,
232232
$filename,
233233
$contentType
234234
);
@@ -247,7 +247,7 @@ private function parsePost($name, $value)
247247
$value
248248
));
249249

250-
if (strtoupper($name) === 'MAX_FILE_SIZE') {
250+
if (\strtoupper($name) === 'MAX_FILE_SIZE') {
251251
$this->maxFileSize = (int)$value;
252252

253253
if ($this->maxFileSize === 0) {
@@ -260,15 +260,15 @@ private function parseHeaders($header)
260260
{
261261
$headers = array();
262262

263-
foreach (explode("\r\n", trim($header)) as $line) {
264-
$parts = explode(':', $line, 2);
263+
foreach (\explode("\r\n", \trim($header)) as $line) {
264+
$parts = \explode(':', $line, 2);
265265
if (!isset($parts[1])) {
266266
continue;
267267
}
268268

269-
$key = strtolower(trim($parts[0]));
270-
$values = explode(';', $parts[1]);
271-
$values = array_map('trim', $values);
269+
$key = \strtolower(trim($parts[0]));
270+
$values = \explode(';', $parts[1]);
271+
$values = \array_map('trim', $values);
272272
$headers[$key] = $values;
273273
}
274274

@@ -278,7 +278,7 @@ private function parseHeaders($header)
278278
private function getParameterFromHeader(array $header, $parameter)
279279
{
280280
foreach ($header as $part) {
281-
if (preg_match('/' . $parameter . '="?(.*)"$/', $part, $matches)) {
281+
if (\preg_match('/' . $parameter . '="?(.*)"$/', $part, $matches)) {
282282
return $matches[1];
283283
}
284284
}
@@ -288,8 +288,8 @@ private function getParameterFromHeader(array $header, $parameter)
288288

289289
private function extractPost($postFields, $key, $value)
290290
{
291-
$chunks = explode('[', $key);
292-
if (count($chunks) == 1) {
291+
$chunks = \explode('[', $key);
292+
if (\count($chunks) == 1) {
293293
$postFields[$key] = $value;
294294
return $postFields;
295295
}
@@ -299,23 +299,23 @@ private function extractPost($postFields, $key, $value)
299299
return $postFields;
300300
}
301301

302-
$chunkKey = rtrim($chunks[0], ']');
302+
$chunkKey = \rtrim($chunks[0], ']');
303303
$parent = &$postFields;
304304
for ($i = 1; isset($chunks[$i]); $i++) {
305305
$previousChunkKey = $chunkKey;
306306

307307
if ($previousChunkKey === '') {
308308
$parent[] = array();
309-
end($parent);
310-
$parent = &$parent[key($parent)];
309+
\end($parent);
310+
$parent = &$parent[\key($parent)];
311311
} else {
312-
if (!isset($parent[$previousChunkKey]) || !is_array($parent[$previousChunkKey])) {
312+
if (!isset($parent[$previousChunkKey]) || !\is_array($parent[$previousChunkKey])) {
313313
$parent[$previousChunkKey] = array();
314314
}
315315
$parent = &$parent[$previousChunkKey];
316316
}
317317

318-
$chunkKey = rtrim($chunks[$i], ']');
318+
$chunkKey = \rtrim($chunks[$i], ']');
319319
}
320320

321321
if ($chunkKey === '') {

0 commit comments

Comments
 (0)