Skip to content

Commit 1144b85

Browse files
committed
MySQLnd: Avoid some reallocations in PS decoder
1 parent 003d9ba commit 1144b85

File tree

1 file changed

+13
-48
lines changed

1 file changed

+13
-48
lines changed

ext/mysqlnd/mysqlnd_ps_codec.c

Lines changed: 13 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,7 @@ static void
212212
ps_fetch_time(zval * zv, const MYSQLND_FIELD * const field, const unsigned int pack_len, const zend_uchar ** row)
213213
{
214214
struct st_mysqlnd_time t;
215-
zend_ulong length; /* First byte encodes the length*/
216-
char * value;
215+
zend_ulong length; /* First byte encodes the length */
217216
DBG_ENTER("ps_fetch_time");
218217

219218
if ((length = php_mysqlnd_net_field_length(row))) {
@@ -241,24 +240,13 @@ ps_fetch_time(zval * zv, const MYSQLND_FIELD * const field, const unsigned int p
241240
}
242241

243242
if (field->decimals > 0 && field->decimals < 7) {
244-
length = mnd_sprintf(
245-
&value,
246-
0,
247-
"%s%02u:%02u:%02u.%0*u",
248-
(t.neg ? "-" : ""),
249-
t.hour,
250-
t.minute,
251-
t.second,
252-
field->decimals,
253-
(uint32_t) (t.second_part / pow(10, 6 - field->decimals))
254-
);
243+
ZVAL_STR(zv, zend_strpprintf(0, "%s%02u:%02u:%02u.%0*u",
244+
(t.neg ? "-" : ""), t.hour, t.minute, t.second, field->decimals,
245+
(uint32_t) (t.second_part / pow(10, 6 - field->decimals))));
255246
} else {
256-
length = mnd_sprintf(&value, 0, "%s%02u:%02u:%02u", (t.neg ? "-" : ""), t.hour, t.minute, t.second);
247+
ZVAL_STR(zv, zend_strpprintf(0, "%s%02u:%02u:%02u",
248+
(t.neg ? "-" : ""), t.hour, t.minute, t.second));
257249
}
258-
259-
DBG_INF_FMT("%s", value);
260-
ZVAL_STRINGL(zv, value, length);
261-
mnd_sprintf_free(value);
262250
DBG_VOID_RETURN;
263251
}
264252
/* }}} */
@@ -270,7 +258,6 @@ ps_fetch_date(zval * zv, const MYSQLND_FIELD * const field, const unsigned int p
270258
{
271259
struct st_mysqlnd_time t = {0};
272260
zend_ulong length; /* First byte encodes the length*/
273-
char * value;
274261
DBG_ENTER("ps_fetch_date");
275262

276263
if ((length = php_mysqlnd_net_field_length(row))) {
@@ -291,11 +278,7 @@ ps_fetch_date(zval * zv, const MYSQLND_FIELD * const field, const unsigned int p
291278
t.time_type = MYSQLND_TIMESTAMP_DATE;
292279
}
293280

294-
length = mnd_sprintf(&value, 0, "%04u-%02u-%02u", t.year, t.month, t.day);
295-
296-
DBG_INF_FMT("%s", value);
297-
ZVAL_STRINGL(zv, value, length);
298-
mnd_sprintf_free(value);
281+
ZVAL_STR(zv, zend_strpprintf(0, "%04u-%02u-%02u", t.year, t.month, t.day));
299282
DBG_VOID_RETURN;
300283
}
301284
/* }}} */
@@ -307,7 +290,6 @@ ps_fetch_datetime(zval * zv, const MYSQLND_FIELD * const field, const unsigned i
307290
{
308291
struct st_mysqlnd_time t;
309292
zend_ulong length; /* First byte encodes the length*/
310-
char * value;
311293
DBG_ENTER("ps_fetch_datetime");
312294

313295
if ((length = php_mysqlnd_net_field_length(row))) {
@@ -336,26 +318,13 @@ ps_fetch_datetime(zval * zv, const MYSQLND_FIELD * const field, const unsigned i
336318
}
337319

338320
if (field->decimals > 0 && field->decimals < 7) {
339-
length = mnd_sprintf(
340-
&value,
341-
0,
342-
"%04u-%02u-%02u %02u:%02u:%02u.%0*u",
343-
t.year,
344-
t.month,
345-
t.day,
346-
t.hour,
347-
t.minute,
348-
t.second,
349-
field->decimals,
350-
(uint32_t) (t.second_part / pow(10, 6 - field->decimals))
351-
);
321+
ZVAL_STR(zv, zend_strpprintf(0, "%04u-%02u-%02u %02u:%02u:%02u.%0*u",
322+
t.year, t.month, t.day, t.hour, t.minute, t.second, field->decimals,
323+
(uint32_t) (t.second_part / pow(10, 6 - field->decimals))));
352324
} else {
353-
length = mnd_sprintf(&value, 0, "%04u-%02u-%02u %02u:%02u:%02u", t.year, t.month, t.day, t.hour, t.minute, t.second);
325+
ZVAL_STR(zv, zend_strpprintf(0, "%04u-%02u-%02u %02u:%02u:%02u",
326+
t.year, t.month, t.day, t.hour, t.minute, t.second));
354327
}
355-
356-
DBG_INF_FMT("%s", value);
357-
ZVAL_STRINGL(zv, value, length);
358-
mnd_sprintf_free(value);
359328
DBG_VOID_RETURN;
360329
}
361330
/* }}} */
@@ -365,15 +334,11 @@ ps_fetch_datetime(zval * zv, const MYSQLND_FIELD * const field, const unsigned i
365334
static void
366335
ps_fetch_string(zval * zv, const MYSQLND_FIELD * const field, const unsigned int pack_len, const zend_uchar ** row)
367336
{
368-
/*
369-
For now just copy, before we make it possible
370-
to write \0 to the row buffer
371-
*/
372337
const zend_ulong length = php_mysqlnd_net_field_length(row);
373338
DBG_ENTER("ps_fetch_string");
374339
DBG_INF_FMT("len = %lu", length);
375340
DBG_INF("copying from the row buffer");
376-
ZVAL_STRINGL(zv, (char *)*row, length);
341+
ZVAL_STRINGL_FAST(zv, (char *)*row, length);
377342

378343
(*row) += length;
379344
DBG_VOID_RETURN;

0 commit comments

Comments
 (0)