Skip to content

Commit 41f0ce9

Browse files
antoine162KangOl
authored andcommitted
[IMP] util/records.py: optimize update_record_from_xml's recursive mode
update_record_from_xml has an optional argument (ensure_references) that will recursively update all refs referred to by the record. However, at the moment, its implementation doesn't keep good track of which refs already have been updated, leading to far more updates than necessary. For example, if we have the following tree of references: ``` Record A - Record B - Record C - Record B - Record D - Record E - Record F - Record B - Record C - Record D - Record E ``` (which is quite common since lots of records end up referring to some objects such as `base.main_company`, `base.group_user`, `base.user_root`, `base.group_system`, etc.) we will end up calling update_record_from_xml 11 times whereas we only need to call it 6 times (one for each of Records A-F). That's a lot of extra SQL queries. Part of odoo/upgrade#5236 Signed-off-by: Christophe Simonis (chs) <[email protected]>
1 parent 3ea28ad commit 41f0ce9

File tree

1 file changed

+32
-4
lines changed

1 file changed

+32
-4
lines changed

src/util/records.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,28 @@ def update_record_from_xml(
714714
from_module=None,
715715
reset_translations=(),
716716
ensure_references=False,
717+
):
718+
__update_record_from_xml(
719+
cr,
720+
xmlid,
721+
reset_write_metadata=reset_write_metadata,
722+
force_create=force_create,
723+
from_module=from_module,
724+
reset_translations=reset_translations,
725+
ensure_references=ensure_references,
726+
done_refs=set(),
727+
)
728+
729+
730+
def __update_record_from_xml(
731+
cr,
732+
xmlid,
733+
reset_write_metadata,
734+
force_create,
735+
from_module,
736+
reset_translations,
737+
ensure_references,
738+
done_refs,
717739
):
718740
from .modules import get_manifest
719741

@@ -798,14 +820,20 @@ def add_ref(ref):
798820
for ref_match in re.finditer(r"\bref\((['\"])(.*?)\1\)", eval_node.get("eval")):
799821
add_ref(ref_match.group(2))
800822

801-
done_refs = set()
823+
done_refs.add(xmlid)
802824
for ref in extra_references:
803825
if ref in done_refs:
804826
continue
805-
done_refs.add(ref)
806827
_logger.info("Update of %s - ensuring the reference %s exists", xmlid, ref)
807-
update_record_from_xml(
808-
cr, ref, reset_write_metadata=reset_write_metadata, force_create=True, ensure_references=True
828+
__update_record_from_xml(
829+
cr,
830+
ref,
831+
reset_write_metadata=reset_write_metadata,
832+
force_create=True,
833+
from_module=from_module,
834+
reset_translations=reset_translations,
835+
ensure_references=True,
836+
done_refs=done_refs,
809837
)
810838

811839
cr_or_env = env(cr) if version_gte("saas~16.2") else cr

0 commit comments

Comments
 (0)