From patchwork Sun Mar 25 03:50:36 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Herbert Xu X-Patchwork-Id: 10306475 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 4CB6A60325 for ; Sun, 25 Mar 2018 03:50:46 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 364C829346 for ; Sun, 25 Mar 2018 03:50:46 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2B24429351; Sun, 25 Mar 2018 03:50:46 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id EF89329346 for ; Sun, 25 Mar 2018 03:50:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753005AbeCYDun (ORCPT ); Sat, 24 Mar 2018 23:50:43 -0400 Received: from orcrist.hmeau.com ([104.223.48.154]:49322 "EHLO deadmen.hmeau.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750981AbeCYDun (ORCPT ); Sat, 24 Mar 2018 23:50:43 -0400 Received: from gondobar.mordor.me.apana.org.au ([192.168.128.4] helo=gondobar) by deadmen.hmeau.com with esmtp (Exim 4.84_2 #2 (Debian)) id 1ezwg2-0005jV-FN; Sun, 25 Mar 2018 11:50:38 +0800 Received: from herbert by gondobar with local (Exim 4.84_2) (envelope-from ) id 1ezwg0-0001MR-A9; Sun, 25 Mar 2018 11:50:36 +0800 Date: Sun, 25 Mar 2018 11:50:36 +0800 From: Herbert Xu To: DASH Mailing List Subject: builtin: Fix echo performance regression Message-ID: <20180325035036.GA5214@gondor.apana.org.au> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) Sender: dash-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: dash@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP The commit d6c0e1e2ffbf7913ab69d51cc794d48d41c8fcb1 ("[BUILTIN] Handle embedded NULs correctly in printf") caused a performance regression in the echo built-in because every echo call now goes through the printf %b slow path where the string is always printed twice to ensure the space padding is correct in the presence of NUL characters. In fact this regression applies to printf %b as well. This is easily fixed by making printf %b take the fast path when no precision/field width modifiers are present. This patch also changes the second strchurnul call to strspn which generates slightly better code. Signed-off-by: Herbert Xu diff --git a/src/bltin/printf.c b/src/bltin/printf.c index ec7c8c2..eaf14b6 100644 --- a/src/bltin/printf.c +++ b/src/bltin/printf.c @@ -98,20 +98,25 @@ static int print_escape_str(const char *f, int *param, int *array, char *s) int total; setstackmark(&smark); - done = conv_escape_str(s, &p); - q = stackblock(); - len = p - q; + done = conv_escape_str(s, &q); + p = stackblock(); + len = q - p; + total = len - 1; + + if (f[1] == 's') + goto easy; - p = makestrspace(len, p); - memset(p, 'X', len - 1); - p[len - 1] = 0; + p = makestrspace(len, q); + memset(p, 'X', total); + p[total] = 0; q = stackblock(); total = ASPF(&p, f, p); len = strchrnul(p, 'X') - p; - memcpy(p + len, q, strchrnul(p + len, ' ') - (p + len)); + memcpy(p + len, q, strspn(p + len, "X")); +easy: out1mem(p, total); popstackmark(&smark);