Code

write_redis plugin: Use an "append" macro to fill the values buffer.
authorFlorian Forster <octo@leeloo.lan.home.verplant.org>
Tue, 17 Aug 2010 14:09:55 +0000 (16:09 +0200)
committerFlorian Forster <octo@leeloo.lan.home.verplant.org>
Tue, 17 Aug 2010 14:09:55 +0000 (16:09 +0200)
This way we don't have to copy the entire buffer, which is hopefully
faster.

src/write_redis.c

index 0b15e0fa95c4e2e37d3ee973bbfca9a47976fe9d..c995a906ed2399c0c6413f83300d96f493a68e16 100644 (file)
@@ -55,36 +55,51 @@ static int wr_write (const data_set_t *ds, /* {{{ */
   wr_node_t *node = ud->data;
   char key[512];
   char value[512];
-  char tmp[512];
+  size_t value_size;
+  char *value_ptr;
   int status;
   int i;
 
-  status = FORMAT_VL (tmp, sizeof (tmp), vl);
+  status = FORMAT_VL (value, sizeof (value), vl);
   if (status != 0)
     return (status);
-  ssnprintf (key, sizeof (key), "collectd/%s", tmp);
-
-  ssnprintf (value, sizeof (value), "%lu", (unsigned long) vl->time);
+  ssnprintf (key, sizeof (key), "collectd/%s", value);
+
+  memset (value, 0, sizeof (value));
+  value_size = sizeof (value);
+  value_ptr = &value[0];
+
+#define APPEND(...) do {                                             \
+  status = snprintf (value_ptr, value_size, __VA_ARGS__);            \
+  if (((size_t) status) > value_size)                                \
+  {                                                                  \
+    value_ptr += value_size;                                         \
+    value_size = 0;                                                  \
+  }                                                                  \
+  else                                                               \
+  {                                                                  \
+    value_ptr += status;                                             \
+    value_size -= status;                                            \
+  }                                                                  \
+} while (0)
+
+  APPEND ("%lu", (unsigned long) vl->time);
   for (i = 0; i < ds->ds_num; i++)
   {
     if (ds->ds[i].type == DS_TYPE_COUNTER)
-      ssnprintf (tmp, sizeof (tmp), "%s:%llu",
-          value, vl->values[i].counter);
+      APPEND ("%llu", vl->values[i].counter);
     else if (ds->ds[i].type == DS_TYPE_GAUGE)
-      ssnprintf (tmp, sizeof (tmp), "%s:%g",
-          value, vl->values[i].gauge);
+      APPEND ("%g", vl->values[i].gauge);
     else if (ds->ds[i].type == DS_TYPE_DERIVE)
-      ssnprintf (tmp, sizeof (tmp), "%s:%"PRIi64,
-          value, vl->values[i].derive);
+      APPEND ("%"PRIi64, vl->values[i].derive);
     else if (ds->ds[i].type == DS_TYPE_ABSOLUTE)
-      ssnprintf (tmp, sizeof (tmp), "%s:%"PRIu64,
-          value, vl->values[i].absolute);
+      APPEND ("%"PRIu64, vl->values[i].absolute);
     else
       assert (23 == 42);
-
-    memcpy (value, tmp, sizeof (value));
   }
 
+#undef APPEND
+
   pthread_mutex_lock (&node->lock);
 
   if (node->conn == NULL)