unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#50301: [PATCH] memory-report: support calculating size for structures
@ 2021-08-31 16:11 yikai
  2021-09-02  7:37 ` Lars Ingebrigtsen
  0 siblings, 1 reply; 9+ messages in thread
From: yikai @ 2021-08-31 16:11 UTC (permalink / raw)
  To: 50301

[-- Attachment #1: Type: text/plain, Size: 1309 bytes --]

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'


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-memory-report-support-calculating-size-for-structure.patch --]
[-- Type: text/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


^ permalink raw reply related	[flat|nested] 9+ messages in thread

* bug#50301: [PATCH] memory-report: support calculating size for structures
  2021-08-31 16:11 bug#50301: [PATCH] memory-report: support calculating size for structures yikai
@ 2021-09-02  7:37 ` Lars Ingebrigtsen
  2021-09-02 23:49   ` Glenn Morris
  0 siblings, 1 reply; 9+ messages in thread
From: Lars Ingebrigtsen @ 2021-09-02  7:37 UTC (permalink / raw)
  To: yikai; +Cc: 50301

yikai@z1k.dev writes:

> 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.

Thanks; applied to Emacs 28.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





^ permalink raw reply	[flat|nested] 9+ messages in thread

* bug#50301: [PATCH] memory-report: support calculating size for structures
  2021-09-02  7:37 ` Lars Ingebrigtsen
@ 2021-09-02 23:49   ` Glenn Morris
  2021-09-03  5:47     ` Lars Ingebrigtsen
  2021-09-04 16:38     ` Glenn Morris
  0 siblings, 2 replies; 9+ messages in thread
From: Glenn Morris @ 2021-09-02 23:49 UTC (permalink / raw)
  To: Lars Ingebrigtsen; +Cc: 50301, yikai


make lisp/emacs-lisp/memory-report-tests TEST_LOAD_EL=no

fails with: void-function cl-struct-slot-info





^ permalink raw reply	[flat|nested] 9+ messages in thread

* bug#50301: [PATCH] memory-report: support calculating size for structures
  2021-09-02 23:49   ` Glenn Morris
@ 2021-09-03  5:47     ` Lars Ingebrigtsen
  2021-09-04 16:38     ` Glenn Morris
  1 sibling, 0 replies; 9+ messages in thread
From: Lars Ingebrigtsen @ 2021-09-03  5:47 UTC (permalink / raw)
  To: Glenn Morris; +Cc: 50301, yikai

Glenn Morris <rgm@gnu.org> writes:

> make lisp/emacs-lisp/memory-report-tests TEST_LOAD_EL=no
>
> fails with: void-function cl-struct-slot-info

Yup; now fixed.  (I didn't notice, because it didn't fail with just
"cd test; make".)

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





^ permalink raw reply	[flat|nested] 9+ messages in thread

* bug#50301: [PATCH] memory-report: support calculating size for structures
  2021-09-02 23:49   ` Glenn Morris
  2021-09-03  5:47     ` Lars Ingebrigtsen
@ 2021-09-04 16:38     ` Glenn Morris
  2021-09-05  8:06       ` Michael Albinus
  1 sibling, 1 reply; 9+ messages in thread
From: Glenn Morris @ 2021-09-04 16:38 UTC (permalink / raw)
  To: Lars Ingebrigtsen; +Cc: 50301, yikai


> make lisp/emacs-lisp/memory-report-tests TEST_LOAD_EL=no
>
> fails with: void-function cl-struct-slot-info

And so does
emacs -Q -f memory-report

As cl-struct-slot-info is a non-autoloaded function in cl-macs
(an internal cl-lib component), I don't see how it is supposed to be
used by external packages. Yet it is documented in the cl-lib manual,
and used in eg tramp...





^ permalink raw reply	[flat|nested] 9+ messages in thread

