optimization for buffer write easy cases

Sun, 01 May 2022 11:54:10 +0200

author
Mike Becker <universe@uap-core.de>
date
Sun, 01 May 2022 11:54:10 +0200
changeset 543
7b9114030ca4
parent 542
45bcfd152f85
child 544
2e73456e5f84

optimization for buffer write easy cases

src/buffer.c file | annotate | diff | comparison | revisions
--- a/src/buffer.c	Sun May 01 11:46:54 2022 +0200
+++ b/src/buffer.c	Sun May 01 11:54:10 2022 +0200
@@ -140,7 +140,16 @@
         size_t nitems,
         CxBuffer *buffer
 ) {
-    // TODO: optimize for special case size == nitems == 1
+    // optimize for easy case
+    if (size == 1 && (buffer->capacity - buffer->pos) >= nitems) {
+        memcpy(buffer->bytes + buffer->pos, ptr, nitems);
+        buffer->pos += nitems;
+        if (buffer->pos > buffer->size) {
+            buffer->size = buffer->pos;
+        }
+        return nitems;
+    }
+
     size_t len;
     if (cx_szmul(size, nitems, &len)) {
         return 0;

mercurial