zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: "zsh-workers@zsh.org" <zsh-workers@zsh.org>
Subject: Re: Idea for optimization (use case: iterate string with index parameter)
Date: Fri, 5 Jan 2018 14:23:57 -0800	[thread overview]
Message-ID: <CAH+w=7ZyKsNCqfO=EQPapmrSh+VPb-EFFhTXvBbt85fOR4DjAw@mail.gmail.com> (raw)
In-Reply-To: <etPan.5a4f7fdd.52e15119.14e5a@zdharma.org>

On Fri, Jan 5, 2018 at 5:38 AM, Sebastian Gniazdowski
<psprint@zdharma.org> wrote:
> iterating string with index parameter is quite slow, because unicode characters are skipped and counted using mbrtowc().

I can't remember the last time I needed to do that kind of iteration.

> For example, I saw z-sy-h uses such loops, my projects sometimes use them too. The point is that iterating a string and doing something with letters, e.g. counting brackets, is a very common use case, and the optimization would be triggered often.

Hmm.  Whether this is worthwhile depends on the size of the typical
processed string.  I can see this affecting z-sy-h when e.g. running
zed on a big function, but probably not when editing a typical command
line.

Maybe it would be reasonable to do something in shell code, e.g.:

typeset -a iter=(${(s//)string})
for ((i=1; i <= $#iter; i++)); do something with $iter[i]; done
string=${(j//)iter} # if needed

That is more memory-intensive, of course, but it also assists with
cases of unordered access into the array of characters.

> In general, the array would hold #N (5-10 or so) last string-index requests. If new request would target the same string, but index greater by 1, getarg() would call mbrtowc() once (via MB_METACHARLEN macro) reusing the previous in-string pointer.

Why only when greater by 1?  If greater, scan to and record the next
needed position.  Same number of mbrtowc() conversions, overall.


  reply	other threads:[~2018-01-05 22:24 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-05 13:38 Sebastian Gniazdowski
2018-01-05 22:23 ` Bart Schaefer [this message]
2018-01-06  5:16   ` Sebastian Gniazdowski

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAH+w=7ZyKsNCqfO=EQPapmrSh+VPb-EFFhTXvBbt85fOR4DjAw@mail.gmail.com' \
    --to=schaefer@brasslantern.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).