Skip to content

Commit e23ec52

Browse files
committed
plugged up some KILLER memory leaks
(although I'm still not 100% satisfied yet)
1 parent 02d4229 commit e23ec52

1 file changed

Lines changed: 94 additions & 90 deletions

File tree

‎v3/js/pytutor.js‎

Lines changed: 94 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,6 +1415,7 @@ ExecutionVisualizer.prototype.renderDataStructures = function() {
14151415
// connectors and redraw them from scratch. doing so avoids mysterious
14161416
// jsPlumb connector alignment issues when the visualizer's enclosing
14171417
// div contains, say, a "position: relative;" CSS tag
1418+
// (which happens in the IPython Notebook)
14181419
var existingConnectionEndpointIDs = d3.map();
14191420

14201421
myViz.jsPlumbInstance.select().each(function(c) {
@@ -1431,9 +1432,6 @@ ExecutionVisualizer.prototype.renderDataStructures = function() {
14311432
}
14321433
});
14331434

1434-
myViz.jsPlumbInstance.reset(); // delete all connectors and state!!!
1435-
1436-
14371435
/*
14381436
existingConnectionEndpointIDs.forEach(function(varID, valueID) {
14391437
console.log('existingConnectionEndpointIDs:', varID, valueID);
@@ -1499,12 +1497,20 @@ ExecutionVisualizer.prototype.renderDataStructures = function() {
14991497
.style('opacity', '0')
15001498
.duration(500)
15011499
.each('end', function() {
1502-
hrExit.remove();
1500+
hrExit
1501+
.each(function(d, idx) {
1502+
$(this).empty(); // crucial for garbage collecting jsPlumb connectors!
1503+
})
1504+
.remove();
15031505
myViz.redrawConnectors();
15041506
});
15051507
}
15061508
else {
1507-
hrExit.remove();
1509+
hrExit
1510+
.each(function(d, idx) {
1511+
$(this).empty(); // crucial for garbage collecting jsPlumb connectors!
1512+
})
1513+
.remove();
15081514
}
15091515

15101516

@@ -1557,12 +1563,20 @@ ExecutionVisualizer.prototype.renderDataStructures = function() {
15571563
.style('opacity', '0') /* fade out */
15581564
.duration(500)
15591565
.each('end', function() {
1560-
tlhExit.remove();
1566+
tlhExit
1567+
.each(function(d, idx) {
1568+
$(this).empty(); // crucial for garbage collecting jsPlumb connectors!
1569+
})
1570+
.remove();
15611571
myViz.redrawConnectors();
15621572
});
15631573
}
15641574
else {
1565-
tlhExit.remove();
1575+
tlhExit
1576+
.each(function(d, idx) {
1577+
$(this).empty(); // crucial for garbage collecting jsPlumb connectors!
1578+
})
1579+
.remove();
15661580
}
15671581

15681582

@@ -1885,64 +1899,53 @@ ExecutionVisualizer.prototype.renderDataStructures = function() {
18851899
$(this).html(varname);
18861900
}
18871901
else {
1888-
var val = curEntry.globals[varname];
1902+
// always delete and re-render the global var ...
1903+
// NB: trying to cache and compare the old value using,
1904+
// say -- $(this).attr('data-curvalue', valStringRepr) -- leads to
1905+
// a mysterious and killer memory leak that I can't figure out yet
1906+
$(this).empty();
18891907

1890-
// include type in repr to prevent conflating integer 5 with string "5"
1891-
var valStringRepr = String(typeof val) + ':' + String(val);
1908+
// make sure varname doesn't contain any weird
1909+
// characters that are illegal for CSS ID's ...
1910+
var varDivID = myViz.generateID('global__' + varnameToCssID(varname));
18921911

1893-
// SUPER HACK - retrieve previous value as a hidden attribute
1894-
// TODO: use the jQuery .data() method to store arbitrary data
1895-
// inside of a DOM element, so that we can avoid munging strings:
1896-
// http://api.jquery.com/data/
1897-
var prevValStringRepr = $(this).attr('data-curvalue');
1912+
// need to get rid of the old connector in preparation for rendering a new one:
1913+
existingConnectionEndpointIDs.remove(varDivID);
18981914

1899-
// IMPORTANT! only clear the div and render a new element if the
1900-
// value has changed
1901-
if (valStringRepr != prevValStringRepr) {
1902-
// TODO: render a transition
1903-
1904-
$(this).empty(); // crude but effective for now
1905-
1906-
// make sure varname doesn't contain any weird
1907-
// characters that are illegal for CSS ID's ...
1908-
var varDivID = myViz.generateID('global__' + varnameToCssID(varname));
1909-
1910-
// need to get rid of the old connector in preparation for rendering a new one:
1911-
existingConnectionEndpointIDs.remove(varDivID);
1912-
1913-
if (isPrimitiveType(val)) {
1914-
renderPrimitiveObject(val, $(this));
1915-
}
1916-
else {
1917-
// add a stub so that we can connect it with a connector later.
1918-
// IE needs this div to be NON-EMPTY in order to properly
1919-
// render jsPlumb endpoints, so that's why we add an " "!
1920-
$(this).append('<div class="stack_pointer" id="' + varDivID + '">&nbsp;</div>');
1921-
1922-
assert(!connectionEndpointIDs.has(varDivID));
1923-
var heapObjID = myViz.generateID('heap_object_' + getRefID(val));
1924-
connectionEndpointIDs.set(varDivID, heapObjID);
1925-
//console.log('STACK->HEAP', varDivID, heapObjID);
1926-
}
1927-
1928-
//console.log('CHANGED', varname, prevValStringRepr, valStringRepr);
1915+
var val = curEntry.globals[varname];
1916+
if (isPrimitiveType(val)) {
1917+
renderPrimitiveObject(val, $(this));
1918+
}
1919+
else {
1920+
// add a stub so that we can connect it with a connector later.
1921+
// IE needs this div to be NON-EMPTY in order to properly
1922+
// render jsPlumb endpoints, so that's why we add an "&nbsp;"!
1923+
$(this).append('<div class="stack_pointer" id="' + varDivID + '">&nbsp;</div>');
1924+
1925+
assert(!connectionEndpointIDs.has(varDivID));
1926+
var heapObjID = myViz.generateID('heap_object_' + getRefID(val));
1927+
connectionEndpointIDs.set(varDivID, heapObjID);
1928+
//console.log('STACK->HEAP', varDivID, heapObjID);
19291929
}
1930-
1931-
// SUPER HACK - set current value as a hidden string attribute
1932-
$(this).attr('data-curvalue', valStringRepr);
19331930
}
1934-
19351931
});
19361932

