Reduce memory taken up by attribute objects.

This commit is contained in:
Tom MacWright
2012-10-26 17:55:58 -04:00
parent a4fa3cd580
commit 0c9d46bee3
5 changed files with 32 additions and 39 deletions
+1 -1
View File
@@ -9,7 +9,7 @@
</head>
<body>
<script type="text/javascript" src="js/lib/underscore-min.js"></script>
<script type="text/javascript" src="js/lib/d3.v2.min.js"></script>
<script type="text/javascript" src="js/lib/d3.v2.js"></script>
<script type="text/javascript" src="js/iD/actions/UndoStack.js"></script>
<script type="text/javascript" src="js/iD/Util.js"></script>
+14 -23
View File
@@ -40,10 +40,12 @@ iD.Connection = function(apiURL) {
function getOrCreate(id, type) {
// summary: Return an entity if it exists: if not, create an empty one with the given id, and return that.
if (type === 'node') {
if (!entities[id]) assign(new iD.Node(connection, id, NaN, NaN, {}, false));
if (!entities[id]) assign(new iD.Node(id, NaN, NaN, {}, false));
return entities[id];
} else if (type === 'way') {
if (!entities[id]) assign(new iD.Way(connection, id, [], {}, false));
if (!entities[id]) {
assign(new iD.Way(connection, id, [], {}, false));
}
return entities[id];
} else if (type === 'relation') {
if (!relations[id]) assign(new iD.Relation(connection, id, [], {}, false));
@@ -53,7 +55,7 @@ iD.Connection = function(apiURL) {
function doCreateNode(tags, lat, lon, perform) {
// summary: Create a new node and save it in the data store, using an undo stack.
var node = new iD.Node(connection, nextNode--, lat, lon, tags, true);
var node = new iD.Node(nextNode--, lat, lon, tags, true);
perform(new iD.actions.CreateEntityAction(node, assign));
return node; // iD.Node
}
@@ -72,11 +74,11 @@ iD.Connection = function(apiURL) {
return relation;
}
function getObjectsByBbox(extent) {
function intersects(extent) {
// summary: Find all drawable entities that are within a given bounding box.
// Each one is an array of entities.
return d3.values(entities).filter(function(e, id) {
return e.within(extent);
return e.intersects(extent);
});
}
@@ -94,14 +96,6 @@ iD.Connection = function(apiURL) {
return function(item) { return item.nodeName === n; };
}
function attributeObject(obj) {
var o = {};
for (var i = 0; i < obj.attributes.length; i++) {
o[obj.attributes[i].nodeName] = obj.attributes[i].nodeValue;
}
return o;
}
function getAttribute(obj, name) {
return obj.attributes[name].nodeValue;
}
@@ -155,24 +149,21 @@ iD.Connection = function(apiURL) {
for (var i = 0; i < dom.childNodes[0].childNodes.length; i++) {
var obj = dom.childNodes[0].childNodes[i], attrib;
if (obj.nodeName === 'node') {
attrib = attributeObject(obj);
var node = new iD.Node(connection,
+attrib.id,
+attrib.lat,
+attrib.lon,
var node = new iD.Node(
+getAttribute(obj, 'id'),
+getAttribute(obj, 'lat'),
+getAttribute(obj, 'lon'),
getTags(obj));
assign(node);
} else if (obj.nodeName === 'way') {
attrib = attributeObject(obj);
var way = new iD.Way(connection,
attrib.id,
getAttribute(obj, 'id'),
getNodes(obj, connection),
getTags(obj));
assign(way);
} else if (obj.nodeName === 'relation') {
attrib = attributeObject(obj);
var relation = new iD.Relation(connection,
attrib.id,
getAttribute(obj, 'id'),
getMembers(obj, connection),
getTags(obj));
assign(relation);
@@ -187,7 +178,7 @@ iD.Connection = function(apiURL) {
connection.relations = relations;
connection.loadFromAPI = loadFromAPI;
connection.loadFromURL = loadFromURL;
connection.getObjectsByBbox = getObjectsByBbox;
connection.intersects = intersects;
connection.doCreateNode = doCreateNode;
connection.doCreateWay = doCreateWay;
connection.doCreateRelation = doCreateRelation;
+2 -3
View File
@@ -1,10 +1,9 @@
if (typeof iD === 'undefined') iD = {};
// [Node](http://wiki.openstreetmap.org/wiki/Node)
iD.Node = function(connection, id, lat, lon, tags, loaded) {
iD.Node = function(id, lat, lon, tags, loaded) {
// summary: An OSM node.
this.entityType = 'node';
this.connection = connection;
this.id = id;
this._id = iD.Util.id();
this.entity = iD.Entity();
@@ -30,7 +29,7 @@ iD.Node.prototype = {
};
},
within: function(extent) {
intersects: function(extent) {
return (this.lon >= extent[0][0]) &&
(this.lon <= extent[1][0]) &&
(this.lat <= extent[0][1]) &&
+14 -11
View File
@@ -65,23 +65,26 @@ iD.Way.prototype = {
},
bounds: function() {
// TODO: cache
return d3.geo.bounds(this.toGeoJSON());
},
// ---------------------
// Bounding-box handling
within: function(left,right,top,bottom) {
intersects: function(extent) {
// No-node ways are inside of nothing.
if (!this.nodes.length) return false;
var bounds = this.bounds();
// TODO invert and just return
if (!this.extent.west ||
(this.extent.west < left && this.extent.east < left ) ||
(this.extent.west > right && this.extent.east > right ) ||
(this.extent.south < bottom && this.extent.north < bottom) ||
(this.extent.south > top && this.extent.south > top)) {
return false;
} else {
return true;
}
// left
return !(
// the bottom right is to the top-left
// of the top-left
bounds[1][0] < extent[0][0] &&
bounds[1][1] < extent[0][1] ||
// The top left is to the bottom-right
// of the top-left
bounds[0][0] > extent[1][0] &&
bounds[0][1] > extent[1][1]);
},
// --------------
+1 -1
View File
@@ -207,7 +207,7 @@ iD.Map = function(obj) {
}
function drawVector() {
var all = connection.all();
var all = connection.intersects(extent());
var ways = all.filter(function(a) {
return a.entityType === 'way' && !a.isClosed();