Skip to contentSkip to conversation

perf: Replace malloc with alloca in hot path

PATCH In review Santosh Jodh 3 participants 4 messages Jan 30, 2012 – Feb 7, 2012

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 perf: Replace malloc with alloca in hot path

Replace malloc with alloc in hot paths for improved performance.

tools/libxc/xc_linux_osdep.c +2 -5
diff -r e2722b24dc09 -r 4952090d35e0 tools/libxc/xc_linux_osdep.c
@@ -242,7 +242,7 @@ static void *linux_privcmd_map_foreign_b
          * IOCTL_PRIVCMD_MMAPBATCH_V2 is not supported - fall back to
          * IOCTL_PRIVCMD_MMAPBATCH.
          */
-        xen_pfn_t *pfn = malloc(num * sizeof(*pfn));
+        xen_pfn_t *pfn = alloca(num * sizeof(*pfn));
 
         if ( pfn )
         {
@@ -288,8 +288,6 @@ static void *linux_privcmd_map_foreign_b
                 break;
             }
 
-            free(pfn);
-
             if ( rc == -ENOENT && i == num )
                 rc = 0;
             else if ( rc )
@@ -524,7 +522,7 @@ static void *linux_gnttab_grant_map(xc_g
     if (flags & XC_GRANT_MAP_SINGLE_DOMAIN)
         domids_stride = 0;
 
-    map = malloc(sizeof(*map) +
+    map = alloca(sizeof(*map) +
                  (count - 1) * sizeof(struct ioctl_gntdev_map_grant_ref));
     if ( map == NULL )
         return NULL;
@@ -598,7 +596,6 @@ static void *linux_gnttab_grant_map(xc_g
     }
 
  out:
-    free(map);
 
     return addr;
 }
Acked-by on v1 1/1
#1 lore raw
Quoted 1 line · Santosh Jodh

Replace malloc with alloc in hot paths for improved performance.

This is a hot path in something like a userspace PV backend which is frequently mapping guest domain pages, or something like that?

Quoted 1 line
Quoted 39 lines

diff -r e2722b24dc09 -r 4952090d35e0 tools/libxc/xc_linux_osdep.c

tools/libxc/xc_linux_osdep.c +2 -5
@@ -242,7 +242,7 @@ static void *linux_privcmd_map_foreign_b
          * IOCTL_PRIVCMD_MMAPBATCH_V2 is not supported - fall back to
          * IOCTL_PRIVCMD_MMAPBATCH.
          */
-        xen_pfn_t *pfn = malloc(num * sizeof(*pfn));
+        xen_pfn_t *pfn = alloca(num * sizeof(*pfn));
 
         if ( pfn )
         {
@@ -288,8 +288,6 @@ static void *linux_privcmd_map_foreign_b
                 break;
             }
 
-            free(pfn);
-
             if ( rc == -ENOENT && i == num )
                 rc = 0;
             else if ( rc )
@@ -524,7 +522,7 @@ static void *linux_gnttab_grant_map(xc_g
     if (flags & XC_GRANT_MAP_SINGLE_DOMAIN)
         domids_stride = 0;
 
-    map = malloc(sizeof(*map) +
+    map = alloca(sizeof(*map) +
                  (count - 1) * sizeof(struct ioctl_gntdev_map_grant_ref));
     if ( map == NULL )
         return NULL;
@@ -598,7 +596,6 @@ static void *linux_gnttab_grant_map(xc_g
     }
 
  out:
-    free(map);
 
     return addr;
 }
author Acked-by Ian Campbell
#2 lore raw

Yes - that is correct. This is used in the IO path for user mode block backend.

Thanks,
Santosh

-----Original Message-----
From: Ian Campbell
Sent: Wednesday, February 01, 2012 8:15 AM
To: Santosh Jodh
Cc: [email protected]
Subject: Re: [PATCH] perf: Replace malloc with alloca in hot path

Quoted 1 line · Santosh Jodh

Replace malloc with alloc in hot paths for improved performance.

This is a hot path in something like a userspace PV backend which is frequently mapping guest domain pages, or something like that?

Quoted 1 line
Quoted 39 lines

diff -r e2722b24dc09 -r 4952090d35e0 tools/libxc/xc_linux_osdep.c

tools/libxc/xc_linux_osdep.c +2 -5
@@ -242,7 +242,7 @@ static void *linux_privcmd_map_foreign_b
          * IOCTL_PRIVCMD_MMAPBATCH_V2 is not supported - fall back to
          * IOCTL_PRIVCMD_MMAPBATCH.
          */
-        xen_pfn_t *pfn = malloc(num * sizeof(*pfn));
+        xen_pfn_t *pfn = alloca(num * sizeof(*pfn));
 
         if ( pfn )
         {
@@ -288,8 +288,6 @@ static void *linux_privcmd_map_foreign_b
                 break;
             }
 
-            free(pfn);
-
             if ( rc == -ENOENT && i == num )
                 rc = 0;
             else if ( rc )
@@ -524,7 +522,7 @@ static void *linux_gnttab_grant_map(xc_g
     if (flags & XC_GRANT_MAP_SINGLE_DOMAIN)
         domids_stride = 0;
 
-    map = malloc(sizeof(*map) +
+    map = alloca(sizeof(*map) +
                  (count - 1) * sizeof(struct ioctl_gntdev_map_grant_ref));
     if ( map == NULL )
         return NULL;
@@ -598,7 +596,6 @@ static void *linux_gnttab_grant_map(xc_g
     }
 
  out:
-    free(map);
 
     return addr;
 }
#3 lore raw
In reply to Ian Campbell

Ian Campbell writes ("Re: [Xen-devel] [PATCH] perf: Replace malloc with alloca in hot path"):

Quoted 9 lines · Santosh Jodh
Quoted 1 line

Replace malloc with alloc in hot paths for improved performance.

This is a hot path in something like a userspace PV backend which is frequently mapping guest domain pages, or something like that?

Quoted 1 line