* bug#50301: [PATCH] memory-report: support calculating size for structures
  2021-09-04 16:38     ` Glenn Morris
@ 2021-09-05  8:06       ` Michael Albinus
  2021-09-05  8:41         ` Lars Ingebrigtsen
  0 siblings, 1 reply; 9+ messages in thread
From: Michael Albinus @ 2021-09-05  8:06 UTC (permalink / raw)
  To: Glenn Morris; +Cc: 50301, Lars Ingebrigtsen, yikai

Glenn Morris <rgm@gnu.org> writes:

Hi Glenn,

> As cl-struct-slot-info is a non-autoloaded function in cl-macs
> (an internal cl-lib component), I don't see how it is supposed to be
> used by external packages. Yet it is documented in the cl-lib manual,
> and used in eg tramp...

So we shall autoload it?

Best regards, Michael.





^ permalink raw reply	[flat|nested] 9+ messages in thread

* bug#50301: [PATCH] memory-report: support calculating size for structures
  2021-09-05  8:06       ` Michael Albinus
@ 2021-09-05  8:41         ` Lars Ingebrigtsen
  2021-09-05 14:43           ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 1 reply; 9+ messages in thread
From: Lars Ingebrigtsen @ 2021-09-05  8:41 UTC (permalink / raw)
  To: Michael Albinus; +Cc: 50301, Glenn Morris, yikai, Stefan Monnier

Michael Albinus <michael.albinus@gmx.de> writes:

>> As cl-struct-slot-info is a non-autoloaded function in cl-macs
>> (an internal cl-lib component), I don't see how it is supposed to be
>> used by external packages. Yet it is documented in the cl-lib manual,
>> and used in eg tramp...
>
> So we shall autoload it?

Well, that seems like the obvious fix here...  and there's plenty of
other autoloaded functions in cl-macs.  But I've added Stefan to the
CCs; perhaps he has comments here.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





^ permalink raw reply	[flat|nested] 9+ messages in thread

* bug#50301: [PATCH] memory-report: support calculating size for structures
  2021-09-05  8:41         ` Lars Ingebrigtsen
@ 2021-09-05 14:43           ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2021-09-05 14:52             ` Lars Ingebrigtsen
  0 siblings, 1 reply; 9+ messages in thread
From: Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2021-09-05 14:43 UTC (permalink / raw)
  To: Lars Ingebrigtsen; +Cc: 50301, Glenn Morris, yikai, Michael Albinus

Lars Ingebrigtsen [2021-09-05 10:41:56] wrote:
> Michael Albinus <michael.albinus@gmx.de> writes:
>>> As cl-struct-slot-info is a non-autoloaded function in cl-macs
>>> (an internal cl-lib component), I don't see how it is supposed to be
>>> used by external packages. Yet it is documented in the cl-lib manual,
>>> and used in eg tramp...
>>
>> So we shall autoload it?
>
> Well, that seems like the obvious fix here...  and there's plenty of
> other autoloaded functions in cl-macs.  But I've added Stefan to the
> CCs; perhaps he has comments here.

Autoload sounds like the correct fix.


        Stefan






^ permalink raw reply	[flat|nested] 9+ messages in thread

* bug#50301: [PATCH] memory-report: support calculating size for structures
  2021-09-05 14:43           ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2021-09-05 14:52             ` Lars Ingebrigtsen
  0 siblings, 0 replies; 9+ messages in thread
From: Lars Ingebrigtsen @ 2021-09-05 14:52 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: 50301, Glenn Morris, yikai, Michael Albinus

Stefan Monnier <monnier@iro.umontreal.ca> writes:

> Autoload sounds like the correct fix.

OK; now done (and removed that offending (require 'cl-macs).)

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no





^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2021-09-05 14:52 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-31 16:11 bug#50301: [PATCH] memory-report: support calculating size for structures yikai
2021-09-02  7:37 ` Lars Ingebrigtsen
2021-09-02 23:49   ` Glenn Morris
2021-09-03  5:47     ` Lars Ingebrigtsen
2021-09-04 16:38     ` Glenn Morris
2021-09-05  8:06       ` Michael Albinus
2021-09-05  8:41         ` Lars Ingebrigtsen
2021-09-05 14:43           ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2021-09-05 14:52             ` Lars Ingebrigtsen

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).