Unverified Commit 5f9de462 authored by Alex Pott's avatar Alex Pott
Browse files

perf: #3574230 Use a weak reference for EntityBase::typedData ...

perf: #3574230 Use a weak reference for EntityBase::typedData                                                                                                                       Thu 12 Mar 10:58:37 2026

                        By: catch
                        By: andypost
                        By: alexpott

(cherry picked from commit 410370cb)
parent 3f857ba7
Loading
Loading
Loading
Loading
Loading
+13 −6
Original line number Diff line number Diff line
@@ -41,9 +41,9 @@ abstract class EntityBase implements EntityInterface {
  protected $enforceIsNew;

  /**
   * A typed data object wrapping this entity.
   * A weak reference to the typed data object wrapping this entity.
   *
   * @var \Drupal\Core\TypedData\ComplexDataInterface
   * @var \WeakReference
   */
  protected $typedData;

@@ -626,12 +626,19 @@ public function toArray() {
  /**
   * {@inheritdoc}
   */
  #[\NoDiscard]
  public function getTypedData() {
    if (!isset($this->typedData)) {
      $class = $this->getTypedDataClass();
      $this->typedData = $class::createFromEntity($this);
    if ($this->typedData instanceof \WeakReference) {
      $return = $this->typedData->get();
      if ($return !== NULL) {
        return $return;
      }
    }
    return $this->typedData;

    $class = $this->getTypedDataClass();
    $return = $class::createFromEntity($this);
    $this->typedData = \WeakReference::create($return);
    return $return;
  }

  /**