[devhelp] DhLink: avoid some memory padding inside struct



commit 326d80ff551cc29cde3380e6de217d27217184de
Author: Sébastien Wilmet <swilmet gnome org>
Date:   Thu Dec 7 18:41:11 2017 +0100

    DhLink: avoid some memory padding inside struct
    
    This was a recent regression caused by the previous commit in DhLink.
    But there was no comments to explain the order.
    
    The guint is 4 bytes long on my machine (x86_64), there was 4 bytes of
    padding for it. Plus 6 bytes of padding after the bit fields, for a
    total of 64 bytes for the DhLink. Now there is only 2 bytes of padding,
    for a total of 56 bytes (on x86_64).
    
    With the gtk-doc documentation installed on my computer, the DhLink
    struct is allocated more than 60k times, so optimizing it is worth it.

 src/dh-link.c |   11 +++++++++--
 1 files changed, 9 insertions(+), 2 deletions(-)
---
diff --git a/src/dh-link.c b/src/dh-link.c
index 478ab38..002cf78 100644
--- a/src/dh-link.c
+++ b/src/dh-link.c
@@ -38,13 +38,18 @@
  */
 
 struct _DhLink {
-        guint ref_count;
-
         /* FIXME: it is possible to optimize this struct to use less memory, by
          * having only one pointer for the book, for example with an union and a
          * secondary struct for @base_path + @book_id.
          */
 
+        /* To avoid some memory padding inside the struct, to use less memory,
+         * the fields are placed in this order:
+         * 1. All the pointers.
+         * 2. Other types.
+         * 3. Bit fields.
+         */
+
         /* @base_path and @book_id are set only for links of @type
          * DH_LINK_TYPE_BOOK.
          */
@@ -59,6 +64,8 @@ struct _DhLink {
 
         gchar *relative_url;
 
+        guint ref_count;
+
         DhLinkType type : 8;
         DhLinkFlags flags : 8;
 };


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