From 93b7df36a7620f1980baefee237bfac730079a6f Mon Sep 17 00:00:00 2001 From: Matthew Fernandez Date: Fri, 26 Feb 2021 20:10:31 -0800 Subject: [PATCH] remove intermediate buffer in libxdot MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/lib/xdot/xdot.c b/lib/xdot/xdot.c index 479e1239a..131eac37a 100644 --- a/lib/xdot/xdot.c +++ b/lib/xdot/xdot.c @@ -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) -- 2.50.0