freetype-commit
[Top][All Lists]
Advanced

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

[freetype2] ftc_resize b4622b3fd: * src/cache/ftccache.c (ftc_cache_resi


From: Werner Lemberg
Subject: [freetype2] ftc_resize b4622b3fd: * src/cache/ftccache.c (ftc_cache_resize): Improve code.
Date: Sun, 7 May 2023 10:31:17 -0400 (EDT)

branch: ftc_resize
commit b4622b3fdd7a724ef25860696f10a0cb95c5f916
Author: Alexei Podtelezhnikov <apodtele@gmail.com>
Commit: Alexei Podtelezhnikov <apodtele@gmail.com>

    * src/cache/ftccache.c (ftc_cache_resize): Improve code.
    
    Redefine local variables to improve readability and flow.
---
 src/cache/ftccache.c | 53 ++++++++++++++++++++--------------------------------
 1 file changed, 20 insertions(+), 33 deletions(-)

diff --git a/src/cache/ftccache.c b/src/cache/ftccache.c
index ef7369d0a..b51a11f16 100644
--- a/src/cache/ftccache.c
+++ b/src/cache/ftccache.c
@@ -114,8 +114,8 @@
     {
       FTC_Node  node, *pnode;
       FT_UFast  p     = cache->p;
-      FT_UFast  mask  = cache->mask;
-      FT_UFast  count = mask + p + 1;    /* number of buckets */
+      FT_UFast  half  = cache->mask + 1;  /* half of the pool  */
+      FT_UFast  count = half + p;         /* number of buckets */
 
 
       /* do we need to expand the buckets array? */
@@ -124,23 +124,25 @@
         FTC_Node  new_list = NULL;
 
 
+        /* split a single bucket */
+        pnode = cache->buckets + p;
+
         /* try to expand the buckets array _before_ splitting
          * the bucket lists
          */
-        if ( p >= mask )
+        if ( ++p >= half )
         {
           FT_Memory  memory = cache->memory;
           FT_Error   error;
 
 
           /* if we can't expand the array, leave immediately */
-          if ( FT_RENEW_ARRAY( cache->buckets,
-                               ( mask + 1 ) * 2, ( mask + 1 ) * 4 ) )
+          if ( FT_RENEW_ARRAY( cache->buckets, 2 * half, 4 * half ) )
             break;
-        }
 
-        /* split a single bucket */
-        pnode = cache->buckets + p;
+          p           = 0;
+          cache->mask = 2 * half - 1;
+        }
 
         for (;;)
         {
@@ -148,7 +150,7 @@
           if ( !node )
             break;
 
-          if ( node->hash & ( mask + 1 ) )
+          if ( node->hash & half )
           {
             *pnode     = node->link;
             node->link = new_list;
@@ -158,53 +160,38 @@
             pnode = &node->link;
         }
 
-        cache->buckets[p + mask + 1] = new_list;
+        cache->buckets[count] = new_list;
 
         cache->slack += FTC_HASH_MAX_LOAD;
-
-        if ( p >= mask )
-        {
-          cache->mask = 2 * mask + 1;
-          cache->p    = 0;
-        }
-        else
-          cache->p = p + 1;
+        cache->p      = p;
       }
 
       /* do we need to shrink the buckets array? */
       else if ( cache->slack > (FT_Long)count * FTC_HASH_SUB_LOAD )
       {
-        FT_UFast   old_index = p + mask;
-        FTC_Node*  pold;
-
-
-        if ( old_index + 1 <= FTC_HASH_INITIAL_SIZE )
+        if ( count <= FTC_HASH_INITIAL_SIZE )
           break;
 
-        if ( p == 0 )
+        if ( p-- == 0 )
         {
           FT_Memory  memory = cache->memory;
           FT_Error   error;
 
 
           /* if we can't shrink the array, leave immediately */
-          if ( FT_QRENEW_ARRAY( cache->buckets,
-                               ( mask + 1 ) * 2, mask + 1 ) )
+          if ( FT_QRENEW_ARRAY( cache->buckets, 2 * half, half ) )
             break;
 
-          cache->mask >>= 1;
-          p             = cache->mask;
+          p           = ( half >> 1 ) - 1;
+          cache->mask = p;
         }
-        else
-          p--;
 
         pnode = cache->buckets + p;
         while ( *pnode )
           pnode = &(*pnode)->link;
 
-        pold   = cache->buckets + old_index;
-        *pnode = *pold;
-        *pold  = NULL;
+        *pnode = cache->buckets[count - 1];
+        cache->buckets[count - 1] = NULL;
 
         cache->slack -= FTC_HASH_MAX_LOAD;
         cache->p      = p;



reply via email to

[Prev in Thread] Current Thread [Next in Thread]