antvis / layout

Layout algorithms for graphs.
206 stars 55 forks source link

深度较大的数据初始渲染时,dfs递归导致内存溢出 #75

Open Kijin-Seija opened 2 years ago

Kijin-Seija commented 2 years ago

使用g6时遇到了一个问题,如下demo

demo

image

demo中是2000条首尾相连的数据,布局模式使用Dagre

定位到 https://github.com/antvis/layout/blob/master/src/layout/dagre/src/order/init-order.ts 里面的方法dfs

const dfs = (v: string) => {
    if (visited.hasOwnProperty(v)) return;
    visited[v] = true;
    const node = g.node(v);
    if (!isNaN(node.rank as number)) {
      layers[node.rank as number].push(v);
    }
    g.successors(v)?.forEach((child) => dfs(child as any));
  };

此demo中图的深度较大,猜测是递归pending的dfs函数过多导致内存溢出。本地调试改为bfs后不再报错(但导致了初始化时图不居中)

const dfs = (v: string) => {
    let cur = [v];
    while(cur.length) {
      let temp: any[] = [];
      cur.forEach((item: any) => {
        if (visited.hasOwnProperty(item)) return;
        visited[item] = true;
        const node = g.node(item);
        if (!isNaN(node.rank as number)) {
          layers[node.rank as number].push(item);
        }
        temp = [...temp, ...(g.successors(v) || [])];
      });
      cur = temp;
    }
  };

image

不知是否有相关的配置可以解决这个问题,或者说一个不成熟的建议是提供给用户可选dfs或者bfs进行初始布局?

Kijin-Seija commented 2 years ago

有大佬能帮忙解答一下吗,对应的代码,相关的demo都齐全,我这边因为对整体工程还不太了解,不敢贸然提pr。感谢