]> granicus.if.org Git - graphviz/commitdiff
remove intermediate buffer in libxdot
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 27 Feb 2021 04:10:31 +0000 (20:10 -0800)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 6 Mar 2021 01:30:19 +0000 (17:30 -0800)
The function jsonString was trying to optimize processing by constructing a
string in an intermediate agxbuf before passing this buffer to the caller’s
print callback. An implicit assumption is that the callback is significantly
more expensive than outputting to the intermediate buffer. This is not true. The
only callbacks ever passed to this function are agxbprint itself and fputs,
which does efficient buffering. Both of these are at least as efficient as
agxbuf, making the trip through the intermediate buffer actually a
*de-optimization*.

lib/xdot/xdot.c

index 479e1239aad5c4e86ec2f29ffcdf5ec5049a6f57..131eac37a8c972e85501e02d15e074c0fa553d28 100644 (file)
@@ -728,19 +728,18 @@ static void jsonPolyline(xdot_polyline * p, pf print, void *info)
 
 static void jsonString(char *p, pf print, void *info)
 {
-    unsigned char c, buf[BUFSIZ];
-    agxbuf xb;
+    char c;
 
-    agxbinit(&xb, BUFSIZ, buf);
-    agxbputc(&xb, '"');
+    print("\"", info);
     while ((c = *p++)) {
-       if (c == '"') agxbput("\\\"", &xb);
-       else if (c == '\\') agxbput("\\\\", &xb);
-       else agxbputc(&xb, c);
+       if (c == '"') print("\\\"", info);
+       else if (c == '\\') print("\\\\", info);
+       else {
+               char buf[2] = { c, '\0' };
+               print(buf, info);
+       }
     }
-    agxbputc(&xb, '"');
-    print(agxbuse(&xb), info);
-    agxbfree(&xb);
+    print("\"", info);
 }
 
 static void jsonXDot_Op(xdot_op * op, pf print, void *info, int more)