From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 8159 invoked by alias); 2 May 2007 07:09:58 -0000 Received: (qmail 8138 invoked by uid 22791); 2 May 2007 07:09:58 -0000 X-Spam-Check-By: sourceware.org Received: from sunsite.ms.mff.cuni.cz (HELO sunsite.mff.cuni.cz) (195.113.15.26) by sourceware.org (qpsmtpd/0.31) with ESMTP; Wed, 02 May 2007 07:09:54 +0000 Received: from sunsite.mff.cuni.cz (localhost.localdomain [127.0.0.1]) by sunsite.mff.cuni.cz (8.13.8/8.13.8) with ESMTP id l427HeDl002356; Wed, 2 May 2007 09:17:40 +0200 Received: (from jakub@localhost) by sunsite.mff.cuni.cz (8.13.8/8.13.8/Submit) id l427HeYh002355; Wed, 2 May 2007 09:17:40 +0200 Date: Wed, 02 May 2007 07:09:00 -0000 From: Jakub Jelinek To: Ulrich Drepper Cc: Glibc hackers Subject: [PATCH] Decrease vfprintf stack usage Message-ID: <20070502071740.GS1826@sunsite.mff.cuni.cz> Reply-To: Jakub Jelinek Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.4.2.2i Mailing-List: contact libc-hacker-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-hacker-owner@sourceware.org X-SW-Source: 2007-05/txt/msg00001.txt.bz2 Hi! The recent vfprintf change increased vfprintf's stack usage 3.5x times: 0000000000000890 <_IO_vfprintf>: 890: 55 push %rbp 891: 48 89 e5 mov %rsp,%rbp 894: 41 57 push %r15 896: 41 56 push %r14 898: 41 55 push %r13 89a: 41 54 push %r12 89c: 53 push %rbx - 89d: 48 81 ec 58 06 00 00 sub $0x658,%rsp + 89d: 48 81 ec 58 16 00 00 sub $0x1658,%rsp I think that's just too much for something rare (I believe most format strings don't use precision for %s). While a VLA is a little bit more expensive, we'll hit that only when somebody actually uses a precision on %s and for usual precisions we'll still use less stack. 2007-05-02 Jakub Jelinek * stdio-common/vfprintf.c (process_string_arg): Use a VLA rather than fixed length array for ignore. --- libc/stdio-common/vfprintf.c.jj 2007-05-02 08:43:05.000000000 +0200 +++ libc/stdio-common/vfprintf.c 2007-05-02 08:50:17.000000000 +0200 @@ -1162,7 +1162,8 @@ vfprintf (FILE *s, const CHAR_T *format, /* In case we have a multibyte character set the \ situation is more complicated. We must not copy \ bytes at the end which form an incomplete character. */\ - wchar_t ignore[1024]; \ + size_t ignore_size = (unsigned) prec > 1024 ? 1024 : prec;\ + wchar_t ignore[ignore_size]; \ const char *str2 = string; \ const char *strend = string + prec; \ if (strend < string) \ @@ -1172,8 +1173,8 @@ vfprintf (FILE *s, const CHAR_T *format, memset (&ps, '\0', sizeof (ps)); \ \ while (str2 != NULL && str2 < strend) \ - if (__mbsnrtowcs (ignore, &str2, strend - str2, 1024, \ - &ps) == (size_t) -1) \ + if (__mbsnrtowcs (ignore, &str2, strend - str2, \ + ignore_size, &ps) == (size_t) -1) \ { \ done = -1; \ goto all_done; \ Jakub