19371933

1938-
globalVarTableCells.exit().remove();
1934+
1935+
globalVarTableCells.exit()
1936+
.each(function(d, idx) {
1937+
$(this).empty(); // crucial for garbage collecting jsPlumb connectors!
1938+
})
1939+
.remove();
19391940

19401941
globalVarTable.exit()
19411942
.each(function(d, i) {
19421943
// detach all stack_pointer connectors for divs that are being removed
19431944
$(this).find('.stack_pointer').each(function(i, sp) {
19441945
existingConnectionEndpointIDs.remove($(sp).attr('id'));
19451946
});
1947+
1948+
$(this).empty(); // crucial for garbage collecting jsPlumb connectors!
19461949
})
19471950
.remove();
19481951

@@ -2048,61 +2051,52 @@ ExecutionVisualizer.prototype.renderDataStructures = function() {
20482051
$(this).html(varname);
20492052
}
20502053
else {
2051-
var val = frame.encoded_locals[varname];
2052-
2053-
// include type in repr to prevent conflating integer 5 with string "5"
2054-
var valStringRepr = String(typeof val) + ':' + String(val);
2055-
2056-
// SUPER HACK - retrieve previous value as a hidden attribute
2057-
var prevValStringRepr = $(this).attr('data-curvalue');
2058-
2059-
// IMPORTANT! only clear the div and render a new element if the
2060-
// value has changed
2061-
if (valStringRepr != prevValStringRepr) {
2062-
// TODO: render a transition
2054+
// always delete and re-render the stack var ...
2055+
// NB: trying to cache and compare the old value using,
2056+
// say -- $(this).attr('data-curvalue', valStringRepr) -- leads to
2057+
// a mysterious and killer memory leak that I can't figure out yet
2058+
$(this).empty();
20632059

2064-
$(this).empty(); // crude but effective for now
2060+
// make sure varname and frame.unique_hash don't contain any weird
2061+
// characters that are illegal for CSS ID's ...
2062+
var varDivID = myViz.generateID(varnameToCssID(frame.unique_hash + '__' + varname));
20652063

2064+
// need to get rid of the old connector in preparation for rendering a new one:
2065+
existingConnectionEndpointIDs.remove(varDivID);
20662066

2067-
// make sure varname and frame.unique_hash don't contain any weird
2068-
// characters that are illegal for CSS ID's ...
2069-
var varDivID = myViz.generateID(varnameToCssID(frame.unique_hash + '__' + varname));
2070-
2071-
// need to get rid of the old connector in preparation for rendering a new one:
2072-
existingConnectionEndpointIDs.remove(varDivID);
2073-
2074-
if (isPrimitiveType(val)) {
2075-
renderPrimitiveObject(val, $(this));
2076-
}
2077-
else {
2078-
// add a stub so that we can connect it with a connector later.
2079-
// IE needs this div to be NON-EMPTY in order to properly
2080-
// render jsPlumb endpoints, so that's why we add an "&nbsp;"!
2081-
$(this).append('<div class="stack_pointer" id="' + varDivID + '">&nbsp;</div>');
2082-
2083-
assert(!connectionEndpointIDs.has(varDivID));
2084-
var heapObjID = myViz.generateID('heap_object_' + getRefID(val));
2085-
connectionEndpointIDs.set(varDivID, heapObjID);
2086-
//console.log('STACK->HEAP', varDivID, heapObjID);
2087-
}
2088-
2089-
//console.log('CHANGED', frame.unique_hash, varname, prevValStringRepr, valStringRepr);
2067+
var val = frame.encoded_locals[varname];
2068+
if (isPrimitiveType(val)) {
2069+
renderPrimitiveObject(val, $(this));
2070+
}
2071+
else {
2072+
// add a stub so that we can connect it with a connector later.
2073+
// IE needs this div to be NON-EMPTY in order to properly
2074+
// render jsPlumb endpoints, so that's why we add an "&nbsp;"!
2075+
$(this).append('<div class="stack_pointer" id="' + varDivID + '">&nbsp;</div>');
2076+
2077+
assert(!connectionEndpointIDs.has(varDivID));
2078+
var heapObjID = myViz.generateID('heap_object_' + getRefID(val));
2079+
connectionEndpointIDs.set(varDivID, heapObjID);
2080+
//console.log('STACK->HEAP', varDivID, heapObjID);
20902081
}
2091-
2092-
// SUPER HACK - set current value as a hidden string attribute
2093-
$(this).attr('data-curvalue', valStringRepr);
20942082
}
20952083
});
20962084

