zsh-users
 help / color / mirror / code / Atom feed
* Tip of the day: (@)...[(R)...] is equal to (@M)...:#... and faster by 18-21%
       [not found] <536851559.167591.1477040396961.ref@mail.yahoo.com>
@ 2016-10-21  8:59 ` psprint
  2016-10-21 16:20   ` Bart Schaefer
  0 siblings, 1 reply; 2+ messages in thread
From: psprint @ 2016-10-21  8:59 UTC (permalink / raw)
  To: zsh-users

Hello,
I've discovered that following code:

found=( "${(@M)history:#(#i)*$~search_pattern*}" )

can be replaced by:

found=( "${(@)history[(R)(#i)*$~search_pattern*]}" )

and the speeds are: 215 ms vs. 175 ms, for "(#i)**" final pattern, ${#history[@]} == 89000, and typeset -U found (resolving to 18500 entries). Performing sequence of searches for word "h", "hi", "his", ..., "history" gives accumulated times 1271 ms vs. 1000 ms, difference is ~ 7*40ms, in check. Measured via zprof, by wrapping block of code with () { ... }.

I wonder if there are there any pitfalls in such replacement?

Best regards,

Sebastian Gniazdowski


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Tip of the day: (@)...[(R)...] is equal to (@M)...:#... and faster by 18-21%
  2016-10-21  8:59 ` Tip of the day: (@)...[(R)...] is equal to (@M)...:#... and faster by 18-21% psprint
@ 2016-10-21 16:20   ` Bart Schaefer
  0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 2016-10-21 16:20 UTC (permalink / raw)
  To: zsh-users

On Oct 21,  8:59am, <psprint@yahoo.com> wrote:
}
} found=( "${(@M)history:#(#i)*$~search_pattern*}" )
} 
} can be replaced by:
} 
} found=( "${(@)history[(R)(#i)*$~search_pattern*]}" )
} 
} I wonder if there are there any pitfalls in such replacement?

There are some differences in the way the pattern may be interpreted
when it appears inside a subscript expression.  There is an entire
section of the documentation (15.2.4 in "info" or HTML docs) devoted
to explaining this.  One paragraph in particular applies here:

    A last detail must be considered when reverse subscripting is
    performed. Parameters appearing in the subscript expression are
    first expanded and then the complete expression is interpreted
    as a pattern. This has two effects: first, parameters behave as
    if GLOB_SUBST were on (and it cannot be turned off); second,
    backslashes are interpreted twice, once when parsing the array
    subscript and again when parsing the pattern. In a reverse
    subscript, it's necessary to use _four_ backslashes to cause a
    single backslash to match literally in the pattern. For complex
    patterns, it is often easiest to assign the desired pattern to a
    parameter and then refer to that parameter in the subscript, because
    then the backslashes, brackets, parentheses, etc., are seen only
    when the complete expression is converted to a pattern. To match
    the value of a parameter literally in a reverse subscript, rather
    than as a pattern, use `${(q)NAME}' to quote the expanded value.

So in your example you don't need "$~search_pattern", the expansion is
always interpreted as a pattern.  I think the literal match advice in
the final sentence could preferentially be ${(b)...} nowadays; there
are a couple of things about the array subscripting doc that might
need update.


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-10-21 16:20 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <536851559.167591.1477040396961.ref@mail.yahoo.com>
2016-10-21  8:59 ` Tip of the day: (@)...[(R)...] is equal to (@M)...:#... and faster by 18-21% psprint
2016-10-21 16:20   ` Bart Schaefer

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).