Skip to content

Regarding the queued effects linked list implementation in the default implementation.Β #93

@medz

Description

@medz

ref: #78

This is just a note for implementations in other languages.

interface EffectNode extends ReactiveNode {
+  nextEffect?: EffectNode;
   fn(): void;
}

-let notifyIndex = 0;
-let queuedLength = 0;

-const queued: (EffectNode | undefined)[] = [];
+let queuedEffects: EffectNode | undefined;
+let queuedEffectsTail?: EffectNode | undefined;

notify impl:

notify(effect: EffectNode) {
  let head: EffectNode | undefined = void 0;
  const tail = effect;
  do {
    effect.flags &= ~ReactiveFlags.Watching;
    effect.nextEffect = head;
    head = effect;
    effect = effect.subs?.sub as EffectNode;
    if (effect === undefined || !(effect.flags & ReactiveFlags.Watching)) {
      break;
    }
  } while(true);

  if (!queuedEffectsTail) {
    queuedEffects = queuedEffectsTail = head
  } else {
    queuedEffectsTail!.nextEffect = head;
    queuedEffectsTail = tail;
  }
}

flush impl:

function flush(): void {
  while (queuedEffects) {
    final effect = queuedEffects;
    if ((queuedEffects = effect.nextEffect) !== void 0) {
      effect.nextEffect = null;
    } else {
      queuedEffectsTail = null;
    }
    run(effect);
  }
}

A linked list implementation doesn't necessarily offer a performance improvement (at least not in JavaScript, and may even result in a performance decrease).

However, in Dart, a linked list implementation is approximately 20% faster than a List implementation (AOT) and about 60% faster than a Map implementation.

When using this implementation, please conduct benchmark tests first to ensure that it does not result in a performance decrease.

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