diff options
| author | Olivier Gayot <duskcoder@gmail.com> | 2013-12-22 00:27:58 +0100 | 
|---|---|---|
| committer | Olivier Gayot <duskcoder@gmail.com> | 2014-01-18 13:44:10 +0100 | 
| commit | 0aac38f5b25f25b2472f991e3275dc976a9aa47e (patch) | |
| tree | da85b0f9c824650f0c28ab625400b1b2bc05e29c /src | |
| parent | 24dc23e78ca4b49d599093f7936ddcb9da818ab6 (diff) | |
rb: improve the performance of rb_put
when we call rb_put with a size greater than the size of the ring buffer
itself, we used to copy in a loop, overriding our new written datas.
it was very inefficient so we avoid these computations
closes #1
Diffstat (limited to 'src')
| -rw-r--r-- | src/rb.c | 17 | 
1 files changed, 17 insertions, 0 deletions
@@ -38,6 +38,23 @@ t_rb *rb_put(t_rb *rb, const void *src, size_t n)  {      size_t size; +    size = rb_get_size(rb); +    if (n >= size) { +        /* we will override the whole ring buffer */ + +        /* the first bytes of src will not be copied, anyway */ +        src += (n - size); + +        memcpy(rb->buffer, src , size); + +        /* hardcode the values */ +        rb->size_filled = size; +        rb->off_w       = 0; +        rb->off_r       = 0; + +        return rb; +    } +      /* loop until n bytes of src have been written */      while (n != 0) {          size = MIN(n, rb_get_size(rb) - rb->off_w);  | 
