Split out HEK size calculation into hek_size().
Nicholas Clark [Thu, 12 May 2011 09:25:19 +0000 (10:25 +0100)]
Add the shared HE overhead to the total size.

CHANGES
Size.xs

diff --git a/CHANGES b/CHANGES
index 4fa757f..b3668a2 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -1,5 +1,8 @@
 Revision history for Perl extension Devel::Size.
 
+0.76_50 2011-05-12 nicholas
+ * Split out HEK size calculation into hek_size(). Add the shared HE overhead.
+
 0.76 2011-05-11 nicholas
  * Just fix the version number in the line below.
 
diff --git a/Size.xs b/Size.xs
index 9dd4f0d..5b8aba8 100644 (file)
--- a/Size.xs
+++ b/Size.xs
@@ -525,6 +525,29 @@ op_size(pTHX_ const OP * const baseop, struct state *st)
   }
 }
 
+static void
+hek_size(pTHX_ struct state *st, HEK *hek, U32 shared)
+{
+    /* Hash keys can be shared. Have we seen this before? */
+    if (!check_new(st, hek))
+       return;
+    st->total_size += HEK_BASESIZE + hek->hek_len
+#if PERL_VERSION < 8
+       + 1 /* No hash key flags prior to 5.8.0  */
+#else
+       + 2
+#endif
+       ;
+    if (shared) {
+#if PERL_VERSION < 10
+       st->total_size += sizeof(struct he);
+#else
+       st->total_size += STRUCT_OFFSET(struct shared_he, shared_he_hek);
+#endif
+    }
+}
+
+
 #if PERL_VERSION < 8 || PERL_SUBVERSION < 9
 #  define SVt_LAST 16
 #endif
@@ -700,12 +723,7 @@ sv_size(pTHX_ struct state *const st, const SV * const orig_thing,
         cur_entry = *(HvARRAY(thing) + cur_bucket);
         while (cur_entry) {
           st->total_size += sizeof(HE);
-          if (cur_entry->hent_hek) {
-            /* Hash keys can be shared. Have we seen this before? */
-            if (check_new(st, cur_entry->hent_hek)) {
-              st->total_size += HEK_BASESIZE + cur_entry->hent_hek->hek_len + 2;
-            }
-          }
+         hek_size(aTHX_ st, cur_entry->hent_hek, HvSHAREKEYS(thing));
          if (recurse >= TOTAL_SIZE_RECURSION)
              sv_size(aTHX_ st, HeVAL(cur_entry), recurse);
           cur_entry = cur_entry->hent_next;