From: "Yikai Zhao" <yikai@z1k.dev>
To: emacs-devel@gnu.org
Subject: [PATCH] memory-report: support calculating size for structures
Date: Sun, 29 Aug 2021 22:40:48 +0800 [thread overview]
Message-ID: <ef1a8d35-e43f-4495-a458-d48460fb3e83@www.fastmail.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 452 bytes --]
Hi,
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.
Regarding the copyright assignment, I'm currently in the progress of signing it. But maybe this patch is small enough to apply without it?
Yikai
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-memory-report-support-calculating-size-for-structure.patch --]
[-- Type: text/x-patch; name="0001-memory-report-support-calculating-size-for-structure.patch", Size: 2236 bytes --]
From 6a851e089d0bd9ef61d9209c5d3eb3932c7a7a6c Mon Sep 17 00:00:00 2001
From: Yikai Zhao <yikai@z1k.dev>
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
next reply other threads:[~2021-08-29 14:40 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-08-29 14:40 Yikai Zhao [this message]
2021-08-29 19:11 ` [PATCH] memory-report: support calculating size for structures Lars Ingebrigtsen
2021-08-31 16:15 ` Yikai Zhao
2021-08-31 16:27 ` Eli Zaretskii
2021-09-01 15:23 ` Yikai Zhao
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ef1a8d35-e43f-4495-a458-d48460fb3e83@www.fastmail.com \
--to=yikai@z1k.dev \
--cc=emacs-devel@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).