# Question on some simple codes in ngx\_buf.c

**URL:** <https://www.ruby-forum.com/t/question-on-some-simple-codes-in-ngx-buf-c/238538>\
**Category:** NGINX\
**Created:** [January 16, 2014, 8:05am UTC](https://www.ruby-forum.com/t/question-on-some-simple-codes-in-ngx-buf-c/238538 "2014-01-16T08:05:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Detlef\_R](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@Detlef\_R](https://www.ruby-forum.com/u/Detlef_R)\
**Post date:** [January 16, 2014, 8:05am UTC](https://www.ruby-forum.com/t/question-on-some-simple-codes-in-ngx-buf-c/238538/1 "2014-01-16T08:05:02Z")

</div>

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:

> **[question on some simple codes in ngx\_buf.c](https://forum.nginx.org/read.php?2,246499,246499#msg-246499)**
>
> Hello there, code snippet in the definition of ngx\_chain\_add\_copy in ngx\_buf.c: ll = chain;

---

<div class="post-metadata">

**Author:** ![microwish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/919ad9/32.png) [@microwish](https://www.ruby-forum.com/u/microwish)\
**Post date:** [January 16, 2014, 5:50pm UTC](https://www.ruby-forum.com/t/question-on-some-simple-codes-in-ngx-buf-c/238538/2 "2014-01-16T17:50:58Z")

</div>

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/](http://nginx.org/)
