Skip to content

perf(sidebar): lazy-create per-tab DOM elements to reduce memory usage#3872

Merged
piroor merged 3 commits intopiroor:trunkfrom
tkng:lazy-dom-elements
Feb 28, 2026
Merged

perf(sidebar): lazy-create per-tab DOM elements to reduce memory usage#3872
piroor merged 3 commits intopiroor:trunkfrom
tkng:lazy-dom-elements

Conversation

@tkng
Copy link
Copy Markdown
Contributor

@tkng tkng commented Feb 28, 2026

TreeItemElement.connectedCallback() の初期HTMLテンプレートから、使用頻度の低い8つのDOM要素を除去し、必要になった時点で初めて生成するようにします。

正直、コードが複雑になってレビューもややこしく、その点については申し訳ないのですが、以下のように、割と効果が高そう(20タブ程度でPSSで25MB程度のメモリ消費量の削減)なので、お手漉きの際にレビューをお願いします。

memory_plot

条件は以下のような感じです

対象の要素:

  • extra-items-container (indent / above / below / behind / front) — TST APIを通じて外部アドオンが利用する場合のみ必要。lazyなgetterにより、初回アクセス時に動的に <span> 要素を生成します。
  • burster — バックグラウンドタブの読み込み完了時のみ必要。sidebar-items.jskTAB_STATE_BURSTING を付与する前に ensureBurster() を呼び出します。
  • contextual-identity-marker — Firefoxのコンテナータブでのみ必要。_updateTabProperties()contextual-identity-* 状態を検出した際に ensureContextualIdentityMarker() を呼び出します。
  • native-tab-group-line — ネイティブタブグループに属するタブでのみ必要。_updateTabProperties()nativeTabGroup または group が存在する場合に ensureNativeTabGroupLine() を呼び出します。

highlighter要素は、アクティブタブやホバー状態で即座に必要となるため、テンプレートに残しています。

tst-api-frontend.js では、ワイルドカード操作(id == '*')や collectExtraContentsRoots() がbacking field(_extraItemsContainer*Root)を参照するように変更し、不要なlazy初期化の発火を防止しています。また、clearExtraTabContentsInElement()では未初期化のコンテナに対するガードを追加しています。

外部アドオン・コンテナー・タブグループを使用しない100タブの環境で、初期生成から最大800個のDOMノードとそれに伴うShadow DOMルートを削減できます。

Remove 8 rarely-used elements per tab from the initial HTML template in
TreeItemElement.connectedCallback() and create them on demand instead:

- extra-items-container (indent/above/below/behind/front): only used by
  external addons via TST API; lazy getters now create host <span> elements
  dynamically when first accessed
- burster: only needed on background tab load completion; ensureBurster()
  is called in sidebar-items.js before adding kTAB_STATE_BURSTING
- contextual-identity-marker: only needed for Firefox Container tabs;
  ensureContextualIdentityMarker() is called in _updateTabProperties()
  when a contextual-identity-* state is detected
- native-tab-group-line: only needed for tabs in native tab groups;
  ensureNativeTabGroupLine() is called in _updateTabProperties()
  when nativeTabGroup or group is present

The highlighter element is intentionally kept in the template because it
is needed immediately for active tab and hover states.

In tst-api-frontend.js, wildcard operations (id == '*') and
collectExtraContentsRoots() now reference backing fields
(_extraItemsContainer*Root) to avoid triggering lazy initialization,
and clearExtraTabContentsInElement() guards against uninitialized
containers.

For 100 tabs without external addons, Containers, or tab groups,
this eliminates up to 800 DOM nodes and their associated Shadow DOM
roots from initial creation.
@tkng tkng marked this pull request as ready for review February 28, 2026 15:40
Comment thread webextensions/sidebar/components/TreeItemElement.js
Comment thread webextensions/sidebar/tst-api-frontend.js
Comment thread webextensions/sidebar/tst-api-frontend.js Outdated
Comment thread webextensions/sidebar/tst-api-frontend.js Outdated
Comment thread webextensions/sidebar/tst-api-frontend.js Outdated
Comment thread webextensions/sidebar/tst-api-frontend.js Outdated
Comment thread webextensions/sidebar/tst-api-frontend.js Outdated
@piroor piroor merged commit 08ec219 into piroor:trunk Feb 28, 2026
1 check passed
@piroor
Copy link
Copy Markdown
Owner

piroor commented Feb 28, 2026

ありがとうございます! 仮想スクロール化で要素数自体をだいぶ減らしたつもりではあるのですが、それでもまだまだ影響が大きかったんですね……

tkng added a commit to tkng/treestyletab that referenced this pull request Mar 2, 2026
piroor#3872)

* perf(sidebar): lazy-create per-tab DOM elements to reduce memory usage

Remove 8 rarely-used elements per tab from the initial HTML template in
TreeItemElement.connectedCallback() and create them on demand instead:

- extra-items-container (indent/above/below/behind/front): only used by
  external addons via TST API; lazy getters now create host <span> elements
  dynamically when first accessed
- burster: only needed on background tab load completion; ensureBurster()
  is called in sidebar-items.js before adding kTAB_STATE_BURSTING
- contextual-identity-marker: only needed for Firefox Container tabs;
  ensureContextualIdentityMarker() is called in _updateTabProperties()
  when a contextual-identity-* state is detected
- native-tab-group-line: only needed for tabs in native tab groups;
  ensureNativeTabGroupLine() is called in _updateTabProperties()
  when nativeTabGroup or group is present

The highlighter element is intentionally kept in the template because it
is needed immediately for active tab and hover states.

In tst-api-frontend.js, wildcard operations (id == '*') and
collectExtraContentsRoots() now reference backing fields
(_extraItemsContainer*Root) to avoid triggering lazy initialization,
and clearExtraTabContentsInElement() guards against uninitialized
containers.

For 100 tabs without external addons, Containers, or tab groups,
this eliminates up to 800 DOM nodes and their associated Shadow DOM
roots from initial creation.

* Optimize with mapAndFilter

* Add DOM structure design comment for future development

---------

Co-authored-by: YUKI "Piro" Hiroshi <piro.outsider.reflex@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants