Home / Function/ propagateNonNull() — react Function Reference

propagateNonNull() — react Function Reference

Architecture documentation for the propagateNonNull() function in CollectHoistablePropertyLoads.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  73ed54f2_d6a7_c7ec_9243_c999d99e8329["propagateNonNull()"]
  53e05ed1_ffb1_8db2_8573_ef5a3fb99c72["CollectHoistablePropertyLoads.ts"]
  73ed54f2_d6a7_c7ec_9243_c999d99e8329 -->|defined in| 53e05ed1_ffb1_8db2_8573_ef5a3fb99c72
  bb062ddc_03b9_8460_2af6_d98cc2189d33["collectHoistablePropertyLoadsImpl()"]
  bb062ddc_03b9_8460_2af6_d98cc2189d33 -->|calls| 73ed54f2_d6a7_c7ec_9243_c999d99e8329
  14f2e51a_d755_814e_2f56_72d3ed119459["getOrInsertDefault()"]
  73ed54f2_d6a7_c7ec_9243_c999d99e8329 -->|calls| 14f2e51a_d755_814e_2f56_72d3ed119459
  041ca752_10c1_3cda_1f5c_02f44a01310e["invariant()"]
  73ed54f2_d6a7_c7ec_9243_c999d99e8329 -->|calls| 041ca752_10c1_3cda_1f5c_02f44a01310e
  ee9b165b_0867_64d5_5519_91b3d1b0cee2["Set_intersect()"]
  73ed54f2_d6a7_c7ec_9243_c999d99e8329 -->|calls| ee9b165b_0867_64d5_5519_91b3d1b0cee2
  7cd776c7_0283_e0eb_af1b_5474c371bd16["assertNonNull()"]
  73ed54f2_d6a7_c7ec_9243_c999d99e8329 -->|calls| 7cd776c7_0283_e0eb_af1b_5474c371bd16
  35f8cf61_5992_c8a3_811c_0cf8af25b023["Set_union()"]
  73ed54f2_d6a7_c7ec_9243_c999d99e8329 -->|calls| 35f8cf61_5992_c8a3_811c_0cf8af25b023
  bc0d4a49_256e_973f_feb6_ed785896b72b["reduceMaybeOptionalChains()"]
  73ed54f2_d6a7_c7ec_9243_c999d99e8329 -->|calls| bc0d4a49_256e_973f_feb6_ed785896b72b
  e080dc7f_df5e_9e0a_7e03_47e871ebcf2f["Set_equal()"]
  73ed54f2_d6a7_c7ec_9243_c999d99e8329 -->|calls| e080dc7f_df5e_9e0a_7e03_47e871ebcf2f
  style 73ed54f2_d6a7_c7ec_9243_c999d99e8329 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/HIR/CollectHoistablePropertyLoads.ts lines 487–622

function propagateNonNull(
  fn: HIRFunction,
  nodes: ReadonlyMap<BlockId, BlockInfo>,
  registry: PropertyPathRegistry,
): void {
  const blockSuccessors = new Map<BlockId, Set<BlockId>>();
  const terminalPreds = new Set<BlockId>();

  for (const [blockId, block] of fn.body.blocks) {
    for (const pred of block.preds) {
      getOrInsertDefault(blockSuccessors, pred, new Set()).add(blockId);
    }
    if (block.terminal.kind === 'throw' || block.terminal.kind === 'return') {
      terminalPreds.add(blockId);
    }
  }

  /**
   * In the context of a control flow graph, the identifiers that a block
   * can assume are non-null can be calculated from the following:
   * X = Union(Intersect(X_neighbors), X)
   */
  function recursivelyPropagateNonNull(
    nodeId: BlockId,
    direction: 'forward' | 'backward',
    traversalState: Map<BlockId, 'active' | 'done'>,
  ): boolean {
    /**
     * Avoid re-visiting computed or currently active nodes, which can
     * occur when the control flow graph has backedges.
     */
    if (traversalState.has(nodeId)) {
      return false;
    }
    traversalState.set(nodeId, 'active');

    const node = nodes.get(nodeId);
    if (node == null) {
      CompilerError.invariant(false, {
        reason: `Bad node ${nodeId}, kind: ${direction}`,
        loc: GeneratedSource,
      });
    }
    const neighbors = Array.from(
      direction === 'backward'
        ? (blockSuccessors.get(nodeId) ?? [])
        : node.block.preds,
    );

    let changed = false;
    for (const pred of neighbors) {
      if (!traversalState.has(pred)) {
        const neighborChanged = recursivelyPropagateNonNull(
          pred,
          direction,
          traversalState,
        );
        changed ||= neighborChanged;
      }
    }
    /**
     * Note that a predecessor / successor can only be active (status != 'done')
     * if it is a self-loop or other transitive cycle. Active neighbors can be
     * filtered out (i.e. not included in the intersection)
     * Example: self loop.
     *    X = Union(Intersect(X, ...X_other_neighbors), X)
     *
     * Example: transitive cycle through node Y, for some Y that is a
     * predecessor / successor of X.
     *    X = Union(
     *          Intersect(
     *            Union(Intersect(X, ...Y_other_neighbors), Y),
     *            ...X_neighbors
     *          ),
     *          X
     *        )
     *
     * Non-active neighbors with no recorded results can occur due to backedges.
     * it's not safe to assume they can be filtered out (e.g. not included in
     * the intersection)
     */

Subdomains

Frequently Asked Questions

What does propagateNonNull() do?
propagateNonNull() is a function in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/HIR/CollectHoistablePropertyLoads.ts.
Where is propagateNonNull() defined?
propagateNonNull() is defined in compiler/packages/babel-plugin-react-compiler/src/HIR/CollectHoistablePropertyLoads.ts at line 487.
What does propagateNonNull() call?
propagateNonNull() calls 7 function(s): Set_equal, Set_intersect, Set_union, assertNonNull, getOrInsertDefault, invariant, reduceMaybeOptionalChains.
What calls propagateNonNull()?
propagateNonNull() is called by 1 function(s): collectHoistablePropertyLoadsImpl.

Analyze Your Own Codebase

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

Try Supermodel Free