Link: optimize so that no link {} is used, less than half the ram used

This commit is contained in:
Pitchaya Boonsarngsuk
2018-01-30 20:10:46 +00:00
parent 7ca8b2bbc3
commit 409777bc14
2 changed files with 29 additions and 22 deletions

View File

@@ -8,17 +8,28 @@ function startLinkSimulation() {
manualStop = true; manualStop = true;
simulation.stop(); simulation.stop();
p1 = performance.now(); p1 = performance.now();
let links = []; let links = [], force;
// Initialize link array. if (tweakedVerOfLink) {
nodes = simulation.nodes(); force = d3.forceLinkTweaked()
for (i = nodes.length-1; i >= 1; i--) { .distance(function (n) {
for (j = i-1; j >= 0; j--) { return distanceFunction(n.source, n.target, props, norm);
links.push({ });
source: nodes[i], }
target: nodes[j], else {
}); for (i = nodes.length-1; i >= 1; i--) {
for (j = i-1; j >= 0; j--) {
links.push({
source: nodes[i],
target: nodes[j],
});
}
} }
force = d3.forceLink()
.distance(function (n) {
return distanceFunction(n.source, n.target, props, norm);
})
.links(links);
} }
/* Add force /* Add force
@@ -33,11 +44,6 @@ function startLinkSimulation() {
* The full pre-calculation will then occur once when the force is being * The full pre-calculation will then occur once when the force is being
* initialized by the simulation. * initialized by the simulation.
*/ */
let force = tweakedVerOfLink ? d3.forceLinkTweaked() : d3.forceLink();
force.distance(function (n) {
return distanceFunction(n.source, n.target, props, norm);
})
.links(links);
simulation simulation
.alphaDecay(0) .alphaDecay(0)

View File

@@ -18,17 +18,16 @@ export default function(links) {
function force(alpha) { function force(alpha) {
// Each iteration in a tick // Each iteration in a tick
for (var k = 0, n = links.length; k < iterations; ++k) { for (var k = 0, n = nodes.length, source, target, i, j, x, y, l; k < iterations; ++k) {
// For each link // For each link
for (var i = 0, link, source, target, x, y, l, b; i < n; ++i) { for (i = 1; i < n; i++) for (j = 0; j < i; j++) {
link = links[i];
// jiggle so it wont divide / multiply by zero after this // jiggle so it wont divide / multiply by zero after this
source = link.source; source = nodes[i];
target = link.target; target = nodes[j];
x = target.x + target.vx - source.x - source.vx || jiggle(); x = target.x + target.vx - source.x - source.vx || jiggle();
y = target.y + target.vy - source.y - source.vy || jiggle(); y = target.y + target.vy - source.y - source.vy || jiggle();
l = Math.sqrt(x * x + y * y); l = Math.sqrt(x * x + y * y);
l = (l - distances[i]) / l * dataSizeFactor * alpha; l = (l - distances[i*(i-1)/2+j]) / l * dataSizeFactor * alpha;
x *= l, y *= l; x *= l, y *= l;
target.vx -= x; target.vx -= x;
target.vy -= y; target.vy -= y;
@@ -46,8 +45,10 @@ export default function(links) {
function initializeDistance() { function initializeDistance() {
if (!nodes) return; if (!nodes) return;
for (var i = 0, n = links.length; i < n; ++i) { for (let i = 1, n = nodes.length; i < n; i++) {
distances[i] = +distance(links[i], i, links); for (let j = 0; j < i; j++) {
distances.push(distanceFunction(nodes[i], nodes[j], props, norm));
}
} }
} }