20972085

2098-
stackVarTableCells.exit().remove();
2086+
stackVarTableCells.exit()
2087+
.each(function(d, idx) {
2088+
$(this).empty(); // crucial for garbage collecting jsPlumb connectors!
2089+
})
2090+
.remove();
20992091

21002092
stackVarTable.exit()
21012093
.each(function(d, i) {
21022094
$(this).find('.stack_pointer').each(function(i, sp) {
21032095
// detach all stack_pointer connectors for divs that are being removed
21042096
existingConnectionEndpointIDs.remove($(sp).attr('id'));
21052097
});
2098+
2099+
$(this).empty(); // crucial for garbage collecting jsPlumb connectors!
21062100
})
21072101
.remove();
21082102

@@ -2112,10 +2106,20 @@ ExecutionVisualizer.prototype.renderDataStructures = function() {
21122106
// detach all stack_pointer connectors for divs that are being removed
21132107
existingConnectionEndpointIDs.remove($(sp).attr('id'));
21142108
});
2109+
2110+
$(this).empty(); // crucial for garbage collecting jsPlumb connectors!
21152111
})
21162112
.remove();
21172113

21182114

2115+
// NB: ugh, I'm not very happy about this hack, but it seems necessary
2116+
// for embedding within sophisticated webpages such as IPython Notebook
2117+
2118+
// delete all connectors. do this AS LATE AS POSSIBLE so that
2119+
// (presumably) the calls to $(this).empty() earlier in this function
2120+
// will properly garbage collect the connectors
2121+
myViz.jsPlumbInstance.reset();
2122+
21192123
// re-render existing connectors ...
21202124
existingConnectionEndpointIDs.forEach(function(varID, valueID) {
21212125
myViz.jsPlumbInstance.connect({source: varID, target: valueID});

0 commit comments

Comments
 (0)