Code

data: Improve serialization of binary data.
authorSebastian Harl <sh@tokkee.org>
Mon, 10 Feb 2014 18:07:49 +0000 (19:07 +0100)
committerSebastian Harl <sh@tokkee.org>
Mon, 10 Feb 2014 18:07:49 +0000 (19:07 +0100)
Instead of calling strbuf_append() for each byte, precompute the string in a
simple for-loop and then append the result to the buffer. This basically
inlines a formatter specific to this use-case and, thus, should be must
faster.

src/core/data.c

index 1bb5400028223c89eca2f9a10e7ee3e81cc5cb03..8f4330eabccf173a2ddca0b6721dab2594295f70 100644 (file)
@@ -114,13 +114,27 @@ sdb_data_format(sdb_data_t *datum, sdb_strbuf_t *buf)
                        break;
                case SDB_TYPE_BINARY:
                        {
-                               size_t i;
-                               /* TODO: improve this! */
-                               sdb_strbuf_append(buf, "%s", "\"");
-                               for (i = 0; i < datum->data.binary.length; ++i)
-                                       sdb_strbuf_append(buf, "\\%x",
-                                                       (int)datum->data.binary.datum[i]);
-                               sdb_strbuf_append(buf, "%s", "\"");
+                               char tmp[4 * datum->data.binary.length + 1];
+                               size_t i, pos;
+
+                               pos = 0;
+                               for (i = 0; i < datum->data.binary.length; ++i) {
+                                       int byte = (int)datum->data.binary.datum[i];
+                                       char hex[] = {'0', '1', '2', '3', '4', '5', '6', '7',
+                                               '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
+
+                                       tmp[pos] = '\\';
+                                       ++pos;
+
+                                       if (byte > 0xf) {
+                                               tmp[pos] = hex[byte >> 4];
+                                               ++pos;
+                                       }
+                                       tmp[pos] = hex[byte & 0xf];
+                                       ++pos;
+                               }
+                               tmp[pos] = '\0';
+                               sdb_strbuf_append(buf, "\"%s\"", tmp);
                        }
                        break;
                default: