Home / Function/ _shrink() — react Function Reference

_shrink() — react Function Reference

Architecture documentation for the _shrink() function in HIRBuilder.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  00391787_ef55_81af_824a_4d0b6e524ba2["_shrink()"]
  df6865e0_b573_e905_84d6_4eb6b419a888["HIRBuilder.ts"]
  00391787_ef55_81af_824a_4d0b6e524ba2 -->|defined in| df6865e0_b573_e905_84d6_4eb6b419a888
  041ca752_10c1_3cda_1f5c_02f44a01310e["invariant()"]
  00391787_ef55_81af_824a_4d0b6e524ba2 -->|calls| 041ca752_10c1_3cda_1f5c_02f44a01310e
  93fd52e0_e922_c395_56fd_3bac1489749c["getTargetIfIndirection()"]
  00391787_ef55_81af_824a_4d0b6e524ba2 -->|calls| 93fd52e0_e922_c395_56fd_3bac1489749c
  1e59d6f3_b074_b12f_86b8_b6d2fe62021a["mapTerminalSuccessors()"]
  00391787_ef55_81af_824a_4d0b6e524ba2 -->|calls| 1e59d6f3_b074_b12f_86b8_b6d2fe62021a
  53244187_914c_cc90_5880_7bfc1fc9c0bb["push()"]
  00391787_ef55_81af_824a_4d0b6e524ba2 -->|calls| 53244187_914c_cc90_5880_7bfc1fc9c0bb
  style 00391787_ef55_81af_824a_4d0b6e524ba2 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/HIR/HIRBuilder.ts lines 618–667

function _shrink(func: HIR): void {
  const gotos = new Map();
  /*
   * Given a target block for some terminator, resolves the ideal block that should be
   * targeted instead. This transitively resolves any blocks that are simple indirections
   * (empty blocks that terminate in a goto).
   */
  function resolveBlockTarget(blockId: BlockId): BlockId {
    let target = gotos.get(blockId) ?? null;
    if (target !== null) {
      return target;
    }
    const block = func.blocks.get(blockId);
    CompilerError.invariant(block != null, {
      reason: `expected block ${blockId} to exist`,
      loc: GeneratedSource,
    });
    target = getTargetIfIndirection(block);
    if (target !== null) {
      //  the target might also be a simple goto, recurse
      target = resolveBlockTarget(target) ?? target;
      gotos.set(blockId, target);
      return target;
    } else {
      //  If the block wasn't an indirection, return the original input.
      return blockId;
    }
  }

  const queue = [func.entry];
  const reachable = new Set<BlockId>();
  while (queue.length !== 0) {
    const blockId = queue.shift()!;
    if (reachable.has(blockId)) {
      continue;
    }
    reachable.add(blockId);
    const block = func.blocks.get(blockId)!;
    block.terminal = mapTerminalSuccessors(block.terminal, prevTarget => {
      const target = resolveBlockTarget(prevTarget);
      queue.push(target);
      return target;
    });
  }
  for (const [blockId] of func.blocks) {
    if (!reachable.has(blockId)) {
      func.blocks.delete(blockId);
    }
  }
}

Subdomains

Frequently Asked Questions

What does _shrink() do?
_shrink() is a function in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/HIR/HIRBuilder.ts.
Where is _shrink() defined?
_shrink() is defined in compiler/packages/babel-plugin-react-compiler/src/HIR/HIRBuilder.ts at line 618.
What does _shrink() call?
_shrink() calls 4 function(s): getTargetIfIndirection, invariant, mapTerminalSuccessors, push.

Analyze Your Own Codebase

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

Try Supermodel Free