Home / Function/ pruneableValue() — react Function Reference

pruneableValue() — react Function Reference

Architecture documentation for the pruneableValue() function in DeadCodeElimination.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  4a81cc91_29b6_ce42_c446_03ec75c6b5f2["pruneableValue()"]
  12a58551_b77c_3215_7e97_0c27aabd262e["DeadCodeElimination.ts"]
  4a81cc91_29b6_ce42_c446_03ec75c6b5f2 -->|defined in| 12a58551_b77c_3215_7e97_0c27aabd262e
  b86031b6_78ac_d498_c775_7aedeaf6521e["findReferencedIdentifiers()"]
  b86031b6_78ac_d498_c775_7aedeaf6521e -->|calls| 4a81cc91_29b6_ce42_c446_03ec75c6b5f2
  1ddcaaa6_a274_5751_db05_5448b1a769b9["isIdOrNameUsed()"]
  4a81cc91_29b6_ce42_c446_03ec75c6b5f2 -->|calls| 1ddcaaa6_a274_5751_db05_5448b1a769b9
  568e9c36_44d8_d092_103e_197bdf559e4e["isIdUsed()"]
  4a81cc91_29b6_ce42_c446_03ec75c6b5f2 -->|calls| 568e9c36_44d8_d092_103e_197bdf559e4e
  f5637d03_fd91_50b8_9da7_b2a24c91bab7["eachPatternOperand()"]
  4a81cc91_29b6_ce42_c446_03ec75c6b5f2 -->|calls| f5637d03_fd91_50b8_9da7_b2a24c91bab7
  d7fde76c_4fd9_feb3_299b_798689f05bc6["assertExhaustive()"]
  4a81cc91_29b6_ce42_c446_03ec75c6b5f2 -->|calls| d7fde76c_4fd9_feb3_299b_798689f05bc6
  style 4a81cc91_29b6_ce42_c446_03ec75c6b5f2 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/Optimization/DeadCodeElimination.ts lines 278–408

function pruneableValue(value: InstructionValue, state: State): boolean {
  switch (value.kind) {
    case 'DeclareLocal': {
      // Declarations are pruneable only if the named variable is never read later
      return !state.isIdOrNameUsed(value.lvalue.place.identifier);
    }
    case 'StoreLocal': {
      if (value.lvalue.kind === InstructionKind.Reassign) {
        // Reassignments can be pruned if the specific instance being assigned is never read
        return !state.isIdUsed(value.lvalue.place.identifier);
      }
      // Declarations are pruneable only if the named variable is never read later
      return !state.isIdOrNameUsed(value.lvalue.place.identifier);
    }
    case 'Destructure': {
      let isIdOrNameUsed = false;
      let isIdUsed = false;
      for (const place of eachPatternOperand(value.lvalue.pattern)) {
        if (state.isIdUsed(place.identifier)) {
          isIdOrNameUsed = true;
          isIdUsed = true;
        } else if (state.isIdOrNameUsed(place.identifier)) {
          isIdOrNameUsed = true;
        }
      }
      if (value.lvalue.kind === InstructionKind.Reassign) {
        // Reassignments can be pruned if the specific instance being assigned is never read
        return !isIdUsed;
      } else {
        // Otherwise pruneable only if none of the identifiers are read from later
        return !isIdOrNameUsed;
      }
    }
    case 'PostfixUpdate':
    case 'PrefixUpdate': {
      // Updates are pruneable if the specific instance instance being assigned is never read
      return !state.isIdUsed(value.lvalue.identifier);
    }
    case 'Debugger': {
      // explicitly retain debugger statements to not break debugging workflows
      return false;
    }
    case 'CallExpression':
    case 'MethodCall': {
      if (state.env.outputMode === 'ssr') {
        const calleee =
          value.kind === 'CallExpression' ? value.callee : value.property;
        const hookKind = getHookKind(state.env, calleee.identifier);
        switch (hookKind) {
          case 'useState':
          case 'useReducer':
          case 'useRef': {
            // unused refs can be removed
            return true;
          }
        }
      }
      return false;
    }
    case 'Await':
    case 'ComputedDelete':
    case 'ComputedStore':
    case 'PropertyDelete':
    case 'PropertyStore':
    case 'StoreGlobal': {
      /*
       * Mutating instructions are not safe to prune.
       * TODO: we could be more precise and make this conditional on whether
       * any arguments are actually modified
       */
      return false;
    }
    case 'NewExpression':
    case 'UnsupportedNode':
    case 'TaggedTemplateExpression': {
      // Potentially safe to prune, since they should just be creating new values
      return false;
    }
    case 'GetIterator':
    case 'NextPropertyOf':
    case 'IteratorNext': {

Domain

Subdomains

Frequently Asked Questions

What does pruneableValue() do?
pruneableValue() is a function in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/Optimization/DeadCodeElimination.ts.
Where is pruneableValue() defined?
pruneableValue() is defined in compiler/packages/babel-plugin-react-compiler/src/Optimization/DeadCodeElimination.ts at line 278.
What does pruneableValue() call?
pruneableValue() calls 4 function(s): assertExhaustive, eachPatternOperand, isIdOrNameUsed, isIdUsed.
What calls pruneableValue()?
pruneableValue() is called by 1 function(s): findReferencedIdentifiers.

Analyze Your Own Codebase

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

Try Supermodel Free