From d8d5dc45656233fdccc3709093a16e2562cd4a40 Mon Sep 17 00:00:00 2001 From: Yikai Zhao Date: Thu, 2 Sep 2021 09:37:06 +0200 Subject: [PATCH] memory-report: support calculating size for structures * lisp/emacs-lisp/memory-report.el (memory-report--object-size-1): Add support for cl-defstruct types (bug#50301). --- lisp/emacs-lisp/memory-report.el | 15 ++++++++++++++- test/lisp/emacs-lisp/memory-report-tests.el | 8 ++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lisp/emacs-lisp/memory-report.el b/lisp/emacs-lisp/memory-report.el index aee2a0079ca..3166d33e029 100644 --- a/lisp/emacs-lisp/memory-report.el +++ b/lisp/emacs-lisp/memory-report.el @@ -29,7 +29,7 @@ (require 'seq) (require 'subr-x) -(eval-when-compile (require 'cl-lib)) +(require 'cl-lib) (defvar memory-report--type-size (make-hash-table)) @@ -243,6 +243,19 @@ by counted more than once." value) total)) +;; 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 0c0297b5fce..e352dd165f3 100644 --- a/test/lisp/emacs-lisp/memory-report-tests.el +++ b/test/lisp/emacs-lisp/memory-report-tests.el @@ -68,6 +68,14 @@ (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.39.2