Home / Function/ visitInstruction() — react Function Reference

visitInstruction() — react Function Reference

Architecture documentation for the visitInstruction() function in ValidatePreservedManualMemoization.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  ad786783_79dc_d97c_fb6f_d2e1268b8fd5["visitInstruction()"]
  310c2965_2897_08a7_588d_8109c9f88c0c["Visitor"]
  ad786783_79dc_d97c_fb6f_d2e1268b8fd5 -->|defined in| 310c2965_2897_08a7_588d_8109c9f88c0c
  2430e594_74e0_ca9a_14be_251ff6fa4bb8["recordDepsInValue()"]
  2430e594_74e0_ca9a_14be_251ff6fa4bb8 -->|calls| ad786783_79dc_d97c_fb6f_d2e1268b8fd5
  1d0d9ade_8ca6_4897_7d4a_358a8565f5b3["recordTemporaries()"]
  ad786783_79dc_d97c_fb6f_d2e1268b8fd5 -->|calls| 1d0d9ade_8ca6_4897_7d4a_358a8565f5b3
  af4d3127_0abf_3e44_50a8_d7a9dd0b9b58["visitInstruction()"]
  ad786783_79dc_d97c_fb6f_d2e1268b8fd5 -->|calls| af4d3127_0abf_3e44_50a8_d7a9dd0b9b58
  14f2e51a_d755_814e_2f56_72d3ed119459["getOrInsertDefault()"]
  ad786783_79dc_d97c_fb6f_d2e1268b8fd5 -->|calls| 14f2e51a_d755_814e_2f56_72d3ed119459
  041ca752_10c1_3cda_1f5c_02f44a01310e["invariant()"]
  ad786783_79dc_d97c_fb6f_d2e1268b8fd5 -->|calls| 041ca752_10c1_3cda_1f5c_02f44a01310e
  b2fc2985_a7ba_9865_c2a3_2a7531f27d44["eachInstructionValueOperand()"]
  ad786783_79dc_d97c_fb6f_d2e1268b8fd5 -->|calls| b2fc2985_a7ba_9865_c2a3_2a7531f27d44
  02303def_636f_c5b3_a751_1cf138fcea69["pushDiagnostic()"]
  ad786783_79dc_d97c_fb6f_d2e1268b8fd5 -->|calls| 02303def_636f_c5b3_a751_1cf138fcea69
  ac13f5c1_be17_dd7a_6bd3_66d91c46aadf["create()"]
  ad786783_79dc_d97c_fb6f_d2e1268b8fd5 -->|calls| ac13f5c1_be17_dd7a_6bd3_66d91c46aadf
  1a2b7047_24c8_62d6_b328_5f07307d27ab["withDetails()"]
  ad786783_79dc_d97c_fb6f_d2e1268b8fd5 -->|calls| 1a2b7047_24c8_62d6_b328_5f07307d27ab
  57237ae0_bd7e_03e2_5399_c9bb90e5cf6a["isUnmemoized()"]
  ad786783_79dc_d97c_fb6f_d2e1268b8fd5 -->|calls| 57237ae0_bd7e_03e2_5399_c9bb90e5cf6a
  e7a01d7c_c128_be66_a07d_007952a380da["printIdentifier()"]
  ad786783_79dc_d97c_fb6f_d2e1268b8fd5 -->|calls| e7a01d7c_c128_be66_a07d_007952a380da
  style ad786783_79dc_d97c_fb6f_d2e1268b8fd5 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/Validation/ValidatePreservedManualMemoization.ts lines 452–604

  override visitInstruction(
    instruction: ReactiveInstruction,
    state: VisitorState,
  ): void {
    /**
     * We don't invoke traverseInstructions because `recordDepsInValue`
     * recursively visits ReactiveValues and instructions
     */
    this.recordTemporaries(instruction, state);
    const value = instruction.value;
    // Track reassignments from inlining of manual memo
    if (
      value.kind === 'StoreLocal' &&
      value.lvalue.kind === 'Reassign' &&
      state.manualMemoState != null
    ) {
      // Complex cases of inlining end up with a temporary that is reassigned
      const ids = getOrInsertDefault(
        state.manualMemoState.reassignments,
        value.lvalue.place.identifier.declarationId,
        new Set(),
      );
      ids.add(value.value.identifier);
    }
    if (
      value.kind === 'LoadLocal' &&
      value.place.identifier.scope != null &&
      instruction.lvalue != null &&
      instruction.lvalue.identifier.scope == null &&
      state.manualMemoState != null
    ) {
      // Simpler cases of inlining assign to the original IIFE lvalue
      const ids = getOrInsertDefault(
        state.manualMemoState.reassignments,
        instruction.lvalue.identifier.declarationId,
        new Set(),
      );
      ids.add(value.place.identifier);
    }
    if (value.kind === 'StartMemoize') {
      let depsFromSource: Array<ManualMemoDependency> | null = null;
      if (value.deps != null) {
        depsFromSource = value.deps;
      }
      CompilerError.invariant(state.manualMemoState == null, {
        reason: 'Unexpected nested StartMemoize instructions',
        description: `Bad manual memoization ids: ${state.manualMemoState?.manualMemoId}, ${value.manualMemoId}`,
        loc: value.loc,
      });

      state.manualMemoState = {
        loc: instruction.loc,
        decls: new Set(),
        depsFromSource,
        manualMemoId: value.manualMemoId,
        reassignments: new Map(),
      };

      /**
       * We check that each scope dependency is either:
       * (1) Not scoped
       *     Checking `identifier.scope == null` is a proxy for whether the dep
       *     is a primitive, global, or other guaranteed non-allocating value.
       *     Non-allocating values do not need memoization.
       *     Note that this is a conservative estimate as some primitive-typed
       *     variables do receive scopes.
       * (2) Scoped (a maybe newly-allocated value with a mutable range)
       *     Here, we check that the dependency's scope has completed before
       *     the manual useMemo as a proxy for mutable-range checking. This
       *     validates that there are no potential rule-of-react violations
       *     in source.
       *     Note that scope range is an overly conservative proxy as we merge
       *     overlapping ranges.
       *     See fixture `error.false-positive-useMemo-overlap-scopes`
       */
      for (const {identifier, loc} of eachInstructionValueOperand(
        value as InstructionValue,
      )) {
        if (
          identifier.scope != null &&
          !this.scopes.has(identifier.scope.id) &&

Domain

Subdomains

Frequently Asked Questions

What does visitInstruction() do?
visitInstruction() is a function in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/Validation/ValidatePreservedManualMemoization.ts.
Where is visitInstruction() defined?
visitInstruction() is defined in compiler/packages/babel-plugin-react-compiler/src/Validation/ValidatePreservedManualMemoization.ts at line 452.
What does visitInstruction() call?
visitInstruction() calls 10 function(s): create, eachInstructionValueOperand, getOrInsertDefault, invariant, isUnmemoized, printIdentifier, pushDiagnostic, recordTemporaries, and 2 more.
What calls visitInstruction()?
visitInstruction() is called by 1 function(s): recordDepsInValue.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free