public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH manager 2/4] ui: fix O(n^2) calculations when loading /cluster/resources
Date: Fri,  5 Sep 2025 13:52:00 +0200	[thread overview]
Message-ID: <20250905120627.2585826-3-d.csapak@proxmox.com> (raw)
In-Reply-To: <20250905120627.2585826-1-d.csapak@proxmox.com>

when we fetch the cluster resources, the ui calculates some fields for
each resource, such as 'hostmem_usage'. This requires the maxmem
attribute from the host which we have to look up in the resource store.
Since the data is not sorted in the store itself, extjs linearly goes
through the records to make the lookup when we use 'findExact'.

So instead of using findExact for every guest (which iterates the whole
resource store again), use a 'nodeCache' there and clear it out before
we load the new data.

This reduces the total time used for 'cacluate_hostmem_usage'
in my test setup (~10000 non-running vms) from 4,408.2 ms to 12.4 ms.
(Measured with the `Performance` tab in Chromium)

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 www/manager6/Utils.js              | 12 ++++--------
 www/manager6/data/ResourceStore.js | 20 ++++++++++++++++++++
 2 files changed, 24 insertions(+), 8 deletions(-)

diff --git a/www/manager6/Utils.js b/www/manager6/Utils.js
index 57a818f6..c48ee0b2 100644
--- a/www/manager6/Utils.js
+++ b/www/manager6/Utils.js
@@ -1070,8 +1070,7 @@ Ext.define('PVE.Utils', {
                 return -1;
             }
 
-            var index = PVE.data.ResourceStore.findExact('id', 'node/' + data.node);
-            var node = PVE.data.ResourceStore.getAt(index);
+            let node = PVE.data.ResourceStore.getNodeById(data.node);
             if (!Ext.isDefined(node) || node === null) {
                 return -1;
             }
@@ -1093,8 +1092,7 @@ Ext.define('PVE.Utils', {
                 return '';
             }
 
-            var index = PVE.data.ResourceStore.findExact('id', 'node/' + record.data.node);
-            var node = PVE.data.ResourceStore.getAt(index);
+            let node = PVE.data.ResourceStore.getNodeById(record.data.node);
             if (!Ext.isDefined(node) || node === null) {
                 return '';
             }
@@ -1148,8 +1146,7 @@ Ext.define('PVE.Utils', {
                 return -1;
             }
 
-            var index = PVE.data.ResourceStore.findExact('id', 'node/' + data.node);
-            var node = PVE.data.ResourceStore.getAt(index);
+            let node = PVE.data.ResourceStore.getNodeById(data.node);
 
             if (!Ext.isDefined(node) || node === null) {
                 return -1;
@@ -1199,8 +1196,7 @@ Ext.define('PVE.Utils', {
                 return '';
             }
 
-            var index = PVE.data.ResourceStore.findExact('id', 'node/' + record.data.node);
-            var node = PVE.data.ResourceStore.getAt(index);
+            let node = PVE.data.ResourceStore.getNodeById(record.data.node);
             var maxmem = node.data.maxmem || 0;
 
             if (record.data.mem > 1) {
diff --git a/www/manager6/data/ResourceStore.js b/www/manager6/data/ResourceStore.js
index d1f3fb63..bf740129 100644
--- a/www/manager6/data/ResourceStore.js
+++ b/www/manager6/data/ResourceStore.js
@@ -2,6 +2,8 @@ Ext.define('PVE.data.ResourceStore', {
     extend: 'Proxmox.data.UpdateStore',
     singleton: true,
 
+    nodeCache: {},
+
     findVMID: function (vmid) {
         let me = this;
         return me.findExact('vmid', parseInt(vmid, 10)) >= 0;
@@ -21,6 +23,22 @@ Ext.define('PVE.data.ResourceStore', {
         return nodes;
     },
 
+    getNodeById: function (id) {
+        let me = this;
+
+        if (!me.nodeCache[id]) {
+            let idx = me.findExact('id', `node/${id}`);
+            me.nodeCache[id] = me.getAt(idx);
+        }
+
+        return me.nodeCache[id];
+    },
+
+    clearCache: function () {
+        let me = this;
+        me.nodeCache = {};
+    },
+
     storageIsShared: function (storage_path) {
         let me = this;
 
@@ -372,5 +390,7 @@ Ext.define('PVE.data.ResourceStore', {
         });
 
         me.callParent([config]);
+
+        me.on('beforeload', me.clearCache, me);
     },
 });
-- 
2.47.2



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


  parent reply	other threads:[~2025-09-05 12:06 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-05 11:51 [pve-devel] [PATCH manager 0/4] gui/api performance improvements Dominik Csapak
2025-09-05 11:51 ` [pve-devel] [PATCH manager 1/4] API2Tools: rrd: remove O(n^2) lookup for keys Dominik Csapak
2025-09-05 13:49   ` Aaron Lauterer
2025-09-05 13:53     ` Dominik Csapak
2025-09-05 13:58       ` Aaron Lauterer
2025-09-05 13:57   ` Daniel Kral
2025-09-05 11:52 ` Dominik Csapak [this message]
2025-09-05 11:52 ` [pve-devel] [PATCH manager 3/4] ui: resource tree: improve performance on initial update Dominik Csapak
2025-09-05 11:52 ` [pve-devel] [PATCH manager 4/4] ui: resource tree: fix change detection Dominik Csapak
2025-09-05 17:40 ` [pve-devel] applied: [PATCH manager 0/4] gui/api performance improvements Thomas Lamprecht

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250905120627.2585826-3-d.csapak@proxmox.com \
    --to=d.csapak@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal