This is an automated email from the git hooks/post-receive script.

guillem pushed a commit to branch master
in repository dpkg.

View the commit online:
https://git.dpkg.org/cgit/dpkg/dpkg.git/commit/?id=0ec260b39b0d23d262ddff07d8954f11c8d954c7

commit 0ec260b39b0d23d262ddff07d8954f11c8d954c7
Author: Guillem Jover <[email protected]>
AuthorDate: Sat Nov 23 19:44:12 2019 +0100

    libdpkg: Only use varbuf_printf() in pkg_format_show() when necessary
    
    Calling varbuf_printf() to add a simple string is quite expensive. We
    should only use it when we need to format the arguments.
---
 debian/changelog      |  2 ++
 lib/dpkg/pkg-format.c | 32 +++++++++++++++++++-------------
 2 files changed, 21 insertions(+), 13 deletions(-)

diff --git a/debian/changelog b/debian/changelog
index 7a564516a..6189967ec 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -113,6 +113,8 @@ dpkg (1.20.0) UNRELEASED; urgency=medium
     - libdpkg: Add missing symbols to the version map.
     - libdpkg: Fix fiemap memory layout usage that confuses gcc 10 to emit a
       warning.
+    - libdpkg: Only use varbuf_printf() in pkg_format_show() when necessary.
+      This should speed up «dpkg-query --show» formatting.
   * Build system:
     - Bump minimal Perl version to 5.24.1.
     - Add a serial versioning to the m4 files.
diff --git a/lib/dpkg/pkg-format.c b/lib/dpkg/pkg-format.c
index 1985df068..d600a947f 100644
--- a/lib/dpkg/pkg-format.c
+++ b/lib/dpkg/pkg-format.c
@@ -388,6 +388,21 @@ static const struct fieldinfo virtinfos[] = {
        { NULL },
 };
 
+static void
+pkg_format_item(struct varbuf *vb,
+                const struct pkg_format_node *node, const char *str)
+{
+       if (node->width == 0)
+               varbuf_add_str(vb, str);
+       else {
+               char fmt[16];
+
+               snprintf(fmt, sizeof(fmt), "%%%s%zus",
+                        ((node->pad) ? "-" : ""), node->width);
+               varbuf_printf(vb, fmt, str);
+       }
+}
+
 void
 pkg_format_show(const struct pkg_format_node *head,
                 struct pkginfo *pkg, struct pkgbin *pkgbin)
@@ -396,19 +411,10 @@ pkg_format_show(const struct pkg_format_node *head,
        struct varbuf vb = VARBUF_INIT, fb = VARBUF_INIT, wb = VARBUF_INIT;
 
        for (node = head; node; node = node->next) {
-               bool ok;
-               char fmt[16];
-
-               ok = false;
-
-               if (node->width > 0)
-                       snprintf(fmt, 16, "%%%s%zus",
-                                ((node->pad) ? "-" : ""), node->width);
-               else
-                       strcpy(fmt, "%s");
+               bool ok = false;
 
                if (node->type == PKG_FORMAT_STRING) {
-                       varbuf_printf(&fb, fmt, node->data);
+                       pkg_format_item(&fb, node, node->data);
                        ok = true;
                } else if (node->type == PKG_FORMAT_FIELD) {
                        const struct fieldinfo *fip;
@@ -421,7 +427,7 @@ pkg_format_show(const struct pkg_format_node *head,
                                fip->wcall(&wb, pkg, pkgbin, 0, fip);
 
                                varbuf_end_str(&wb);
-                               varbuf_printf(&fb, fmt, wb.buf);
+                               pkg_format_item(&fb, node, wb.buf);
                                varbuf_reset(&wb);
                                ok = true;
                        } else {
@@ -429,7 +435,7 @@ pkg_format_show(const struct pkg_format_node *head,
 
                                afp = find_arbfield_info(pkgbin->arbs, 
node->data);
                                if (afp) {
-                                       varbuf_printf(&fb, fmt, afp->value);
+                                       pkg_format_item(&fb, node, afp->value);
                                        ok = true;
                                }
                        }

-- 
Dpkg.Org's dpkg

Reply via email to