Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change edge types to numbers #6126

Merged
merged 16 commits into from
Sep 16, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
187 changes: 136 additions & 51 deletions packages/core/core/src/BundleGraph.js

Large diffs are not rendered by default.

39 changes: 22 additions & 17 deletions packages/core/core/src/Graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ import type {TraversalActions, GraphVisitor} from '@parcel/types';
import assert from 'assert';
import nullthrows from 'nullthrows';

export type GraphOpts<TNode, TEdgeType: string | null = null> = {|
type NullEdgeType = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should not support the null edge type at all, and just have users/extenders always specify the types of edges the way they do nodes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

export type GraphOpts<TNode, TEdgeType: number = 0> = {|
nodes?: Map<NodeId, TNode>,
edges?: AdjacencyListMap<TEdgeType | null>,
edges?: AdjacencyListMap<TEdgeType | NullEdgeType>,
rootNodeId?: ?NodeId,
|};

export const ALL_EDGE_TYPES = '@@all_edge_types';

export default class Graph<TNode: Node, TEdgeType: string | null = null> {
export default class Graph<TNode: Node, TEdgeType: number = 0> {
nodes: Map<NodeId, TNode>;
inboundEdges: AdjacencyList<TEdgeType | null>;
outboundEdges: AdjacencyList<TEdgeType | null>;
inboundEdges: AdjacencyList<TEdgeType | NullEdgeType>;
outboundEdges: AdjacencyList<TEdgeType | NullEdgeType>;
rootNodeId: ?NodeId;

constructor(opts: GraphOpts<TNode, TEdgeType> = ({}: any)) {
Expand Down Expand Up @@ -60,7 +61,7 @@ export default class Graph<TNode: Node, TEdgeType: string | null = null> {

// Returns a list of all edges in the graph. This can be large, so iterating
// the complete list can be costly in large graphs. Used when merging graphs.
getAllEdges(): Array<Edge<TEdgeType | null>> {
getAllEdges(): Array<Edge<TEdgeType | NullEdgeType>> {
let edges = [];
for (let [from, edgeList] of this.outboundEdges.getListMap()) {
for (let [type, toNodes] of edgeList) {
Expand Down Expand Up @@ -98,7 +99,7 @@ export default class Graph<TNode: Node, TEdgeType: string | null = null> {
return this.rootNodeId ? this.getNode(this.rootNodeId) : null;
}

addEdge(from: NodeId, to: NodeId, type: TEdgeType | null = null): void {
addEdge(from: NodeId, to: NodeId, type: TEdgeType | NullEdgeType = 0): void {
if (!this.getNode(from)) {
throw new Error(`"from" node '${from}' not found`);
}
Expand All @@ -111,13 +112,17 @@ export default class Graph<TNode: Node, TEdgeType: string | null = null> {
this.inboundEdges.addEdge(to, from, type);
}

hasEdge(from: NodeId, to: NodeId, type?: TEdgeType | null = null): boolean {
hasEdge(
from: NodeId,
to: NodeId,
type?: TEdgeType | NullEdgeType = 0,
): boolean {
return this.outboundEdges.hasEdge(from, to, type);
}

getNodesConnectedTo(
node: TNode,
type: TEdgeType | null | Array<TEdgeType | null> = null,
type: TEdgeType | NullEdgeType | Array<TEdgeType | NullEdgeType> = 0,
): Array<TNode> {
assertHasNode(this, node);

Expand Down Expand Up @@ -150,7 +155,7 @@ export default class Graph<TNode: Node, TEdgeType: string | null = null> {

getNodesConnectedFrom(
node: TNode,
type: TEdgeType | null | Array<TEdgeType | null> = null,
type: TEdgeType | NullEdgeType | Array<TEdgeType | NullEdgeType> = 0,
): Array<TNode> {
assertHasNode(this, node);

Expand Down Expand Up @@ -215,7 +220,7 @@ export default class Graph<TNode: Node, TEdgeType: string | null = null> {
this.removeNode(node);
}

removeEdges(node: TNode, type: TEdgeType | null = null) {
removeEdges(node: TNode, type: TEdgeType | NullEdgeType = 0) {
assertHasNode(this, node);

for (let to of this.outboundEdges.getEdges(node.id, type)) {
Expand All @@ -227,7 +232,7 @@ export default class Graph<TNode: Node, TEdgeType: string | null = null> {
removeEdge(
from: NodeId,
to: NodeId,
type: TEdgeType | null = null,
type: TEdgeType | NullEdgeType = 0,
removeOrphans: boolean = true,
) {
if (!this.outboundEdges.hasEdge(from, to, type)) {
Expand Down Expand Up @@ -291,7 +296,7 @@ export default class Graph<TNode: Node, TEdgeType: string | null = null> {
replaceNode(
fromNode: TNode,
toNode: TNode,
type: TEdgeType | null = null,
type: TEdgeType | NullEdgeType = 0,
): void {
assertHasNode(this, fromNode);

Expand All @@ -310,7 +315,7 @@ export default class Graph<TNode: Node, TEdgeType: string | null = null> {
fromNode: TNode,
toNodes: $ReadOnlyArray<TNode>,
replaceFilter?: null | (TNode => boolean),
type?: TEdgeType | null = null,
type?: TEdgeType | NullEdgeType = 0,
): void {
assertHasNode(this, fromNode);

Expand Down Expand Up @@ -339,7 +344,7 @@ export default class Graph<TNode: Node, TEdgeType: string | null = null> {
traverse<TContext>(
visit: GraphVisitor<TNode, TContext>,
startNode: ?TNode,
type: TEdgeType | null | Array<TEdgeType | null> = null,
type: TEdgeType | NullEdgeType | Array<TEdgeType | NullEdgeType> = 0,
): ?TContext {
return this.dfs({
visit,
Expand All @@ -352,15 +357,15 @@ export default class Graph<TNode: Node, TEdgeType: string | null = null> {
filter: (TNode, TraversalActions) => ?TValue,
visit: GraphVisitor<TValue, TContext>,
startNode: ?TNode,
type?: TEdgeType | null | Array<TEdgeType | null>,
type?: TEdgeType | Array<TEdgeType | NullEdgeType>,
): ?TContext {
return this.traverse(mapVisitor(filter, visit), startNode, type);
}

traverseAncestors<TContext>(
startNode: TNode,
visit: GraphVisitor<TNode, TContext>,
type: TEdgeType | null | Array<TEdgeType | null> = null,
type: TEdgeType | NullEdgeType | Array<TEdgeType | NullEdgeType> = 0,
): ?TContext {
return this.dfs({
visit,
Expand Down
22 changes: 17 additions & 5 deletions packages/core/core/src/Parcel.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ import {AbortController} from 'abortcontroller-polyfill/dist/cjs-ponyfill';
import {PromiseQueue} from '@parcel/utils';
import ParcelConfig from './ParcelConfig';
import logger from '@parcel/logger';
import RequestTracker, {getWatcherOptions} from './RequestTracker';
import {bundleGraphEdgeTypes} from './BundleGraph';
import RequestTracker, {
getWatcherOptions,
requestGraphEdgeTypes,
} from './RequestTracker';
import createAssetGraphRequest from './requests/AssetGraphRequest';
import createValidationRequest from './requests/ValidationRequest';
import createBundleGraphRequest from './requests/BundleGraphRequest';
Expand Down Expand Up @@ -295,14 +299,22 @@ export default class Parcel {
bundleGraphRequest,
);

// $FlowFixMe Added in Flow 0.121.0 upgrade in #4381 (Windows only)
dumpGraphToGraphViz(bundleGraph._graph, 'BundleGraph');
dumpGraphToGraphViz(
// $FlowFixMe Added in Flow 0.121.0 upgrade in #4381 (Windows only)
bundleGraph._graph,
'BundleGraph',
bundleGraphEdgeTypes,
);

await this.#packagerRunner.writeBundles(bundleGraph);
assertSignalNotAborted(signal);

// $FlowFixMe
dumpGraphToGraphViz(this.#requestTracker.graph, 'RequestGraph');
dumpGraphToGraphViz(
// $FlowFixMe
this.#requestTracker.graph,
'RequestGraph',
requestGraphEdgeTypes,
);

let event = {
type: 'buildSuccess',
Expand Down
Loading