Question on some simple codes in ngx_buf.c

Hello there,

code snippet in the definition of ngx_chain_add_copy in ngx_buf.c:

ll = chain;



for (cl = *chain; cl; cl = cl->next) {
    ll = &cl->next;
}

Why is ll assigned repeatedly? I’m sorry for failed thinking out any
necessity.

And I modified the above as the following. Is it OK?

if (*chain) {
    for (cl = *chain; cl->next; cl = cl->next) { /* void */ }
    ll = &cl->next;
} else {
    ll = chain;
}

Thank you very much.

Posted at Nginx Forum:

Hello!

On Thu, Jan 16, 2014 at 02:04:45AM -0500, microwish wrote:

    ll = &cl->next;
    for (cl = *chain; cl->next; cl = cl->next) { /* void */ }
    ll = &cl->next;
} else {
    ll = chain;
}

Thank you very much.

The code snippets look equivalent from logical point of view.
From performance point of view - they are mostly equivalent too,
as cl->next address anyway needs to be loaded on each cycle
iteration and will be available in a register, so assignment is
essentially a nop. The code currently used is shorter though, and
easier to read.


Maxim D.
http://nginx.org/