Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm
Precedence: bulk
X-No-Archive: yes
List-Id: Zsh Workers List <zsh-workers.zsh.org>
List-Post: <mailto:zsh-workers@zsh.org>
List-Help: <mailto:zsh-workers-help@zsh.org>
X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on f.primenet.com.au
X-Spam-Level: 
X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM,
	T_DKIM_INVALID autolearn=ham autolearn_force=no version=3.4.1
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=20120113;
        h=mime-version:in-reply-to:references:from:date:message-id:subject:to
         :cc:content-type;
        bh=XPJBUyekULyNC76USZlba36Cd0cM+vBXUZ90iREK96I=;
        b=HYDLJihIcVcqp/fNhP4CWeqaXi2GB9u8C0MezesdYb5dQ+E76P41B4fUS6AAGuIANw
         Xrem0IeHeMBsrOQh3f//+0DTPEkTEUUJQda2LBjzta7LeKwHntbpVf4e2zmZJMSAw2Xy
         iXVE1wQIVnYqsMIzrnLc5Rprbv0Qd9TvD73SyMEmCKeF/a02wHHwiKos49KW0i6Y4Cc/
         qZIbJFQXDnGjluv5tF7HgD/F9P7l5ZixhtABByM5EwRdSE8lopPqH4Wh0Hen4G8B89om
         CiF7kM6mGfzR2QCLRYrKMoAuln5z3qFXuwc44jOWCbK7HJcPqtfFbdBkytmAKg8t/HvR
         DO8g==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:mime-version:in-reply-to:references:from:date
         :message-id:subject:to:cc:content-type;
        bh=XPJBUyekULyNC76USZlba36Cd0cM+vBXUZ90iREK96I=;
        b=bL+BB0M/AUvbd7QpCj5XplX4yyb7ncGNqHmSwSCY0n9bV0Iomnh1lCRvu+HM66yZPD
         5RyO1rhVszNiXUvRD5OPYA6f41DOdPHnkbetikLHbpRFDB2YXAK6ucScyG/4Riy93Ktf
         M10relltOG6Hcb3WBknuplAXv+N59Vo6YQtMayAFcN0trcu7h+z5GmkkuTHTczAZs7rl
         JehnFKoofNQSXkepKkXcZbQO9zWcEQRDuxpOIkY7CECiLSF4txe2o3oAMilVe/rR1lAQ
         qXReO9FoDfF4pYWEOpPdnxnJQWeQlMgupQX/f0j6pA/6gD8J0RWEw/SkeG4RlrXPKJTY
         qwjQ==
X-Gm-Message-State: AG10YOTWEhnthiQNrgpBbUrFYDP1pWAXInqeRc0teLTxy0arqOkMZOAZtSYrHGyFBsSk16eiBl7Gtt+6RCF+6A==
X-Received: by 10.112.134.165 with SMTP id pl5mr18684122lbb.126.1455187449961;
 Thu, 11 Feb 2016 02:44:09 -0800 (PST)
MIME-Version: 1.0
In-Reply-To: <160210101846.ZM2333@torch.brasslantern.com>
References: <160111233259.ZM6719@torch.brasslantern.com> <CAKc7PVDhxbVgmV7WcGgyDzMmWSjwQ=7=LExXTwr7YvPzPpn2RA@mail.gmail.com>
 <160118223126.ZM28565@torch.brasslantern.com> <CAKc7PVB-X+DYVEHRqSoOiDAjNeTsVyz=EN72FHQuF=AnRmh65A@mail.gmail.com>
 <160119195608.ZM31931@torch.brasslantern.com> <20160123235303.GE20278@tarsus.local2>
 <160123222057.ZM16192@torch.brasslantern.com> <CAKc7PVCJFXCt+EVkYw3B7NDAUvdphgUuX6yjbt8mQYbv1Mx01w@mail.gmail.com>
 <160210101846.ZM2333@torch.brasslantern.com>
From: Sebastian Gniazdowski <sgniazdowski@gmail.com>
Date: Thu, 11 Feb 2016 11:43:50 +0100
Message-ID: <CAKc7PVAvJrGbzHrO8zwK_BxLdXH5knQNkiNrb0cE2h4Rh9A_Kg@mail.gmail.com>
Subject: Re: Slow highlighting (Re: "drop-in replacement" and transpose-words-match)
To: Bart Schaefer <schaefer@brasslantern.com>
Cc: Zsh hackers list <zsh-workers@zsh.org>
Content-Type: text/plain; charset=UTF-8
X-Seq: zsh-workers 37945

Hello,

a=""; a=${(r:100000::_:)a}; zshstyle() { repeat 100; do
b=${a[-5000,-1]}; done }; shstyle() { repeat 100; do b=${a:
-5000:5000}; done }; time ( zshstyle ); time ( shstyle )

( zshstyle; )  0,66s user 0,01s system 99% cpu 0,673 total
( shstyle; )  0,40s user 0,00s system 99% cpu 0,402 total

The point of this tangent example is: every indexing works by
iterating over buffer and counting characters. By using ":5000" one
pass of finding where an index points to is skipped, as it says "5000
characters from now on". Index -1 iterates from the beginning again to
find end of string.

Here Zsyh indexes buffer, uses -1 and also (i):

    if [[ $arg == ';' ]] ; then
      local needle=$'[;\n]'
      integer offset=${${buf[start_pos+1,-1]}[(i)$needle]}
      (( start_pos += offset - 1 ))
      (( end_pos = start_pos + $#arg ))
    else
      ((start_pos+=${#buf[$start_pos+1,-1]}-${#${buf[$start_pos+1,-1]##([[:space:]]|\\[[:space:]])#}}))
      ((end_pos=$start_pos+${#arg}))
    fi

The longer the buffer the more time -1 consumes. Of course any
indexing is slow, not only -1, but this is nice emphasis of the issue.
The only solution is apparently making Zsh storing strings as real
arrays, of wint_t type.

As for the (i), as far as I remember from the time I worked on C
source, reverse indexing uses one additional "iterate counting
characters" block:

https://github.com/zsh-users/zsh/blob/master/Src/params.c#L1360-L1396

That said, zsyh could be somewhat optimized if the ":howmany" syntax
would be utilized.

Best regards,
Sebastian Gniazdowski

