[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCHv2 1/9] maint: use lighter-weight function for straight appends



It costs quite a few processor cycles to go through printf parsing
just to determine that we only meant to append.

* src/xen/xend_internal.c (xend_op_ext): Consolidate multiple
printfs into one.
* src/qemu/qemu_command.c (qemuBuildWatchdogDevStr)
(qemuBuildUSBInputDevStr, qemuBuildSoundDevStr)
(qemuBuildSoundCodecStr, qemuBuildVideoDevStr): Likewise.
(qemuBuildCpuArgStr, qemuBuildCommandLine): Prefer virBufferAdd
over virBufferVsprintf for trivial appends.
* src/phyp/phyp_driver.c (phypExec, phypUUIDTable_Push)
(phypUUIDTable_Pull): Likewise.
* src/conf/nwfilter_conf.c (macProtocolIDFormatter)
(arpOpcodeFormatter, formatIPProtocolID, printStringItems)
(virNWFilterPrintStateMatchFlags, virNWIPAddressFormat)
(virNWFilterDefFormat): Likewise.
* src/security/virt-aa-helper.c (main): Likewise.
* src/util/sexpr.c (sexpr2string): Likewise.
* src/xenxs/xen_sxpr.c (xenFormatSxprChr): Likewise.
* src/xenxs/xen_xm.c (xenFormatXMDisk): Likewise.
---
 src/conf/nwfilter_conf.c      |   16 ++++++++--------
 src/phyp/phyp_driver.c        |    7 +++----
 src/qemu/qemu_command.c       |   30 +++++++++++-------------------
 src/security/virt-aa-helper.c |    2 +-
 src/util/sexpr.c              |    4 ++--
 src/xen/xend_internal.c       |    6 ++----
 src/xenxs/xen_sxpr.c          |    2 +-
 src/xenxs/xen_xm.c            |    4 ++--
 8 files changed, 30 insertions(+), 41 deletions(-)

