Skip to content

Commit

Permalink
perf: Optimize performance by preventing memory leaks and reducing un…
Browse files Browse the repository at this point in the history
…necessary operations
  • Loading branch information
sanity committed Nov 27, 2024
1 parent b2ebbbe commit c41802f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
28 changes: 26 additions & 2 deletions hugo-site/static/js/small-world-comparison.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,26 @@ waitForD3().then(() => {
});
}

// Keep SVG reference
let statsSvg;

function updateStats() {
const statsDiv = document.getElementById('statsGraph');

// Reset stats after certain threshold to prevent overflow
if (smallWorldNetwork.stats.attempts > 1000) {
smallWorldNetwork.stats = {
success: 0,
attempts: 0,
totalPathLength: 0
};
randomNetwork.stats = {
success: 0,
attempts: 0,
totalPathLength: 0
};
}

const swSuccess = smallWorldNetwork.stats.attempts === 0 ? 0 :
(smallWorldNetwork.stats.success / smallWorldNetwork.stats.attempts * 100);
const swAvgPath = smallWorldNetwork.stats.success === 0 ? 0 :
Expand All @@ -231,8 +248,15 @@ waitForD3().then(() => {
const rnAvgPath = randomNetwork.stats.success === 0 ? 0 :
(randomNetwork.stats.totalPathLength / randomNetwork.stats.success);

// Clear previous graph
statsDiv.innerHTML = '';
// Create or reuse SVG
if (!statsSvg) {
statsDiv.innerHTML = '';
statsSvg = d3.select('#statsGraph')
.append('svg')
.attr('width', width + margin.left + margin.right)
.attr('height', height + margin.top + margin.bottom);
}
statsSvg.selectAll('*').remove();

// Create SVG
const margin = {top: 30, right: 60, bottom: 40, left: 60};
Expand Down
12 changes: 6 additions & 6 deletions hugo-site/static/js/small-world-routing.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,12 +231,12 @@ async function initVisualization() {
}

function animatePath() {
if (!isPlaying) return;

// Cancel any existing animation
if (animationFrame) {
cancelAnimationFrame(animationFrame);
animationFrame = null;
if (!isPlaying) {
if (animationFrame) {
cancelAnimationFrame(animationFrame);
animationFrame = null;
}
return;
}

const path = findPath(sourceNode, targetNode);
Expand Down
18 changes: 13 additions & 5 deletions hugo-site/static/js/small-world-scale.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,20 +211,22 @@ waitForD3().then(() => {
return null;
}

// Keep SVG reference to avoid repeated selections
let chartSvg;

function updateChart() {
const margin = {top: 20, right: 20, bottom: 60, left: 50};
const chartWidth = width - margin.left - margin.right;
const chartHeight = height - margin.top - margin.bottom;

// Create SVG if it doesn't exist
let svg = d3.select('#scalingChart svg');
if (svg.empty()) {
svg = d3.select('#scalingChart')
// Create or reuse SVG
if (!chartSvg) {
chartSvg = d3.select('#scalingChart')
.append('svg')
.attr('width', width)
.attr('height', height);
}
svg.selectAll('*').remove();
chartSvg.selectAll('*').remove();

const g = svg.append('g')
.attr('transform', `translate(${margin.left},${margin.top})`);
Expand Down Expand Up @@ -332,6 +334,12 @@ waitForD3().then(() => {
numPeers < 200 ? 20 :
numPeers < 350 ? 25 : 30;
numPeers += stepSize;

// Limit array size to prevent memory growth
if (averagePathLengths.length > 100) {
const keepEvery = Math.ceil(averagePathLengths.length / 100);
averagePathLengths = averagePathLengths.filter((_, i) => i % keepEvery === 0);
}

// Allow UI updates between iterations
if (i < batchSize - 1) {
Expand Down

0 comments on commit c41802f

Please sign in to comment.