{"data":{"id":"magento2-40217","source":"github-pr","sourceRef":"magento/magento2#40217","title":"Time complexity of _loadAttributes in Eav\\Model\\Entity\\Collection\\AbstractCollection increases with number of products in cart and attributes","pr":{"number":40217,"url":"https://github.com/magento/magento2/pull/40217","author":"senthilengg","mergedAt":"2025-11-20T15:18:19Z","mergeCommit":"0dc6ce587dc487f939300cf02c41b8b3133683c6","headCommit":"7a4f0b73ddc1ee2abc017aef426c4714e64b0724","baseRef":"2.4-develop","diffSha256":"cbc02d62af7715bed1f9007cb1752b2e1cfeeeda6c1114247d00411512c28266"},"issues":[{"number":40216,"url":"https://github.com/magento/magento2/issues/40216","title":"Time complexity of _loadAttributes in Eav\\Model\\Entity\\Collection\\AbstractCollection increases with number of products in cart and attributes","labels":["Area: Catalog","Component: Eav","Issue: Confirmed","Priority: P2","Progress: done","Reported on 2.4.x","Reproduced on 2.4.x","Triage: Dev.Experience"],"kind":"human"}],"fixedIn":"2.4.9","containingTags":["2.4.9"],"reportedOn":null,"codeMatch":{"2.4.6":"conflict","2.4.6-p1":"conflict","2.4.6-p2":"conflict","2.4.6-p3":"conflict","2.4.6-p4":"conflict","2.4.6-p5":"conflict","2.4.6-p6":"conflict","2.4.6-p7":"conflict","2.4.6-p8":"conflict","2.4.6-p9":"conflict","2.4.6-p10":"conflict","2.4.6-p11":"conflict","2.4.6-p12":"conflict","2.4.6-p13":"conflict","2.4.6-p14":"conflict","2.4.6-p15":"conflict","2.4.7":"conflict","2.4.7-p1":"conflict","2.4.7-p2":"conflict","2.4.7-p3":"conflict","2.4.7-p4":"conflict","2.4.7-p5":"conflict","2.4.7-p6":"conflict","2.4.7-p7":"conflict","2.4.7-p8":"conflict","2.4.7-p9":"conflict","2.4.7-p10":"conflict","2.4.8":"conflict","2.4.8-p1":"conflict","2.4.8-p2":"conflict","2.4.8-p3":"conflict","2.4.8-p4":"conflict","2.4.8-p5":"conflict","2.4.9":"conflict"},"affectedVersions":[],"components":["magento/module-eav"],"files":[{"path":"app/code/Magento/Eav/Model/Entity/Collection/AbstractCollection.php","change":"modified","package":"magento/module-eav"}],"stripped":{"tests":[],"docs":[],"outsideCode":[]},"linesChanged":53,"mergeBatched":false,"excluded":null,"sections":{"description":"Iterates per attribute per product which increases the time complexity to multifold","stepsToReproduce":"1. Review https://github.com/magento/magento2/blob/2.4-develop/app/code/Magento/Catalog/Model/AbstractModel.php#L180 and there is a nested for in https://github.com/magento/magento2/blob/2.4-develop/app/code/Magento/Eav/Model/Entity/Collection/AbstractCollection.php#L1236 and it calls `_setItemAttributeValue` once per attribute and `_setItemAttributeValue` iterates all products and set the value which is **O(n*m) [50 products in cart & 50 attributes leads to 2500 iterations]**\n\n```\nn = number of attributes\nm= numbers of products\n```\nProfiler Output\n\n<img width=\"1590\" height=\"608\" alt=\"Image\" src=\"https://github.com/user-attachments/assets/04e7110c-4994-46bf-9651-4d8cd62e8151\" />\n\n\n2. In addition `setData` function in https://github.com/magento/magento2/blob/2.4-develop/app/code/Magento/Catalog/Model/AbstractModel.php#L180 does the same validation for all products instead it should be done once per attribute per collection given it doesn't change per product or per entityValue","expectedResult":"Iterates the number of rows from select query and then set once per product","actualResult":"Iterates per attribute per product which increases the time complexity to multifold","source":"issue"},"signatures":["throw new LocalizedException("],"labels":{"area":["Catalog"],"component":["Eav"],"priority":"P2","severity":null,"reportedOn":["2.4.x"]},"categories":["Catalog/Product"],"triage":{"model":"@cf/cloudflare/clef","requestHash":"be781916a334e6e6d825ec18f563993717f032fb206b21f9b85f526bf506fe08","isBugfix":0.6915,"changeKind":{"choice":"refactor","probabilities":{"bugfix":0.4549,"feature":0.0144,"refactor":0.512,"tests_only":0.0067,"docs_only":0.0055,"dependency":0.0065},"confidence":0.3632},"scope":{"score":0.8405,"probabilities":{"0":0.3851,"1":0.3893,"2":0.2256},"confidence":0.0261},"risk":{"score":1.0542,"probabilities":{"0":0.2258,"1":0.4942,"2":0.28},"confidence":0.0605},"area":{"choice":"catalog","probabilities":{"catalog":0.7704,"checkout":0.032,"customer":0.0068,"admin":0.0363,"graphql_api":0.0061,"framework":0.0964,"frontend":0.0115,"other":0.0405},"confidence":0.5509},"securityRelevant":0.0061,"reportedVersion":{"choice":"unspecified","probabilities":{"2.4.0":0.0083,"2.4.0-p1":0.0051,"2.4.1":0.0043,"2.4.1-p1":0.0036,"2.4.2":0.0047,"2.4.2-p1":0.0047,"2.4.2-p2":0.0061,"2.4.3":0.0099,"2.4.3-p1":0.0088,"2.4.3-p2":0.0083,"2.4.3-p3":0.0061,"2.4.4":0.0098,"2.4.4-p1":0.0069,"2.4.4-p10":0.0062,"2.4.4-p11":0.0071,"2.4.4-p12":0.0059,"2.4.4-p13":0.0061,"2.4.4-p14":0.007,"2.4.4-p15":0.0055,"2.4.4-p16":0.0066,"2.4.4-p17":0.0055,"2.4.4-p18":0.0045,"2.4.4-p2":0.0031,"2.4.4-p3":0.0035,"2.4.4-p4":0.004,"2.4.4-p5":0.0044,"2.4.4-p6":0.0042,"2.4.4-p7":0.0047,"2.4.4-p8":0.0044,"2.4.4-p9":0.0038,"2.4.5":0.0096,"2.4.5-p1":0.007,"2.4.5-p10":0.0057,"2.4.5-p11":0.0077,"2.4.5-p12":0.0075,"2.4.5-p13":0.0064,"2.4.5-p14":0.0073,"2.4.5-p15":0.0053,"2.4.5-p16":0.006,"2.4.5-p17":0.006,"2.4.5-p2":0.0032,"2.4.5-p3":0.0046,"2.4.5-p4":0.0047,"2.4.5-p5":0.0052,"2.4.5-p6":0.0061,"2.4.5-p7":0.0052,"2.4.5-p8":0.0051,"2.4.5-p9":0.0041,"2.4.6":0.0184,"2.4.6-p1":0.0139,"2.4.6-p10":0.0092,"2.4.6-p11":0.0125,"2.4.6-p12":0.0108,"2.4.6-p13":0.009,"2.4.6-p14":0.0094,"2.4.6-p15":0.0074,"2.4.6-p2":0.0044,"2.4.6-p3":0.0064,"2.4.6-p4":0.0085,"2.4.6-p5":0.007,"2.4.6-p6":0.0089,"2.4.6-p7":0.0074,"2.4.6-p8":0.0069,"2.4.6-p9":0.0049,"2.4.7":0.0137,"2.4.7-p1":0.011,"2.4.7-p10":0.01,"2.4.7-p2":0.0067,"2.4.7-p3":0.009,"2.4.7-p4":0.0118,"2.4.7-p5":0.0089,"2.4.7-p6":0.0136,"2.4.7-p7":0.0096,"2.4.7-p8":0.0079,"2.4.7-p9":0.0047,"2.4.8":0.0236,"2.4.8-p1":0.0146,"2.4.8-p2":0.021,"2.4.8-p3":0.0158,"2.4.8-p4":0.0161,"2.4.8-p5":0.0128,"2.4.9":0.0206,"unspecified":0.3438},"confidence":0.1142},"backportWorthy":{"score":1.1013,"probabilities":{"0":0.2395,"1":0.4196,"2":0.3409},"confidence":0.0244}},"curated":{"title":"Batches EAV collection attribute value setting to cut loop iterations","description":"In Eav AbstractCollection::_loadAttributes, it calls _setItemAttributeValue once per attribute and _setItemAttributeValue iterates all products, which is O(n*m): 50 products in cart and 50 attributes lead to 2500 iterations. Now the select query result becomes [product_id => [attribute_code => value]], and the new _setItemAttributeValues sets the values once per product with array union (+).","categories":["Catalog/Product"],"author":"claude-code/opus-5.5","date":"2026-10-06","reviewedBy":null},"tests":null},"_documentation":"https://magento.watch/api","_description":"Upstream fix magento2-40217 details"}