From 07ebe9624ff036a2732bf6362e1ef29329c1e6ac Mon Sep 17 00:00:00 2001 From: Pawel Kozlowski Date: Wed, 15 May 2019 16:13:59 +0200 Subject: [PATCH] docs(ivy): document rules for the global state access (#30485) PR Close #30485 --- packages/core/src/render3/PERF_NOTES.md | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/packages/core/src/render3/PERF_NOTES.md b/packages/core/src/render3/PERF_NOTES.md index fc94cb2266..625b37a2ac 100644 --- a/packages/core/src/render3/PERF_NOTES.md +++ b/packages/core/src/render3/PERF_NOTES.md @@ -104,8 +104,18 @@ export function i18nStartFirstTemplatePass(tView: TView, index: number, message: } ``` - - ## Loops Don't use `forEach`, it can cause megamorphic function calls (depending on the browser) and function allocations. It is [a lot slower than regular `for` loops](https://jsperf.com/for-vs-foreach-misko) + +## Limit global state access + +Ivy implementation uses some variables in `packages/core/src/render3/state.ts` that could be considered "global state" (those are not truly global variables exposed on `window` but still those variables are easily accessible from anywhere in the ivy codebase). Usage of this global state should be limited to avoid unnecessary function calls (state getters) and improve code readability. + +As a rule, the global state should be accessed _only_ from instructions (functions invoked from the generated code). + +## Instructions should be only called from the generated code + +Instruction functions should be called only from the generated template code. As a consequence of this rule, instructions shouldn't call other instructions. + +Calling instructions from other instructions (or any part of the ivy codebase) multiplies global state access (see previous rule) and makes reasoning about code more difficult. \ No newline at end of file