diff options
author | Nathan Bossart <nathan@postgresql.org> | 2023-12-04 11:55:18 -0600 |
---|---|---|
committer | Nathan Bossart <nathan@postgresql.org> | 2023-12-04 11:55:18 -0600 |
commit | b14b1eb4da4c97afec24cf8956e842b98ebb2a51 (patch) | |
tree | 621b51a60c7c4863686a42cf97b67718ea22ca46 /src | |
parent | e7c6efe305afff030b50fe6f792af48deff5cf3e (diff) | |
download | postgresql-b14b1eb4da4c97afec24cf8956e842b98ebb2a51.tar.gz postgresql-b14b1eb4da4c97afec24cf8956e842b98ebb2a51.zip |
Teach convert() and friends to avoid copying when possible.
Presently, pg_convert() allocates a new bytea and copies the result
regardless of whether any conversion actually happened. This
commit adjusts this function to return the source pointer as-is if
no conversion occurred. This optimization isn't expected to make a
tremendous difference, but it still seems worthwhile to avoid
unnecessary memory allocations.
Author: Yurii Rashkovskii
Reviewed-by: Bertrand Drouvot
Discussion: https://postgr.es/m/CA%2BRLCQyknBPSWXRBQGOi6aYEcdQ9RpH9Kch4GjoeY8dQ3D%2Bvhw%40mail.gmail.com
Diffstat (limited to 'src')
-rw-r--r-- | src/backend/utils/mb/mbutils.c | 12 |
1 files changed, 6 insertions, 6 deletions
diff --git a/src/backend/utils/mb/mbutils.c b/src/backend/utils/mb/mbutils.c index 67a1ab2ab23..10eb6cd6fa7 100644 --- a/src/backend/utils/mb/mbutils.c +++ b/src/backend/utils/mb/mbutils.c @@ -585,19 +585,19 @@ pg_convert(PG_FUNCTION_ARGS) src_encoding, dest_encoding); - /* update len if conversion actually happened */ - if (dest_str != src_str) - len = strlen(dest_str); + + /* return source string if no conversion happened */ + if (dest_str == src_str) + PG_RETURN_BYTEA_P(string); /* * build bytea data type structure. */ + len = strlen(dest_str); retval = (bytea *) palloc(len + VARHDRSZ); SET_VARSIZE(retval, len + VARHDRSZ); memcpy(VARDATA(retval), dest_str, len); - - if (dest_str != src_str) - pfree(dest_str); + pfree(dest_str); /* free memory if allocated by the toaster */ PG_FREE_IF_COPY(string, 0); |