Skip to content

Taxjar extension performance issues #378

@snoop0x7b

Description

@snoop0x7b

Prerequisites

Description

This extension causes performance issues in checkout and in cart editing operations because it uses a query-in-a-loop anti-pattern. Secondly, to make matters worse, it also uses the product repository for its fetch operations, fetching unnecessary product attributes, which makes these queries slower.

Model/Tax/Sales/Total/Quote/Tax.php:352-366

        try {
            $product = $this->productRepository->getById($item->getProductId(), false, $item->getStoreId());
            
            // Configurable products should use the PTC of the child (when available)
            if ($product->getTypeId() == 'configurable') {
                $children = $item->getChildren();
        
                if (is_array($children) && isset($children[0])) {
                    $product = $this->productRepository->getById(
                        $children[0]->getProductId(),
                        false,
                        $item->getStoreId()
                    );
                }
            }
                
            $extensionAttributes->setTjPtc($product->getTjPtc());
        } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
            $msg = $e->getMessage() . "\nline item" . $itemDataObject->getCode() . ' for ' . $item->getRowTotal();
            $this->logger->log($msg); 
        } 

This method is called once per product in the cart.

This should be refactored to use the product collection, and be built into the caller, so that this isn't called once per product.

Steps to Reproduce

  1. Build a cart of a 5+ items on a store with a large number of attributes (50+)
  2. Connect a PHP profiling or debugger
  3. Remove items from the cart - Item removal will be slow.

Expected Result

Cart editing should run in O(1) time because we shouldn't be executing queries for every item in the cart, or fetching every single attribute.

Actual Result

Editing the cart is slow. Progressing through checkout steps is slow.

Versions

  • Magento 2.4
  • Magento 2.3
  • Magento Open Source (fka Magento 2 Community Edition)
  • Adobe Commerce (fka Magento 2 Enterprise Edition)
  • PHP 8.2
  • PHP 8.1
  • PHP 7.4
  • PHP 7.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions