public inbox for libc-hacker@sourceware.org
 help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@redhat.com>
To: Ulrich Drepper <drepper@redhat.com>
Cc: Glibc hackers <libc-hacker@sources.redhat.com>
Subject: [PATCH] Speed up sprintf
Date: Thu, 13 May 2004 11:15:00 -0000	[thread overview]
Message-ID: <20040513090306.GA5191@sunsite.ms.mff.cuni.cz> (raw)

Hi!

My http://sources.redhat.com/ml/libc-hacker/2003-05/msg00032.html
change apparently slowed down sprintf quite a lot on longer strings, if
buffer passed to sprintf is located in the first half of address space.

The problem is that the fix from last year for sprintf now sets write_end
to ~0L and if write_ptr is small enough, (ssize_t) (write_end - write_ptr)
is negative.  This results in overflow being called for every single
character instead of doing a mempcpy.
The following patch seems to fix it, now sprintf at least on the sample
testcase I have on IA-64 is 30% faster than sprintf in glibc 2.2.4
(as opposed to being almost 4 times slower).

2004-05-13  Jakub Jelinek  <jakub@redhat.com>

	* libio/genops.c (_IO_default_xsputn): Avoid one overflow per char if
	count is negative, yet write_ptr < write_end.
	(_IO_default_xsgetn): Avoid one underflow per char if count is
	negative, yet read_ptr < read_end.

--- libc/libio/genops.c.jj	2004-02-12 11:52:09.000000000 +0100
+++ libc/libio/genops.c	2004-05-13 13:02:15.715861960 +0200
@@ -447,10 +447,10 @@ _IO_default_xsputn (f, data, n)
   for (;;)
     {
       /* Space available. */
-      _IO_ssize_t count = f->_IO_write_end - f->_IO_write_ptr;
-      if (count > 0)
+      if (f->_IO_write_ptr < f->_IO_write_end)
 	{
-	  if ((_IO_size_t) count > more)
+	  _IO_size_t count = f->_IO_write_end - f->_IO_write_ptr;
+	  if (count > more)
 	    count = more;
 	  if (count > 20)
 	    {
@@ -462,9 +462,7 @@ _IO_default_xsputn (f, data, n)
 #endif
 	      s += count;
             }
-	  else if (count <= 0)
-	    count = 0;
-	  else
+	  else if (count)
 	    {
 	      char *p = f->_IO_write_ptr;
 	      _IO_ssize_t i;
@@ -504,10 +502,10 @@ _IO_default_xsgetn (fp, data, n)
   for (;;)
     {
       /* Data available. */
-      _IO_ssize_t count = fp->_IO_read_end - fp->_IO_read_ptr;
-      if (count > 0)
+      if (fp->_IO_read_ptr < fp->_IO_read_end)
 	{
-	  if ((_IO_size_t) count > more)
+	  _IO_size_t count = fp->_IO_read_end - fp->_IO_read_ptr;
+	  if (count > more)
 	    count = more;
 	  if (count > 20)
 	    {
@@ -519,9 +517,7 @@ _IO_default_xsgetn (fp, data, n)
 #endif
 	      fp->_IO_read_ptr += count;
 	    }
-	  else if (count <= 0)
-	    count = 0;
-	  else
+	  else if (count)
 	    {
 	      char *p = fp->_IO_read_ptr;
 	      int i = (int) count;

	Jakub

             reply	other threads:[~2004-05-13 11:15 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-05-13 11:15 Jakub Jelinek [this message]
2004-05-13 17:39 ` Ulrich Drepper

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20040513090306.GA5191@sunsite.ms.mff.cuni.cz \
    --to=jakub@redhat.com \
    --cc=drepper@redhat.com \
    --cc=libc-hacker@sources.redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).