[IMP] web: optimize read/unlink when id list is empty

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) <ged@openerp.com>
This commit is contained in:
Géry Debongnie
2021-07-05 14:17:58 +00:00
parent 3e8ee3aedc
commit f8ffa757c8
2 changed files with 39 additions and 5 deletions
+12 -2
View File
@@ -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 });
}
@@ -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([]);
});