From f8ffa757c81de439352dca4cc4a99779a2c19970 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9ry=20Debongnie?= Date: Thu, 1 Jul 2021 10:15:56 +0000 Subject: [PATCH] [IMP] web: optimize read/unlink when id list is empty MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit With this commit, we can simply skip performing a rpc when we know that the id list given to read/unlink is empty. closes odoo/odoo#73063 Signed-off-by: Géry Debongnie (ged) --- addons/web/static/src/core/orm_service.js | 14 +++++++-- .../static/tests/core/orm_service_tests.js | 30 +++++++++++++++++-- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/addons/web/static/src/core/orm_service.js b/addons/web/static/src/core/orm_service.js index 710dd67f87b..b3c2f053fbc 100644 --- a/addons/web/static/src/core/orm_service.js +++ b/addons/web/static/src/core/orm_service.js @@ -119,7 +119,12 @@ class ORM { read(model, ids, fields, ctx) { validatePrimitiveList("ids", "number", ids); - validatePrimitiveList("fields", "string", fields); + if (fields) { + validatePrimitiveList("fields", "string", fields); + } + if (!ids.length) { + return Promise.resolve([]); + } return this.call(model, "read", [ids, fields], { context: ctx }); } @@ -148,7 +153,9 @@ class ORM { searchRead(model, domain, fields, options = {}, ctx = {}) { validateArray("domain", domain); - validatePrimitiveList("fields", "string", fields); + if (fields) { + validatePrimitiveList("fields", "string", fields); + } const kwargs = { context: ctx, domain, fields }; assignOptions(kwargs, options, ["offset", "limit", "order"]); return this.call(model, "search_read", [], kwargs); @@ -156,6 +163,9 @@ class ORM { unlink(model, ids, ctx) { validatePrimitiveList("ids", "number", ids); + if (!ids.length) { + return true; + } return this.call(model, "unlink", [ids], { context: ctx }); } diff --git a/addons/web/static/tests/core/orm_service_tests.js b/addons/web/static/tests/core/orm_service_tests.js index a7766416548..0557d6e832b 100644 --- a/addons/web/static/tests/core/orm_service_tests.js +++ b/addons/web/static/tests/core/orm_service_tests.js @@ -261,9 +261,9 @@ QUnit.test("silent mode", async (assert) => { orm.call("my_model", "my_method"); orm.silent.call("my_model", "my_method"); orm.call("my_model", "my_method"); - orm.read("my_model", [], []); - orm.silent.read("my_model", [], []); - orm.read("my_model", [], []); + orm.read("my_model", [1], []); + orm.silent.read("my_model", [1], []); + orm.read("my_model", [1], []); assert.verifySteps([ "/web/dataset/call_kw/my_model/my_method", @@ -291,3 +291,27 @@ QUnit.test("validate some obviously wrong calls", async (assert) => { assert.strictEqual(error.message, "Invalid ids list: false"); } }); + +QUnit.test("optimize read and unlink if no ids", async (assert) => { + serviceRegistry.add("rpc", { + start() { + return async (route) => { + assert.step(route); + }; + }, + }); + const env = await makeTestEnv(); + const orm = env.services.orm; + + await orm.read("my_model", [1], []); + assert.verifySteps(["/web/dataset/call_kw/my_model/read"]); + + await orm.read("my_model", [], []); + assert.verifySteps([]); + + await orm.unlink("my_model", [1], {}); + assert.verifySteps(["/web/dataset/call_kw/my_model/unlink"]); + + await orm.unlink("my_model", [], {}); + assert.verifySteps([]); +});