Skip to content

Commit

Permalink
Cache remaining interactions that may be cached
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Jun 7, 2022
1 parent 2deea79 commit c43c3df
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 29 deletions.
13 changes: 11 additions & 2 deletions src/ast/NodeInteractions.ts
@@ -1,7 +1,6 @@
import SpreadElement from './nodes/SpreadElement';
import { ExpressionEntity, UNKNOWN_EXPRESSION } from './nodes/shared/Expression';

// TODO Lukas for usages and see if caching makes sense
export const INTERACTION_ACCESSED = 0;
export const INTERACTION_ASSIGNED = 1;
export const INTERACTION_CALLED = 2;
Expand All @@ -11,7 +10,7 @@ export interface NodeInteractionAccessed {
type: typeof INTERACTION_ACCESSED;
}

export const NODE_INTERACTION_ACCESS: NodeInteractionAccessed = {
export const NODE_INTERACTION_UNKNOWN_ACCESS: NodeInteractionAccessed = {
thisArg: null,
type: INTERACTION_ACCESSED
};
Expand Down Expand Up @@ -39,6 +38,16 @@ export interface NodeInteractionCalled {

export const NO_ARGS = [];

// While this is technically a call without arguments, we can compare against
// this reference in places where precise values or thisArg would make a
// difference
export const NODE_INTERACTION_UNKNOWN_CALL: NodeInteractionCalled = {
args: NO_ARGS,
thisArg: null,
type: INTERACTION_CALLED,
withNew: false
};

export type NodeInteraction =
| NodeInteractionAccessed
| NodeInteractionAssigned
Expand Down
8 changes: 6 additions & 2 deletions src/ast/nodes/Identifier.ts
Expand Up @@ -10,7 +10,7 @@ import {
INTERACTION_ACCESSED,
INTERACTION_ASSIGNED,
INTERACTION_CALLED,
NODE_INTERACTION_ACCESS,
NODE_INTERACTION_UNKNOWN_ACCESS,
NodeInteraction,
NodeInteractionCalled
} from '../NodeInteractions';
Expand Down Expand Up @@ -140,7 +140,11 @@ export default class Identifier extends NodeBase implements PatternNode {
return (
(this.context.options.treeshake as NormalizedTreeshakingOptions).unknownGlobalSideEffects &&
this.variable instanceof GlobalVariable &&
this.variable.hasEffectsOnInteractionAtPath(EMPTY_PATH, NODE_INTERACTION_ACCESS, context)
this.variable.hasEffectsOnInteractionAtPath(
EMPTY_PATH,
NODE_INTERACTION_UNKNOWN_ACCESS,
context
)
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/ast/nodes/SpreadElement.ts
@@ -1,7 +1,7 @@
import type { NormalizedTreeshakingOptions } from '../../rollup/types';
import type { HasEffectsContext } from '../ExecutionContext';
import type { NodeInteractionWithThisArg } from '../NodeInteractions';
import { NODE_INTERACTION_ACCESS } from '../NodeInteractions';
import { NODE_INTERACTION_UNKNOWN_ACCESS } from '../NodeInteractions';
import { type ObjectPath, type PathTracker, UNKNOWN_PATH, UnknownKey } from '../utils/PathTracker';
import type * as NodeType from './NodeType';
import { type ExpressionNode, NodeBase } from './shared/Node';
Expand Down Expand Up @@ -34,7 +34,7 @@ export default class SpreadElement extends NodeBase {
(propertyReadSideEffects === 'always' ||
this.argument.hasEffectsOnInteractionAtPath(
UNKNOWN_PATH,
NODE_INTERACTION_ACCESS,
NODE_INTERACTION_UNKNOWN_ACCESS,
context
)))
);
Expand Down
8 changes: 4 additions & 4 deletions src/ast/nodes/shared/FunctionBase.ts
Expand Up @@ -6,9 +6,9 @@ import {
type InclusionContext
} from '../../ExecutionContext';
import {
INTERACTION_ACCESSED,
INTERACTION_CALLED,
NO_ARGS,
NODE_INTERACTION_UNKNOWN_ACCESS,
NODE_INTERACTION_UNKNOWN_CALL,
NodeInteraction,
NodeInteractionCalled,
NodeInteractionWithThisArg
Expand Down Expand Up @@ -105,14 +105,14 @@ export default abstract class FunctionBase extends NodeBase {
if (
returnExpression.hasEffectsOnInteractionAtPath(
['then'],
{ args: NO_ARGS, thisArg: returnExpression, type: INTERACTION_CALLED, withNew: false },
NODE_INTERACTION_UNKNOWN_CALL,
context
) ||
(propertyReadSideEffects &&
(propertyReadSideEffects === 'always' ||
returnExpression.hasEffectsOnInteractionAtPath(
['then'],
{ thisArg: returnExpression, type: INTERACTION_ACCESSED },
NODE_INTERACTION_UNKNOWN_ACCESS,
context
)))
) {
Expand Down
3 changes: 2 additions & 1 deletion src/ast/nodes/shared/MethodBase.ts
Expand Up @@ -5,6 +5,7 @@ import {
INTERACTION_ASSIGNED,
INTERACTION_CALLED,
NO_ARGS,
NODE_INTERACTION_UNKNOWN_CALL,
NodeInteraction,
NodeInteractionCalled,
NodeInteractionWithThisArg
Expand Down Expand Up @@ -139,7 +140,7 @@ export default class MethodBase extends NodeBase implements DeoptimizableEntity
this.accessedValue = UNKNOWN_EXPRESSION;
return (this.accessedValue = this.value.getReturnExpressionWhenCalledAtPath(
EMPTY_PATH,
{ args: NO_ARGS, thisArg: null, type: INTERACTION_CALLED, withNew: false },
NODE_INTERACTION_UNKNOWN_CALL,
SHARED_RECURSION_TRACKER,
this
));
Expand Down
9 changes: 2 additions & 7 deletions src/ast/nodes/shared/MethodTypes.ts
Expand Up @@ -2,8 +2,8 @@ import type { HasEffectsContext } from '../../ExecutionContext';
import {
INTERACTION_ACCESSED,
INTERACTION_CALLED,
NO_ARGS,
NODE_INTERACTION_UNKNOWN_ASSIGNMENT,
NODE_INTERACTION_UNKNOWN_CALL,
NodeInteraction,
NodeInteractionCalled,
NodeInteractionWithThisArg
Expand Down Expand Up @@ -84,12 +84,7 @@ export class Method extends ExpressionEntity {
if (
interaction.args[argIndex]?.hasEffectsOnInteractionAtPath(
EMPTY_PATH,
{
args: NO_ARGS,
thisArg: null,
type: INTERACTION_CALLED,
withNew: false
},
NODE_INTERACTION_UNKNOWN_CALL,
context
)
) {
Expand Down
13 changes: 2 additions & 11 deletions src/ast/values.ts
Expand Up @@ -2,7 +2,7 @@ import type { HasEffectsContext } from './ExecutionContext';
import {
INTERACTION_ACCESSED,
INTERACTION_CALLED,
NO_ARGS,
NODE_INTERACTION_UNKNOWN_CALL,
NodeInteraction,
NodeInteractionCalled
} from './NodeInteractions';
Expand Down Expand Up @@ -153,16 +153,7 @@ const stringReplace: RawMemberDescription = {
(typeof arg1.getLiteralValueAtPath(EMPTY_PATH, SHARED_RECURSION_TRACKER, {
deoptimizeCache() {}
}) === 'symbol' &&
arg1.hasEffectsOnInteractionAtPath(
EMPTY_PATH,
{
args: NO_ARGS,
thisArg: null,
type: INTERACTION_CALLED,
withNew: false
},
context
))
arg1.hasEffectsOnInteractionAtPath(EMPTY_PATH, NODE_INTERACTION_UNKNOWN_CALL, context))
);
},
returns: UNKNOWN_LITERAL_STRING
Expand Down

0 comments on commit c43c3df

Please sign in to comment.