Immer patches for Array are correct, yet inefficient. Suggesting a new algorithm for optimized patches
メンテナーはふだん 4 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 初心者へのやさしさ
- 25/100
- issue の種類
- 機能追加
- 明瞭さ
- おおむね明確
- 活発さ
- 停滞
- 技術スタック
- javascript, typescript
- 領域
- performance
調査の方向性
tests/patch.js の配列パッチテストから始め、特に参照されている行の "arrays - multiple prepend" ケースを確認します。現在の期待されるパッチと提案された結果を比較し、そのうえで提案された bounded matching algorithm を prepend、splice、その他の配列変更に対して評価します。小さな配列編集で不要なパッチを削減しながら、最適化された動作が正しさを維持していれば完了です。
索引モデルが issue の本文から書いたものです。
説明
I am thinking as offering it as a pull request - let me know what you think
🚀 Feature Proposal
Today when computing a patch on arrays, immer compares objects by index, which results in all array change in case of unshift or splice usage. An example is the following test taken from immer testing -
https://github.com/immerjs/immer/blob/main/__tests__/patch.js#L543-L559
describe("arrays - multiple prepend", () => {
runPatchTest(
{x: [1, 2, 3]},
d => {
d.x.unshift(4)
d.x.unshift(5)
// 4,5,1,2,3
},
[
{op: "replace", path: ["x", 0], value: 5},
{op: "replace", path: ["x", 1], value: 4},
{op: "replace", path: ["x", 2], value: 1},
{op: "add", path: ["x", 3], value: 2},
{op: "add", path: ["x", 4], value: 3}
]
)
})
the expected result should be instead
[
{op: "add", path: ["x", 0], value: 5},
{op: "add", path: ["x", 1], value: 4}
]
Motivation
I am working on a new framework that also serializes patches, and the above is a performance issue.
Consider the above case - for an array of object, lets say an array of 100 objects with 10 attributes each, the current algorithm will generate 1000 patches, while my suggestion is to produce only 1 per added / removed object
Can this be solved in user-land code?
no
Example
See the example above.
Usage: immer -> serialize -> deserialize -> patched object
suggested algorithm
(pseudo code)
the problem with json diff is that given two arrays, without any additional knowledge, to figure out if an item was
pushed to the front, we have to do a === comparison between the array items, which is O(n^2) complexity.
for (let a = 0; a < A.length; a++) {
for (let b = 0; b < A.length; b++) {
if (A[a] === B[b])
// compute the diff
}
}
However, we can create an algorithm focused on small changes that has complexity O(n) with a cutoff.
The algorithm makes the assumption that small changes can be found fast and require small number of mutations to describe.
The cutoff is set at let limit = log(min(A.length, B.length)) and the algorithm has complexity O(limit^2) ~ O(n)
The algorithm -
let limit = log(min(A.length, B.length));
let a=0, b=0;
mainLoop: while (a < A.length && b < B.length) {
if (A(a) === B(b)) {
a += 1;
b += 1
}
continue;
for (let seekSize = 1; seekSize < limit; seekSize++) {
for (let index = 1; index <= seekSize; index++) {
if (A[a+index] === B[b+seekSize-index])
// we have a match, the elements from A between (a..a+index) and replace them with the items from B between (b..b+seekSize-index)
continue mainLoop;
}
}
// revert to direct comparison of attributes json diff model (the standard model used by all other algorithms)
}
- 主要言語
- JavaScript
- スター
- 29k
- フォーク
- 905
- 平均マージ
- 3日 16時間
- マージ済み PR(30日)
- 22
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
immerjs/immer のほかの issue
-
Shrinking an array with `length =` and growing it back loses the patches for the truncated indices対応中かも @RClaudiuM が今日担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
メンテナーはふだん 4 日以内に返信
-
難易度 3/5 1〜2日 初心者へのやさしさ 35/100
メンテナーはふだん 4 日以内に返信
-
Docs: Chinese (zh-CN) website translation is outdated vs English docs再び着手できるかも @lijiayou0728 が 91 日前に担当しましたが、オープン中のプルリクエストはありません。 オープン
難易度 4/5 3〜5日 初心者へのやさしさ 55/100
immerjs/immer#1242 · コメント 5 件 ·
メンテナーはふだん 4 日以内に返信
-
question
難易度 5/5 1週間以上 初心者へのやさしさ 35/100
メンテナーはふだん 4 日以内に返信
-
難易度 5/5 1週間以上 初心者へのやさしさ 35/100
immerjs/immer#1217 · コメント 1 件 ·
メンテナーはふだん 4 日以内に返信
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
Automattic/node-canvas#2633 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
capricorn86/happy-dom#2594 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 79/100
microsoft/monaco-editor#5575 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
メンテナーはふだん 1 日以内に返信