From: Nikhil Deshpande <ndeshpande@vmware.com>
To: pgsql-odbc@postgresql.org
Subject: [PATCH] Remove unncessary localtime() calls during data type conversion
Date: Fri, 12 Jun 2015 17:42:30 -0700
Message-ID: <557B7C76.1030502@vmware.com> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-odbc>
Hi,
While doing some profiling work on an ODBC client, we noticed tons
of libc localtime() and __tz_convert() calls on a hot code path
(SQLFetch()), but most of the fields being retrieved were not of
date/time type.
These libc calls are costly (non-trivial work in terms of string ops,
timzone conversion etc.) and the cost adds up quickly for high-frequency
call like SQLFetch(). We noticed that this penalty is paid even
for data types which aren't date/time related. Code inspection
showed localtime() call in convert.c:copy_and_convert_field()
at function start, being invoked regardless of whether output of
localtime() is used or not.
Attached is a non-intrusive patch that moves the call down
to where it's needed (for both getting data from server and
sending data to server), but I'm not sure if the patch is complete
(it covers the case where source data type is time but
destination needs date part, "TIME->DATE[TIME]").
Could you please review this patch and modify it for correctness
and consider for committing it?
---
A longer term cleaner approach would be to refactor the code to aim for
at most one call to localtime() (e.g. if it's already invoked once
in copy_and_convert_field() before, reuse that data). However
that is much more intrusive change.
I checked the test/src/result-conversions-test.c code and the
test case specific to this scenario is commented out (since it
the expected result would depend on actual current date).
I un-commented those test cases, ran the test with and without
my patch and got same results. Also the data looks ok for
"TIME->DATE[TIME]" case, input is time, output is date.
1114a1176
> '13:23:34' (time) as SQL_C_TYPE_DATE: y: 2015 m: 6 d: 12
2015-6-12 is indeed current date.
Thanks,
Nikhil
--
Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-odbcAttachments:0001-Remove-unncessary-localtime-calls-during-data-type-c.patch (text/plain, 3.2K) download
From e4f32886b34198d5620335b507e3ee5d0363d738 Mon Sep 17 00:00:00 2001
From: Nikhil R Deshpande <ndeshpande@vmware.com>
Date: Fri, 12 Jun 2015 11:53:09 -0700
Subject: [PATCH 1/1] Remove unncessary localtime() calls during data type
conversion
While sending/fetching field of time data type to/from server,
it might get converted to a data type that needs date part. In this
case, the driver chooses to fill up the missing date part as
current date, obtained through localtime() libc call. However
this localtime() call is invoked for every combination of source
and destination data types, and not just for above specific
time->date[time] case.
localtime() is not a cheap call (involves libc doing string
processing and timezone conversion operations), and both
send/fetch data paths are very high occurrence/hot code paths.
This penalty adds up to significant impact on overall driver
performance.
---
convert.c | 43 +++++++++++++++++++++++--------------------
1 file changed, 23 insertions(+), 20 deletions(-)
diff --git a/convert.c b/convert.cindex 8802e6e..d22885a 100644--- a/convert.c+++ b/convert.c@@ -912,16 +912,6 @@ copy_and_convert_field(StatementClass *stmt,
memset(&std_time, 0, sizeof(SIMPLE_TIME));
- /* Initialize current date */-#ifdef HAVE_LOCALTIME_R- tim = localtime_r(&stmt_t, &tm);-#else- tim = localtime(&stmt_t);-#endif /* HAVE_LOCALTIME_R */- std_time.m = tim->tm_mon + 1;- std_time.d = tim->tm_mday;- std_time.y = tim->tm_year + 1900;-
mylog("copy_and_convert: field_type = %d, fctype = %d, value = '%s', cbValueMax=%d\n", field_type, fCType, (value == NULL) ? "<NULL>" : value, cbValueMax);
if (!value)
@@ -1017,6 +1007,17 @@ mylog("null_cvt_date_string=%d\n", conn->connInfo.cvt_null_date_string);
case PG_TYPE_TIME:
sscanf(value, "%2d:%2d:%2d", &std_time.hh, &std_time.mm, &std_time.ss);
+ /* Initialize date in case conversion destination+ * expects date part from this source time data.+ */+#ifdef HAVE_LOCALTIME_R+ tim = localtime_r(&stmt_t, &tm);+#else+ tim = localtime(&stmt_t);+#endif /* HAVE_LOCALTIME_R */+ std_time.m = tim->tm_mon + 1;+ std_time.d = tim->tm_mday;+ std_time.y = tim->tm_year + 1900;
break;
case PG_TYPE_ABSTIME:
@@ -4139,15 +4140,6 @@ inolog("ipara=%p paramType=%d %d proc_return=%d\n", ipara, ipara ? ipara->paramT
param_string[0] = '\0';
cbuf[0] = '\0';
memset(&st, 0, sizeof(st));
- t = SC_get_time(qb->stmt);-#ifdef HAVE_LOCALTIME_R- tim = localtime_r(&t, &tm);-#else- tim = localtime(&t);-#endif /* HAVE_LOCALTIME_R */- st.m = tim->tm_mon + 1;- st.d = tim->tm_mday;- st.y = tim->tm_year + 1900;
ivstruct = (SQL_INTERVAL_STRUCT *) buffer;
/* Convert input C type to a neutral format */
@@ -4290,7 +4282,18 @@ mylog("C_WCHAR=%s(%d)\n", buffer, used);
st.hh = ts->hour;
st.mm = ts->minute;
st.ss = ts->second;
-+ /* Initialize date in case conversion destination+ * expects date part from this source time data.+ */+ t = SC_get_time(qb->stmt);+#ifdef HAVE_LOCALTIME_R+ tim = localtime_r(&t, &tm);+#else+ tim = localtime(&t);+#endif /* HAVE_LOCALTIME_R */+ st.m = tim->tm_mon + 1;+ st.d = tim->tm_mday;+ st.y = tim->tm_year + 1900;
break;
}
--
1.8.5.6
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-odbc@postgresql.org
Cc: ndeshpande@vmware.com
Subject: Re: [PATCH] Remove unncessary localtime() calls during data type conversion
In-Reply-To: <557B7C76.1030502@vmware.com>
References: <557B7C76.1030502@vmware.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox