Skip to content

Commit abd9ca3

Browse files
committed
Make assorted performance improvements in snprintf.c.
In combination, these changes make our version of snprintf as fast or faster than most platforms' native snprintf, except for cases involving floating-point conversion (which we still delegate to the native sprintf). The speed penalty for a float conversion is down to around 10% though, much better than before. Notable changes: * Rather than always parsing the format twice to see if it contains instances of %n$, do the extra scan only if we actually find a $. This obviously wins for non-localized formats, and even when there is use of %n$, we can avoid scanning text before the first % twice. * Use strchrnul() if available to find the next %, and emit the literal text between % escapes as strings rather than char-by-char. * Create a bespoke function (dopr_outchmulti) for the common case of emitting N copies of the same character, in place of writing loops around dopr_outch. * Simplify construction of the format string for invocations of sprintf for floats. * Const-ify some internal functions, and avoid unnecessary use of pass-by-reference arguments. Patch by me, reviewed by Andres Freund Discussion: https://postgr.es/m/11787.1534530779@sss.pgh.pa.us
1 parent 9bc9f72 commit abd9ca3

File tree

5 files changed

+432
-273
lines changed

5 files changed

+432
-273
lines changed

configure

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15100,7 +15100,7 @@ fi
1510015100
LIBS_including_readline="$LIBS"
1510115101
LIBS=`echo "$LIBS" | sed -e 's/-ledit//g' -e 's/-lreadline//g'`
1510215102

15103-
for ac_func in cbrt clock_gettime fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll posix_fallocate ppoll pstat pthread_is_threaded_np readlink setproctitle setproctitle_fast setsid shm_open symlink sync_file_range utime utimes wcstombs_l
15103+
for ac_func in cbrt clock_gettime fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll posix_fallocate ppoll pstat pthread_is_threaded_np readlink setproctitle setproctitle_fast setsid shm_open strchrnul symlink sync_file_range utime utimes wcstombs_l
1510415104
do :
1510515105
as_ac_var=`$as_echo "ac_cv_func_$ac_func" | $as_tr_sh`
1510615106
ac_fn_c_check_func "$LINENO" "$ac_func" "$as_ac_var"

configure.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1571,7 +1571,7 @@ PGAC_FUNC_WCSTOMBS_L
15711571
LIBS_including_readline="$LIBS"
15721572
LIBS=`echo "$LIBS" | sed -e 's/-ledit//g' -e 's/-lreadline//g'`
15731573

1574-
AC_CHECK_FUNCS([cbrt clock_gettime fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll posix_fallocate ppoll pstat pthread_is_threaded_np readlink setproctitle setproctitle_fast setsid shm_open symlink sync_file_range utime utimes wcstombs_l])
1574+
AC_CHECK_FUNCS([cbrt clock_gettime fdatasync getifaddrs getpeerucred getrlimit mbstowcs_l memmove poll posix_fallocate ppoll pstat pthread_is_threaded_np readlink setproctitle setproctitle_fast setsid shm_open strchrnul symlink sync_file_range utime utimes wcstombs_l])
15751575

15761576
AC_REPLACE_FUNCS(fseeko)
15771577
case $host_os in

src/include/pg_config.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,9 @@
523523
/* Define to 1 if you have the <stdlib.h> header file. */
524524
#undef HAVE_STDLIB_H
525525

526+
/* Define to 1 if you have the `strchrnul' function. */
527+
#undef HAVE_STRCHRNUL
528+
526529
/* Define to 1 if you have the `strerror_r' function. */
527530
#undef HAVE_STRERROR_R
528531

src/include/pg_config.h.win32

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,9 @@
394394
/* Define to 1 if you have the <stdlib.h> header file. */
395395
#define HAVE_STDLIB_H 1
396396

397+
/* Define to 1 if you have the `strchrnul' function. */
398+
/* #undef HAVE_STRCHRNUL */
399+
397400
/* Define to 1 if you have the `strerror_r' function. */
398401
/* #undef HAVE_STRERROR_R */
399402

0 commit comments

Comments
 (0)