# Buffer chains optimisation

**URL:** <https://www.ruby-forum.com/t/buffer-chains-optimisation/135862>\
**Category:** NGINX\
**Created:** [April 25, 2008, 7:17pm UTC](https://www.ruby-forum.com/t/buffer-chains-optimisation/135862 "2008-04-25T19:17:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![vchav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/13edae/32.png) [@vchav](https://www.ruby-forum.com/u/vchav)\
**Post date:** [April 25, 2008, 7:17pm UTC](https://www.ruby-forum.com/t/buffer-chains-optimisation/135862/1 "2008-04-25T19:17:30Z")

</div>

Hi,

I read the guide about the Nginx module development and I don’t find any  
description about how buffer chains are processed. For example, I send a  
file line by line and I would like to know if it’s optimized to create  
one buffer (ngx\_buf\_t) and one chain (ngx\_chain\_t) for each line or to  
concatenate entire document and send it with only one buffer/chain.

Thanks in advance

---

<div class="post-metadata">

**Author:** ![vchav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/13edae/32.png) [@vchav](https://www.ruby-forum.com/u/vchav)\
**Post date:** [April 25, 2008, 8:01pm UTC](https://www.ruby-forum.com/t/buffer-chains-optimisation/135862/2 "2008-04-25T20:01:50Z")

</div>

On Fri, Apr 25, 2008 at 07:17:30PM +0200, Chavelle V. wrote:

> I read the guide about the Nginx module development and I don’t find any  
> description about how buffer chains are processed. For example, I send a  
> file line by line and I would like to know if it’s optimized to create  
> one buffer (ngx\_buf\_t) and one chain (ngx\_chain\_t) for each line or to  
> concatenate entire document and send it with only one buffer/chain.

nginx does some memory/file pointer gathering before passing data to  
writev()/sendfile(), i.e, two neighbour memory/file blocks became one.  
But nginx does not copy data to concatenate them.

---

<div class="post-metadata">

**Author:** ![vchav](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/13edae/32.png) [@vchav](https://www.ruby-forum.com/u/vchav)\
**Post date:** [April 25, 2008, 8:40pm UTC](https://www.ruby-forum.com/t/buffer-chains-optimisation/135862/3 "2008-04-25T20:40:01Z")

</div>

Chavelle V. \<lists@…\> writes:

> I read the guide about the Nginx module development and I don’t find any  
> description about how buffer chains are processed. For example, I send a  
> file line by line and I would like to know if it’s optimized to create  
> one buffer (ngx\_buf\_t) and one chain (ngx\_chain\_t) for each line or to  
> concatenate entire document and send it with only one buffer/chain.

One buffer and one chain is OK. You can use a static string to setup a  
buffer  
(without copying) or a dynamically allocated string (via the pool of  
memory  
automagically freed when the request is done); that way you can mix  
buffers in a  
chain with different kind of data. So in your case one buffer for the  
entire  
document should be a good approach.

Best regards.
