loopbackio / loopbackio/loopback-next
Performance issues with ModelUtils._coerce and coerceArray
まだ誰も着手していません。
- 主要言語
- TypeScript
- スター
- 5.1k
- フォーク
- 1.1k
- 平均マージ
- 2日 21時間
- マージ済み PR(30日)
- 27
説明
Describe the bug
The way ModelUtils._coerce uses coerceArray is inefficient, and is causing it to show up as 20% of my application's CPU usage in profiler runs under a create heavy workload.
The problem code is this in juggler:
try {
// Coerce val into an array if it resembles an array-like object
val = coerceArray(val);
} catch (e) {
// NOOP when not coercable into an array.
}
Throwing and catching an error is much more expensive than returning a value, and nearly every call to _coerce hits this try/throw/catch path.

A variant of coerceArray that doesn't throw, but instead returns [arrayVal | undefined, coerced: boolean] I think would fix the performance problem here.
Logs
No response
Additional information
No response
Reproduction
design issue
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
lib/model-utils.js の 498〜503 行付近から始めます。ここで ModelUtils._coerce は try/catch パス内から coerceArray を呼び出しています。coerceArray の動作と、issue で説明されている create-heavy なワークロードを確認し、そのうえで coercion が例外の繰り返しによるコストなしに既存の動作を維持することを検証し、profiling で改善を確認します。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- javascript
- 領域
- backend, performance
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 35/100