Date: prev next · Thread: first prev next last
2010 Archives by date, by thread · List index


As rtl_alloc_cache is called 600k times on writer startup I looked at
it. 

I made the function somewhat simpler to read by removing an indention
level (unfortunately making the diff harder to read), and also tried to
microoptimize a tiny bit.

Would the compiler be smart enough to optimize this on compilation to
identical code or is the new one a tiny tad nicer?

-    if (n >= SAL_MAX_SIZE - (RTL_MEMALIGN + RTL_MEMALIGN - 1))
+    if (n > SAL_MAX_SIZE - 2 * RTL_MEMALIGN

I reran with callgrind and it *seemed* to use a bit less CPU
instructions but I am not experienced enough to really judge this.

The new code is easier to read in any case (I think). Would someone have
a quick look at the patch?

From 4b1be5485c54b0e2cdf3c1e88479cbbdc7703888 Mon Sep 17 00:00:00 2001
From: Sebastian Spaeth <Sebastian@SSpaeth.de>
Date: Fri, 26 Nov 2010 13:25:11 +0100
Subject: [PATCH 1/2] Simplify rtl_alloc_cache somewhat

1) Simplify the calculation somewhat by replacing this:

-    if (n >= SAL_MAX_SIZE - (RTL_MEMALIGN + RTL_MEMALIGN - 1))
+    if (n > SAL_MAX_SIZE - 2 * RTL_MEMALIGN

This function is called 600k times on writer startup, so even microoptimizations here might matter.

2) Remove one indentation level by returning early rather than indenting the whole function in an 
if() {}.


Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
---
 sal/rtl/source/alloc_global.c |   55 +++++++++++++++++++++--------------------
 1 files changed, 28 insertions(+), 27 deletions(-)

diff --git a/sal/rtl/source/alloc_global.c b/sal/rtl/source/alloc_global.c
index abb6072..0ce519b 100644
--- a/sal/rtl/source/alloc_global.c
+++ b/sal/rtl/source/alloc_global.c
@@ -202,40 +202,40 @@ void *
 SAL_CALL rtl_allocateMemory (sal_Size n) SAL_THROW_EXTERN_C()
 {
     void * p = 0;
-    if (n > 0)
+
+    /* Don't do anything if requested size is 0 */
+    if (n == 0)
+        return 0;
+
+    OSL_ASSERT(RTL_MEMALIGN >= sizeof(sal_Size));
+    if (n > SAL_MAX_SIZE - 2 * RTL_MEMALIGN)
     {
-        char *     addr;
-        sal_Size   size = RTL_MEMORY_ALIGN(n + RTL_MEMALIGN, RTL_MEMALIGN);
+        /* requested size too large for roundup alignment */
+        return 0;
+    }
 
-        OSL_ASSERT(RTL_MEMALIGN >= sizeof(sal_Size));
-        if (n >= SAL_MAX_SIZE - (RTL_MEMALIGN + RTL_MEMALIGN - 1))
-        {
-            /* requested size too large for roundup alignment */
-            return 0;
-        }
+    char *     addr;
+    sal_Size   size = RTL_MEMORY_ALIGN(n + RTL_MEMALIGN, RTL_MEMALIGN);
 
 try_alloc:
-        if (size <= RTL_MEMORY_CACHED_LIMIT)
-            addr = (char*)rtl_cache_alloc(g_alloc_table[(size - 1) >> RTL_MEMALIGN_SHIFT]);
-        else
-            addr = (char*)rtl_arena_alloc (gp_alloc_arena, &size);
+    /* RTL_MEMORY_CACHED_LIMIT = 4 * 4096 */
+    if (size <= RTL_MEMORY_CACHED_LIMIT)
+        addr = (char*)rtl_cache_alloc(g_alloc_table[(size - 1) >> RTL_MEMALIGN_SHIFT]);
+    else
+        addr = (char*)rtl_arena_alloc (gp_alloc_arena, &size);
 
-        if (addr != 0)
-        {
-            ((sal_Size*)(addr))[0] = size;
-            p = addr + RTL_MEMALIGN;
-        }
-        else if (gp_alloc_arena == 0)
-        {
-            if (rtl_memory_init())
-            {
-                /* try again */
-                goto try_alloc;
-            }
-        }
+    if (addr)
+    {
+        ((sal_Size*)(addr))[0] = size;
+        p = addr + RTL_MEMALIGN;
     }
-    return (p);
+    else if (gp_alloc_arena == 0 && rtl_memory_init())
+    {
+        /* init memory succeeded, so try again */
+        goto try_alloc;
+    }
+
+    return p;
 }
 
 /* ================================================================= */
-- 
1.7.1


Sebastian

Context


Privacy Policy | Impressum (Legal Info) | Copyright information: Unless otherwise specified, all text and images on this website are licensed under the Creative Commons Attribution-Share Alike 3.0 License. This does not include the source code of LibreOffice, which is licensed under the Mozilla Public License (MPLv2). "LibreOffice" and "The Document Foundation" are registered trademarks of their corresponding registered owners or are in actual use as trademarks in one or more countries. Their respective logos and icons are also subject to international copyright laws. Use thereof is explained in our trademark policy.