Re: Bash-specific performance by avoiding sed

Eric Blake <[email protected]>
Newsgroups gmane.comp.gnu.libtool.general
Organization Red Hat, Inc.
Message-ID <[email protected]>
On 03/09/2015 01:50 PM, Bob Friesenhahn wrote:
> On Mon, 9 Mar 2015, Mike Gran wrote:
> 
>> Hello libtool,
>>
>> I don't know if y'all saw this blogpost where a guy pushed
>> the sed regular expression handling into bash-specific
>> regular expressions when bash was available.  He claims
>> there's a significant performance improvement because of
>> reduced forking.
>>
>> http://harald.hoyer.xyz/2015/03/05/libtool-getting-rid-of-180000-sed-forks/
>>
> 
> There is an issue in the libtool bug tracker regarding this.
> 
> This solution only works with GNU bash.  It would be good if volunteers
> could research to see if there are similar solutions which can work with
> other common shells (e.g. dash, ksh, zsh).

For context, we're trying to speed up:

sed_quote_subst='s|\([`"$\\]\)|\\\1|g'
_G_unquoted_arg=`printf '%s\n' "$1" |$SED "$sed_quote_subst"`

How about this, which should be completely portable to XSI shells (alas,
it still uses ${a#b} and ${a%b} at the end, so it is not portable to
ancient Solaris /bin/sh):

# func_quote STRING
# Escapes all \`"$ in STRING with another \, and stores that in $quoted
func_quote () {
  case $1 in
    *[\\\`\"\$]*)
      save_IFS=$IFS pre=.$1.
      for char in '\' '`' '"' '$'; do
        post= IFS=$char
        for part in $pre; do
          post=${post:+$post\\$char}$part
        done
        pre=$post
      done
      IFS=$save_IFS post=${post%.}
      quoted=${post#.} ;;
    *) quoted=$1 ;;
  esac
}

(of course, with proper munging of internal variable names [$save_IFS,
$pre, $post, $char, $quoted] so as to be less likely to collide with
other variables in use)

So, where we were previously doing:

_G_unquoted_arg=`printf '%s\n' "$1" | $SED "$sed_quote_subst"`

we would now do:

func_quote "$1"
_G_unquoted_arg=$quoted

for the same result, without any forking.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

_______________________________________________
https://lists.gnu.org/mailman/listinfo/libtool
signature.asc (application/pgp-signature, 604 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1
Comment: Public key at http://people.redhat.com/eblake/eblake.gpg
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQEcBAEBCAAGBQJU/gcnAAoJEKeha0olJ0NqZ20IAKyOsIiHdEm7paDcMVGD4R5j
ve+NuGNXaiotZf6GSabnbXOFDbbyJtDF9y2kxfsZtGoQi8j5rxv8qtfAHCeZYRug
2f5ihaOmCn6WVOqKRK+bxXpTme5vUjszs2VXPAoy6E7QNVA+rDkKOcc7Mt21uhI7
Wnkn0QqCyiXJsBc7MPD7RWMd1aRMlrRF8RQbpEkhrg+NJoQjitS3xaNlkf5rhHNl
xoTcf3kO1dBV1TCSAekzZm0mN/RjPZMMq+X4/Yg2+soxAQZsjKah9Upng400/qvs
6xcTQxQqtahFAdSYi6HHe0tk8IRNfsFrBvclmU5bkyvZphZ7bIzNlMIP8mfPTxo=
=GFB4
-----END PGP SIGNATURE-----
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.