From 2c31533f0a62348d4922d2a9517d2969e7d52b90 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Tue, 13 Oct 2020 22:08:53 -0700 Subject: [PATCH] refactor(core): Use `~x` instead of `-x` which can result in `-0` (#39233) `expandoInstructions` uses negative numbers by `-x`. This has lead to issues in the paste as `-0` is processed as float rather than integer leading to de-optimization. PR Close #39233 --- packages/core/src/render3/instructions/shared.ts | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/packages/core/src/render3/instructions/shared.ts b/packages/core/src/render3/instructions/shared.ts index 53a38ab907..9a3d22a680 100644 --- a/packages/core/src/render3/instructions/shared.ts +++ b/packages/core/src/render3/instructions/shared.ts @@ -79,12 +79,7 @@ export function setHostBindingsByExecutingExpandoInstructions(tView: TView, lVie if (instruction <= 0) { // Negative numbers mean that we are starting new EXPANDO block and need to update // the current element and directive index. - // Important: In JS `-x` and `0-x` is not the same! If `x===0` then `-x` will produce - // `-0` which requires non standard math arithmetic and it can prevent VM optimizations. - // `0-0` will always produce `0` and will not cause a potential deoptimization in VM. - // TODO(misko): PERF This should be refactored to use `~instruction` as that does not - // suffer from `-0` and it is faster/more compact. - currentElementIndex = 0 - instruction; + currentElementIndex = ~instruction; setSelectedIndex(currentElementIndex); // Injector block and providers are taken into account. @@ -1373,10 +1368,7 @@ export function generateExpandoInstructionBlock( tView.firstCreatePass, true, 'Expando block should only be generated on first create pass.'); - // Important: In JS `-x` and `0-x` is not the same! If `x===0` then `-x` will produce `-0` which - // requires non standard math arithmetic and it can prevent VM optimizations. - // `0-0` will always produce `0` and will not cause a potential deoptimization in VM. - const elementIndex = 0 - tNode.index; + const elementIndex = ~tNode.index; const providerStartIndex = tNode.providerIndexes & TNodeProviderIndexes.ProvidersStartIndexMask; const providerCount = tView.data.length - providerStartIndex; (tView.expandoInstructions || (tView.expandoInstructions = []))