Re: Bash-specific performance by avoiding sed

Mike Frysinger <[email protected]>
Newsgroups gmane.comp.gnu.libtool.general
Message-ID <20150309220434.GA9455@vapier>
On 09 Mar 2015 14:48, Eric Blake wrote:
> On 03/09/2015 01:50 PM, Bob Friesenhahn wrote:
> > On Mon, 9 Mar 2015, Mike Gran wrote:
> >> 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

should we test the size of the string first ?  i've written such raw shell 
string parsing functions before, and once you hit a certain size (like 1k+ 
iirc), forking out to sed is way faster, especially when running in multibyte 
locales (like UTF8) which most people are doing nowadays.
-mike

_______________________________________________
https://lists.gnu.org/mailman/listinfo/libtool
signature.asc (application/pgp-signature, 819 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2

iQIcBAEBCAAGBQJU/hjyAAoJEEFjO5/oN/WBK60P/RsYAlLjIhs+flk2etBKrKwH
msAv8/YGPs/QjjaeY8+x3Ybt/BowdK0qVUAWvoZAt6p4M8010yAfv5TkcmS99rAL
uMdy5mv1sLe2HU08v+8vgeQnAzoXWG3IWeVURw46IBNafXkDZUbSmhVOfulV5C0w
sQTTP3G2SSogW3c9lNAbdUBCnWw0W9xBEpdz1Izc1ac1OUzvs0RL7uSwxvE0FvGG
YOeFsX+Wx939pXweBA6Mf9oTgUuwSnxiqQK9S0DJ3WJGTZWVH41AH34xTqrCl7ix
wgYFaxeVU4DW+4bMavOekYmvycp1+8sDR2cuxlRpjcFQq0A3+qu2qeHAzRg0fYef
u/jl6ZQQ8SIaJ4ULrABQ8fy+2tXDGVOafofTxWANvbRox1hCxsyeEDDFg/F2Tt+h
l4poY5KVSTsnPEaeB1Bi2Kth5v9PvWUhen2mvyV/3NmUu74ZHoMUkE0iR/wvUBvt
um3BXVge1W4mXGaYwJsljsCtfiIZ7SCOqcJUyC5On8NrragGT4sOO+jt9kCAWZfD
9Kr/JKGU5jcR10etwFBI7H8rhW2YDXWKtWo6l8QHy3uQZoD/f3BxvD8wnhOOe+ks
iPAbG8YF5mPIo+Ih6XhFj6R1l31WNOj285AGQMmQndiwwmBiTnc4vbQ+recVzPUY
jNpiM+sFgwbG50gcW67S
=PfHD
-----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.