Skip to content

Commit

Permalink
[compiler] Add context callee import if required
Browse files Browse the repository at this point in the history
Previously the compiler would add an import for the specified context
callee even if the context access was not lowered, leading to unused
imports.

This PR tracks if lowering has happened and adds the import only when
necessary.

[ghstack-poisoned]
  • Loading branch information
gsathya committed Aug 7, 2024
1 parent 5112793 commit 7deadab
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,9 @@ export function compileProgram(
}
}

const hasLoweredContextAccess = compiledFns.some(
c => c.compiledFn.hasLoweredContextAccess,
);
const externalFunctions: Array<ExternalFunction> = [];
let gating: null | ExternalFunction = null;
try {
Expand All @@ -512,7 +515,7 @@ export function compileProgram(
}

const lowerContextAccess = pass.opts.environment?.lowerContextAccess;
if (lowerContextAccess) {
if (lowerContextAccess && hasLoweredContextAccess) {
externalFunctions.push(tryParseExternalFunction(lowerContextAccess));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,7 @@ export class Environment {
config: EnvironmentConfig;
fnType: ReactFunctionType;
useMemoCacheIdentifier: string;
hasLoweredContextAccess: boolean;

#contextIdentifiers: Set<t.Identifier>;
#hoistedIdentifiers: Set<t.Identifier>;
Expand All @@ -575,6 +576,7 @@ export class Environment {
this.useMemoCacheIdentifier = useMemoCacheIdentifier;
this.#shapes = new Map(DEFAULT_SHAPES);
this.#globals = new Map(DEFAULT_GLOBALS);
this.hasLoweredContextAccess = false;

if (
config.disableMemoizationForDebugging &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export function lowerContextAccess(
}
}

if (contextAccess.size > 0) {
if (contextAccess.size > 0 && contextKeys.size > 0) {
for (const [, block] of fn.body.blocks) {
let nextInstructions: Array<Instruction> | null = null;

Expand Down Expand Up @@ -120,6 +120,7 @@ export function lowerContextAccess(
}
markInstructionIds(fn.body);
inferTypes(fn);
fn.env.hasLoweredContextAccess = true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ export type CodegenFunction = {
fn: CodegenFunction;
type: ReactFunctionType | null;
}>;

/**
* This is true if the compiler has the lowered useContext calls.
*/
hasLoweredContextAccess: boolean;
};

export function codegenFunction(
Expand Down Expand Up @@ -348,6 +353,7 @@ function codegenReactiveFunction(
prunedMemoBlocks: countMemoBlockVisitor.prunedMemoBlocks,
prunedMemoValues: countMemoBlockVisitor.prunedMemoValues,
outlined: [],
hasLoweredContextAccess: fn.env.hasLoweredContextAccess,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ function App() {
## Code

```javascript
import { useContext_withSelector } from "react-compiler-runtime";
import { c as _c } from "react/compiler-runtime"; // @lowerContextAccess
function App() {
const $ = _c(3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ function App() {
## Code

```javascript
import { useContext_withSelector } from "react-compiler-runtime";
import { c as _c } from "react/compiler-runtime"; // @lowerContextAccess
function App() {
const $ = _c(3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ function App() {
## Code

```javascript
import { useContext_withSelector } from "react-compiler-runtime";
import { c as _c } from "react/compiler-runtime"; // @lowerContextAccess
function App() {
const $ = _c(3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ function App() {
## Code

```javascript
import { useContext_withSelector } from "react-compiler-runtime";
import { c as _c } from "react/compiler-runtime"; // @lowerContextAccess
function App() {
const $ = _c(3);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ function App() {
## Code

```javascript
import { useContext_withSelector } from "react-compiler-runtime";
import { c as _c } from "react/compiler-runtime"; // @lowerContextAccess
function App() {
const $ = _c(3);
Expand Down

0 comments on commit 7deadab

Please sign in to comment.