diff --git a/src/conf/nwfilter_conf.c b/src/conf/nwfilter_conf.c
index 0732322..327fab3 100644
--- a/src/conf/nwfilter_conf.c
+++ b/src/conf/nwfilter_conf.c
@@ -488,7 +488,7 @@ macProtocolIDFormatter(virBufferPtr buf,
     if (intMapGetByInt(macProtoMap,
                        nwf->p.ethHdrFilter.dataProtocolID.u.u16,
                        &str)) {
-        virBufferVSprintf(buf, "%s", str);
+        virBufferAdd(buf, str, -1);
     } else {
         if (nwf->p.ethHdrFilter.dataProtocolID.datatype == DATATYPE_UINT16)
             asHex = false;
@@ -591,7 +591,7 @@ arpOpcodeFormatter(virBufferPtr buf,
     if (intMapGetByInt(arpOpcodeMap,
                        nwf->p.arpHdrFilter.dataOpcode.u.u16,
                        &str)) {
-        virBufferVSprintf(buf, "%s", str);
+        virBufferAdd(buf, str, -1);
     } else {
         virBufferVSprintf(buf, "%d", nwf->p.arpHdrFilter.dataOpcode.u.u16);
     }
@@ -653,7 +653,7 @@ formatIPProtocolID(virBufferPtr buf,
     if (intMapGetByInt(ipProtoMap,
                        nwf->p.ipHdrFilter.ipHdr.dataProtocolID.u.u8,
                        &str)) {
-        virBufferVSprintf(buf, "%s", str);
+        virBufferAdd(buf, str, -1);
     } else {
         if (nwf->p.ipHdrFilter.ipHdr.dataProtocolID.datatype == DATATYPE_UINT8)
             asHex = false;
@@ -734,8 +734,8 @@ printStringItems(virBufferPtr buf, const struct int_map *int_map,
             for (i = 0; int_map[i].val; i++) {
                 if (mask == int_map[i].attr) {
                     if (c >= 1)
-                        virBufferVSprintf(buf, "%s", sep);
-                    virBufferVSprintf(buf, "%s", int_map[i].val);
+                        virBufferAdd(buf, sep, -1);
+                    virBufferAdd(buf, int_map[i].val, -1);
                     c++;
                 }
             }
@@ -769,7 +769,7 @@ virNWFilterPrintStateMatchFlags(virBufferPtr buf, const char *prefix,
     if (!disp_none && (flags & RULE_FLAG_STATE_NONE))
         return;

-    virBufferVSprintf(buf, "%s", prefix);
+    virBufferAdd(buf, prefix, -1);

     printStringItems(buf, stateMatchMap, flags, ",");
 }
@@ -2699,7 +2699,7 @@ virNWIPAddressFormat(virBufferPtr buf, virSocketAddrPtr ipaddr)
     char *output = virSocketFormatAddr(ipaddr);

     if (output) {
-        virBufferVSprintf(buf, "%s", output);
+        virBufferAdd(buf, output, -1);
         VIR_FREE(output);
     }
 }
@@ -2936,7 +2936,7 @@ virNWFilterDefFormat(virNWFilterDefPtr def)
         xml = virNWFilterEntryFormat(def->filterEntries[i]);
         if (!xml)
             goto err_exit;
-        virBufferVSprintf(&buf, "%s", xml);
+        virBufferAdd(&buf, xml, -1);
         VIR_FREE(xml);
     }

diff --git a/src/phyp/phyp_driver.c b/src/phyp/phyp_driver.c
index 3862c9c..5742d95 100644
--- a/src/phyp/phyp_driver.c
+++ b/src/phyp/phyp_driver.c
@@ -1,4 +1,3 @@
-
 /*
  * Copyright (C) 2010-2011 Red Hat, Inc.
  * Copyright IBM Corp. 2009
@@ -156,7 +155,7 @@ phypExec(LIBSSH2_SESSION *session, const char *cmd, int *exit_status,
             rc = libssh2_channel_read(channel, buffer, buffer_size);
             if (rc > 0) {
                 bytecount += rc;
-                virBufferVSprintf(&tex_ret, "%s", buffer);
+                virBufferAdd(&tex_ret, buffer, -1);
             }
         }
         while (rc > 0);
@@ -494,7 +493,7 @@ phypUUIDTable_Push(virConnectPtr conn)
     char *remote_file = NULL;

     if (conn->uri->user != NULL) {
-        virBufferVSprintf(&username, "%s", conn->uri->user);
+        virBufferAdd(&username, conn->uri->user, -1);

         if (virBufferError(&username)) {
             virBufferFreeAndReset(&username);
@@ -711,7 +710,7 @@ phypUUIDTable_Pull(virConnectPtr conn)
     char *remote_file = NULL;

     if (conn->uri->user != NULL) {
-        virBufferVSprintf(&username, "%s", conn->uri->user);
+        virBufferAdd(&username, conn->uri->user, -1);

         if (virBufferError(&username)) {
             virBufferFreeAndReset(&username);
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index 71e5062..2205ed1 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -1798,8 +1798,7 @@ qemuBuildWatchdogDevStr(virDomainWatchdogDefPtr dev,
         goto error;
     }

-    virBufferVSprintf(&buf, "%s", model);
-    virBufferVSprintf(&buf, ",id=%s", dev->info.alias);
+    virBufferVSprintf(&buf, "%s,id=%s", model, dev->info.alias);
     if (qemuBuildDeviceAddressStr(&buf, &dev->info, qemuCaps) < 0)
         goto error;

@@ -1845,10 +1844,9 @@ qemuBuildUSBInputDevStr(virDomainInputDefPtr dev)
 {
     virBuffer buf = VIR_BUFFER_INITIALIZER;

-    virBufferVSprintf(&buf, "%s",
+    virBufferVSprintf(&buf, "%s,id=%s",
                       dev->type == VIR_DOMAIN_INPUT_TYPE_MOUSE ?
-                      "usb-mouse" : "usb-tablet");
-    virBufferVSprintf(&buf, ",id=%s", dev->info.alias);
+                      "usb-mouse" : "usb-tablet", dev->info.alias);

     if (virBufferError(&buf)) {
         virReportOOMError();
@@ -1884,8 +1882,7 @@ qemuBuildSoundDevStr(virDomainSoundDefPtr sound,
     else if (STREQ(model, "ich6"))
         model = "intel-hda";

-    virBufferVSprintf(&buf, "%s", model);
-    virBufferVSprintf(&buf, ",id=%s", sound->info.alias);
+    virBufferVSprintf(&buf, "%s,id=%s", model, sound->info.alias);
     if (qemuBuildDeviceAddressStr(&buf, &sound->info, qemuCaps) < 0)
         goto error;

@@ -1908,10 +1905,8 @@ qemuBuildSoundCodecStr(virDomainSoundDefPtr sound,
     virBuffer buf = VIR_BUFFER_INITIALIZER;
     int cad = 0;

-    virBufferVSprintf(&buf, "%s", codec);
-    virBufferVSprintf(&buf, ",id=%s-codec%d", sound->info.alias, cad);
-    virBufferVSprintf(&buf, ",bus=%s.0", sound->info.alias);
-    virBufferVSprintf(&buf, ",cad=%d", cad);
+    virBufferVSprintf(&buf, "%s,id=%s-codec%d,bus=%s.0,cad=%d",
+                      codec, sound->info.alias, cad, sound->info.alias, cad);

     if (virBufferError(&buf)) {
         virReportOOMError();
@@ -1938,8 +1933,7 @@ qemuBuildVideoDevStr(virDomainVideoDefPtr video,
         goto error;
     }

-    virBufferVSprintf(&buf, "%s", model);
-    virBufferVSprintf(&buf, ",id=%s", video->info.alias);
+    virBufferVSprintf(&buf, "%s,id=%s", model, video->info.alias);

     if (video->type == VIR_DOMAIN_VIDEO_TYPE_QXL) {
         if (video->vram > (UINT_MAX / 1024)) {
@@ -2572,7 +2566,7 @@ qemuBuildCpuArgStr(const struct qemud_driver *driver,
             goto cleanup;
         *hasHwVirt = hasSVM > 0 ? true : false;

-        virBufferVSprintf(&buf, "%s", guest->model);
+        virBufferAdd(&buf, guest->model, -1);
         for (i = 0; i < guest->nfeatures; i++) {
             char sign;
             if (guest->features[i].policy == VIR_CPU_FEATURE_DISABLE)
@@ -3128,7 +3122,7 @@ qemuBuildCommandLine(virConnectPtr conn,
                 else if (def->os.bootmenu == VIR_DOMAIN_BOOT_MENU_DISABLED)
                     virBufferVSprintf(&boot_buf, "order=%s,menu=off", boot);
             } else {
-                virBufferVSprintf(&boot_buf, "%s", boot);
+                virBufferAdd(&boot_buf, boot, -1);
             }

             virCommandAddArgBuffer(cmd, &boot_buf);
@@ -3291,8 +3285,7 @@ qemuBuildCommandLine(virConnectPtr conn,
                                           host->name,
                                           host->port);
                     } else {
-                        virBufferVSprintf(&rbd_hosts, "%s",
-                                          host->name);
+                        virBufferAdd(&rbd_hosts, host->name, -1);
                     }
                 }
             }
@@ -3414,8 +3407,7 @@ qemuBuildCommandLine(virConnectPtr conn,
                                               host->name,
                                               host->port);
                         } else {
-                            virBufferVSprintf(&rbd_hosts, "%s",
-                                              host->name);
+                            virBufferAdd(&rbd_hosts, host->name, -1);
                         }
                     }
                     break;
diff --git a/src/security/virt-aa-helper.c b/src/security/virt-aa-helper.c
index bb716e6..08ff53c 100644
--- a/src/security/virt-aa-helper.c
+++ b/src/security/virt-aa-helper.c
@@ -1204,7 +1204,7 @@ main(int argc, char **argv)
             virBufferVSprintf(&buf, "  \"%s/run/libvirt/**/%s.pid\" rwk,\n",
                               LOCALSTATEDIR, ctl->def->name);
             if (ctl->files)
-                virBufferVSprintf(&buf, "%s", ctl->files);
+                virBufferAdd(&buf, ctl->files, -1);
         }

         if (virBufferError(&buf)) {
diff --git a/src/util/sexpr.c b/src/util/sexpr.c
index ae1692a..da3d4b3 100644
--- a/src/util/sexpr.c
+++ b/src/util/sexpr.c
@@ -1,7 +1,7 @@
 /*
  * sexpr.c : S-Expression routines to communicate with the Xen Daemon
  *
- * Copyright (C) 2010 Red Hat, Inc.
+ * Copyright (C) 2010-2011 Red Hat, Inc.
  * Copyright (C) 2005 Anthony Liguori <aliguori us ibm com>
  *
  *  This file is subject to the terms and conditions of the GNU Lesser General
@@ -234,7 +234,7 @@ sexpr2string(const struct sexpr *sexpr, virBufferPtr buffer)
             strchr(sexpr->u.value, '('))
             virBufferVSprintf(buffer, "'%s'", sexpr->u.value);
         else
-            virBufferVSprintf(buffer, "%s", sexpr->u.value);
+            virBufferAdd(buffer, sexpr->u.value, -1);

         break;
     case SEXPR_NIL:
diff --git a/src/xen/xend_internal.c b/src/xen/xend_internal.c
index 04122ba..57422d3 100644
--- a/src/xen/xend_internal.c
+++ b/src/xen/xend_internal.c
@@ -487,13 +487,11 @@ xend_op_ext(virConnectPtr xend, const char *path, const char *key, va_list ap)
     while (k) {
         v = va_arg(ap, const char *);

-        virBufferVSprintf(&buf, "%s", k);
-        virBufferVSprintf(&buf, "%s", "=");
-        virBufferVSprintf(&buf, "%s", v);
+        virBufferVSprintf(&buf, "%s=%s", k, v);
         k = va_arg(ap, const char *);

         if (k)
-            virBufferVSprintf(&buf, "%s", "&");
+            virBufferAddChar(&buf, '&');
     }

     if (virBufferError(&buf)) {
diff --git a/src/xenxs/xen_sxpr.c b/src/xenxs/xen_sxpr.c
index b590517..d2ec370 100644
--- a/src/xenxs/xen_sxpr.c
+++ b/src/xenxs/xen_sxpr.c
@@ -1514,7 +1514,7 @@ xenFormatSxprChr(virDomainChrDefPtr def,
     case VIR_DOMAIN_CHR_TYPE_STDIO:
     case VIR_DOMAIN_CHR_TYPE_VC:
     case VIR_DOMAIN_CHR_TYPE_PTY:
-        virBufferVSprintf(buf, "%s", type);
+        virBufferAdd(buf, type, -1);
         break;

     case VIR_DOMAIN_CHR_TYPE_FILE:
diff --git a/src/xenxs/xen_xm.c b/src/xenxs/xen_xm.c
index 89f75a5..dbcaf15 100644
--- a/src/xenxs/xen_xm.c
+++ b/src/xenxs/xen_xm.c
@@ -1115,13 +1115,13 @@ static int xenFormatXMDisk(virConfValuePtr list,
                 goto cleanup;
             }
         }
-        virBufferVSprintf(&buf, "%s", disk->src);
+        virBufferAdd(&buf, disk->src, -1);
     }
     virBufferAddLit(&buf, ",");
     if (hvm && xendConfigVersion == 1)
         virBufferAddLit(&buf, "ioemu:");

-    virBufferVSprintf(&buf, "%s", disk->dst);
+    virBufferAdd(&buf, disk->dst, -1);
     if (disk->device == VIR_DOMAIN_DISK_DEVICE_CDROM)
         virBufferAddLit(&buf, ":cdrom");

-- 
1.7.4.2


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]