zsh-workers
 help / color / mirror / code / Atom feed
* Possible huge setarrvalue optimization
@ 2016-11-18  6:17 Sebastian Gniazdowski
  2016-11-18  7:09 ` Sebastian Gniazdowski
  2016-11-18  9:32 ` Sebastian Gniazdowski
  0 siblings, 2 replies; 13+ messages in thread
From: Sebastian Gniazdowski @ 2016-11-18  6:17 UTC (permalink / raw)
  To: zsh-workers

Hello,
in params.c / setarrvalue there is code:

        post_assignment_length = v->start + arrlen(val);
        if (v->end <= pre_assignment_length)
            post_assignment_length += pre_assignment_length - v->end +
            1;

        p = new = (char **) zshcalloc(sizeof(char *)
                                      * (post_assignment_length + 1));

        for (i = 0; i < v->start; i++)
            *p++ = i < pre_assignment_length ? ztrdup(*q++) :
            ztrdup("");
        for (r = val; *r;) {
            /* Give away ownership of the string */
            *p++ = *r++;
        }
        if (v->end < pre_assignment_length)
            for (q = old + v->end; *q;)
                *p++ = ztrdup(*q++);
        *p = NULL;

        v->pm->gsu.a->setfn(v->pm, new);

The point is that the code is also run when doing:

a=( a b c d )
a[1]="x"

The optimization can be: no allocation of new array when size of output
array doesn't change. The problem is that following debug:

        fprintf( _F, "%d %d\n", pre_assignment_length,
        post_assignment_length );

Shows "4 5" for the a[1]="x" assignment. Shows the same for a+=( "x" ).
If this would be fixed, one could detect that pre_assignment_length ==
post_assignment_length, and skip allocation, only do:

        for (r = val; *r;) {
            /* Give away ownership of the string */
            *p++ = *r++;
        }

This would be a huge optimization. Has anyone idea of how the
pre_assignment_length / post_assignment_length computation works, and
why it's wrong for a[1]="x" ?

-- 
  Sebastian Gniazdowski
  psprint@fastmail.com


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

end of thread, other threads:[~2017-01-05 15:50 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-11-18  6:17 Possible huge setarrvalue optimization Sebastian Gniazdowski
2016-11-18  7:09 ` Sebastian Gniazdowski
2016-11-18  9:32 ` Sebastian Gniazdowski
2016-11-18 12:20   ` Sebastian Gniazdowski
2016-11-20 11:46     ` Daniel Shahaf
2016-11-20 17:41       ` Bart Schaefer
2016-11-20 20:54         ` Sebastian Gniazdowski
2016-11-20 21:19         ` Peter Stephenson
2016-12-24 17:19         ` Daniel Shahaf
2017-01-04 18:31           ` Sebastian Gniazdowski
2017-01-05  4:13             ` Daniel Shahaf
2017-01-05 13:32               ` Sebastian Gniazdowski
2017-01-05 15:47                 ` Daniel Shahaf

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