Subject: bug#50301: [PATCH] memory-report: support calculating size for structures

From: [email-masked]
Date: Wed, 01 Sep 2021 00:11:18 +0800

Tags: patch

Tags: patch

(already sent to emacs-devel mailing list, re-submit using submit-emacs-patch)

Here's another patch on memory-report package which fixes the object
size calculation for structures (cl-defstruct types). Before this patch,
memory-report--object-size will report empty size for structure objects,
which results in incorrect report about "Largest Variables" in
memory-report result.

In GNU Emacs 28.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.30, cairo version 1.17.4)
 of 2021-08-20 built on lilydjwg
Repository revision: 657fe8b9d87dd17d9b50dd8e15bfd917c6c630b2
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12013000
System Description: Arch Linux

Configured using:
 'configure --prefix=/usr --sysconfdir=/etc --libexecdir=/usr/lib
 --localstatedir=/var --mandir=/usr/share/man --with-gameuser=:games
 --with-sound=alsa --with-modules --without-gconf --without-gsettings
 --enable-link-time-optimization --with-native-compilation
 --with-x-toolkit=gtk3 --without-xaw3d --without-compress-install
 '--program-transform-name=s/\([ec]tags\)/\1.emacs/'
 'CFLAGS=-march=x86-64 -mtune=generic -O2 -pipe -fno-plt -flto
 -fuse-linker-plugin' CPPFLAGS=-D_FORTIFY_SOURCE=2
 LDFLAGS=-Wl,-O1,--sort-common,--as-needed,-z,relro,-z,now'

>From 6a851e089d0bd9ef61d9209c5d3eb3932c7a7a6c Mon Sep 17 00:00:00 2001
From: Yikai Zhao <[email-masked]>
Date: Sun, 29 Aug 2021 21:47:12 +0800
Subject: [PATCH] memory-report: support calculating size for structures

---
 lisp/emacs-lisp/memory-report.el            | 16 +++++++++++++++-
 test/lisp/emacs-lisp/memory-report-tests.el |  8 ++++++++
 2 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/lisp/emacs-lisp/memory-report.el b/lisp/emacs-lisp/memory-report.el
index aee2a0079c..cfa7010af4 100644
--- a/lisp/emacs-lisp/memory-report.el
+++ b/lisp/emacs-lisp/memory-report.el
@@ -29,7 +29,8 @@
 
 (require 'seq)
 (require 'subr-x)
-(eval-when-compile (require 'cl-lib))
+(require 'cl-lib)
+(require 'cl-macs)
 
 (defvar memory-report--type-size (make-hash-table))
 
@@ -243,6 +244,19 @@ memory-report--object-size-1
      value)
     total))
 
+;; for all cl-defstruct types
+(cl-defmethod memory-report--object-size-1 (counted (value cl-structure-object))
+  (let ((struct-type (type-of value)))
+    (apply '+
+           (memory-report--size 'vector)
+           (mapcar (lambda (slot)
+                     (if (eq (car slot) 'cl-tag-slot)
+                         0
+                       (memory-report--object-size
+                        counted
+                        (cl-struct-slot-value struct-type (car slot) value))))
+                   (cl-struct-slot-info struct-type)))))
+
 (defun memory-report--format (bytes)
   (setq bytes (/ bytes 1024.0))
   (let ((units '("KiB" "MiB" "GiB" "TiB")))
diff --git a/test/lisp/emacs-lisp/memory-report-tests.el b/test/lisp/emacs-lisp/memory-report-tests.el
index 0c0297b5fc..e352dd165f 100644
--- a/test/lisp/emacs-lisp/memory-report-tests.el
+++ b/test/lisp/emacs-lisp/memory-report-tests.el
@@ -68,6 +68,14 @@ memory-report-sizes-vectors
                 (vector string string))
                124))))
 
+(ert-deftest memory-report-sizes-structs ()
+  (cl-defstruct memory-report-test-struct
+    (item0 nil)
+    (item1 nil))
+  (let ((s (make-memory-report-test-struct :item0 "hello" :item1 "world")))
+    (should (= (memory-report-object-size s)
+               90))))
+
 (provide 'memory-report-tests)
 
 ;;; memory-report-tests.el ends here
-- 
2.33.0