Home / Function/ dropManualMemoization() — react Function Reference

dropManualMemoization() — react Function Reference

Architecture documentation for the dropManualMemoization() function in DropManualMemoization.ts from the react codebase.

Entity Profile

Dependency Diagram

graph TD
  dad92af5_71c7_94b3_8cce_ac3af87f3e61["dropManualMemoization()"]
  4b3f307b_2e5b_6c5a_0729_065bd25db103["DropManualMemoization.ts"]
  dad92af5_71c7_94b3_8cce_ac3af87f3e61 -->|defined in| 4b3f307b_2e5b_6c5a_0729_065bd25db103
  6dd28fec_69c3_eac3_c226_cd84ba88484d["findOptionalPlaces()"]
  dad92af5_71c7_94b3_8cce_ac3af87f3e61 -->|calls| 6dd28fec_69c3_eac3_c226_cd84ba88484d
  39c5a476_7a02_eb77_deeb_828d88fc0da0["extractManualMemoizationArgs()"]
  dad92af5_71c7_94b3_8cce_ac3af87f3e61 -->|calls| 39c5a476_7a02_eb77_deeb_828d88fc0da0
  193f2943_7c23_21d6_ef25_f7950dd25d21["getManualMemoizationReplacement()"]
  dad92af5_71c7_94b3_8cce_ac3af87f3e61 -->|calls| 193f2943_7c23_21d6_ef25_f7950dd25d21
  02303def_636f_c5b3_a751_1cf138fcea69["pushDiagnostic()"]
  dad92af5_71c7_94b3_8cce_ac3af87f3e61 -->|calls| 02303def_636f_c5b3_a751_1cf138fcea69
  ac13f5c1_be17_dd7a_6bd3_66d91c46aadf["create()"]
  dad92af5_71c7_94b3_8cce_ac3af87f3e61 -->|calls| ac13f5c1_be17_dd7a_6bd3_66d91c46aadf
  1a2b7047_24c8_62d6_b328_5f07307d27ab["withDetails()"]
  dad92af5_71c7_94b3_8cce_ac3af87f3e61 -->|calls| 1a2b7047_24c8_62d6_b328_5f07307d27ab
  1293701f_8193_f51d_4766_24202f185c80["makeManualMemoizationMarkers()"]
  dad92af5_71c7_94b3_8cce_ac3af87f3e61 -->|calls| 1293701f_8193_f51d_4766_24202f185c80
  bd1bab6c_5b88_55cf_c015_7e286c9e8978["collectTemporaries()"]
  dad92af5_71c7_94b3_8cce_ac3af87f3e61 -->|calls| bd1bab6c_5b88_55cf_c015_7e286c9e8978
  0c09df5a_6c07_11e5_1a6b_9253007463b8["markInstructionIds()"]
  dad92af5_71c7_94b3_8cce_ac3af87f3e61 -->|calls| 0c09df5a_6c07_11e5_1a6b_9253007463b8
  531eb985_e192_f9a2_2d7b_5deeb85ba95c["asResult()"]
  dad92af5_71c7_94b3_8cce_ac3af87f3e61 -->|calls| 531eb985_e192_f9a2_2d7b_5deeb85ba95c
  53244187_914c_cc90_5880_7bfc1fc9c0bb["push()"]
  dad92af5_71c7_94b3_8cce_ac3af87f3e61 -->|calls| 53244187_914c_cc90_5880_7bfc1fc9c0bb
  style dad92af5_71c7_94b3_8cce_ac3af87f3e61 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

compiler/packages/babel-plugin-react-compiler/src/Inference/DropManualMemoization.ts lines 389–554

export function dropManualMemoization(
  func: HIRFunction,
): Result<void, CompilerError> {
  const errors = new CompilerError();
  const isValidationEnabled =
    func.env.config.validatePreserveExistingMemoizationGuarantees ||
    func.env.config.validateNoSetStateInRender ||
    func.env.config.enablePreserveExistingMemoizationGuarantees;
  const optionals = findOptionalPlaces(func);
  const sidemap: IdentifierSidemap = {
    functions: new Map(),
    manualMemos: new Map(),
    react: new Set(),
    maybeDeps: new Map(),
    maybeDepsLists: new Map(),
    optionals,
  };
  let nextManualMemoId = 0;

  /**
   * Phase 1:
   * - Overwrite manual memoization from
   *   CallExpression callee="useMemo/Callback", args=[fnArg, depslist])
   *   to either
   *   CallExpression callee=fnArg
   *   LoadLocal fnArg
   * - (if validation is enabled) collect manual memoization markers
   */
  const queuedInserts: Map<
    InstructionId,
    TInstruction<StartMemoize> | TInstruction<FinishMemoize>
  > = new Map();
  for (const [_, block] of func.body.blocks) {
    for (let i = 0; i < block.instructions.length; i++) {
      const instr = block.instructions[i]!;
      if (
        instr.value.kind === 'CallExpression' ||
        instr.value.kind === 'MethodCall'
      ) {
        const id =
          instr.value.kind === 'CallExpression'
            ? instr.value.callee.identifier.id
            : instr.value.property.identifier.id;

        const manualMemo = sidemap.manualMemos.get(id);
        if (manualMemo != null) {
          const memoDetails = extractManualMemoizationArgs(
            instr as TInstruction<CallExpression> | TInstruction<MethodCall>,
            manualMemo.kind,
            sidemap,
            errors,
          );

          if (memoDetails == null) {
            continue;
          }
          const {fnPlace, depsList, depsLoc} = memoDetails;

          instr.value = getManualMemoizationReplacement(
            fnPlace,
            instr.value.loc,
            manualMemo.kind,
          );
          if (isValidationEnabled) {
            /**
             * Explicitly bail out when we encounter manual memoization
             * without inline instructions, as our current validation
             * assumes that source depslists closely match inferred deps
             * due to the `exhaustive-deps` lint rule (which only provides
             * diagnostics for inline memo functions)
             * ```js
             * useMemo(opaqueFn, [dep1, dep2]);
             * ```
             * While we could handle this by diffing reactive scope deps
             * of the opaque arg against the source depslist, this pattern
             * is rare and likely sketchy.
             */
            if (!sidemap.functions.has(fnPlace.identifier.id)) {
              errors.pushDiagnostic(
                CompilerDiagnostic.create({
                  category: ErrorCategory.UseMemo,

Domain

Subdomains

Frequently Asked Questions

What does dropManualMemoization() do?
dropManualMemoization() is a function in the react codebase, defined in compiler/packages/babel-plugin-react-compiler/src/Inference/DropManualMemoization.ts.
Where is dropManualMemoization() defined?
dropManualMemoization() is defined in compiler/packages/babel-plugin-react-compiler/src/Inference/DropManualMemoization.ts at line 389.
What does dropManualMemoization() call?
dropManualMemoization() calls 11 function(s): asResult, collectTemporaries, create, extractManualMemoizationArgs, findOptionalPlaces, getManualMemoizationReplacement, makeManualMemoizationMarkers, markInstructionIds, and 3 more.

Analyze Your Own Codebase

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

Try Supermodel Free