[PATCH 1/2] Simplify rtl_alloc_cache somewhat

Sebastian Spaeth Sebastian at SSpaeth.de
Fri Nov 26 04:25:11 PST 2010


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 at 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

--=-=-=--


More information about the LibreOffice mailing list