Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove trailing references from ManagedArray and Heap #8889

Merged
merged 3 commits into from
Jun 28, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions Source/Core/Heap.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,17 @@ Object.defineProperties(Heap.prototype, {
return this._maximumLength;
},
set: function (value) {
this._maximumLength = value;
if (this._length > value && value > 0) {
var originalLength = this._length;
if (value < originalLength) {
var array = this._array;
// Remove trailing references
for (var i = value; i < originalLength; ++i) {
array[i] = undefined;
}
this._length = value;
this._array.length = value;
array.length = value;
}
this._maximumLength = value;
},
},

Expand Down Expand Up @@ -211,6 +217,7 @@ Heap.prototype.pop = function (index) {
var root = array[index];
swap(array, index, --this._length);
this.heapify(index);
array[this._length] = undefined; // Remove trailing reference
return root;
};

Expand Down
24 changes: 18 additions & 6 deletions Source/Core/ManagedArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,17 @@ Object.defineProperties(ManagedArray.prototype, {
return this._length;
},
set: function (length) {
this._length = length;
if (length > this._array.length) {
this._array.length = length;
var array = this._array;
var originalLength = this._length;
if (length < originalLength) {
// Remove trailing references
for (var i = length; i < originalLength; ++i) {
array[i] = undefined;
}
} else if (length > array.length) {
array.length = length;
}
this._length = length;
},
},

Expand Down Expand Up @@ -74,7 +81,7 @@ ManagedArray.prototype.set = function (index, element) {
Check.typeOf.number("index", index);
//>>includeEnd('debug');

if (index >= this.length) {
if (index >= this._length) {
this.length = index + 1;
}
this._array[index] = element;
Expand Down Expand Up @@ -105,7 +112,12 @@ ManagedArray.prototype.push = function (element) {
* @returns {*} The last element in the array.
*/
ManagedArray.prototype.pop = function () {
return this._array[--this.length];
if (this._length === 0) {
return undefined;
}
var element = this._array[this._length - 1];
--this.length;
return element;
};

/**
Expand Down Expand Up @@ -142,7 +154,7 @@ ManagedArray.prototype.resize = function (length) {
* @param {Number} [length] The length.
*/
ManagedArray.prototype.trim = function (length) {
length = defaultValue(length, this.length);
length = defaultValue(length, this._length);
this._array.length = length;
};
export default ManagedArray;
37 changes: 37 additions & 0 deletions Specs/Core/HeapSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ import { Heap } from "../../Source/Cesium.js";
describe("Core/Heap", function () {
var length = 100;

function expectTrailingReferenceToBeRemoved(heap) {
var array = heap._array;
var length = heap._length;
var reservedLength = array.length;
for (var i = length; i < reservedLength; ++i) {
expect(array[i]).toBeUndefined();
}
}

function checkHeap(heap, comparator) {
var array = heap.internalArray;
var pass = true;
Expand Down Expand Up @@ -90,6 +99,34 @@ describe("Core/Heap", function () {
expect(pass).toBe(true);
});

it("pop removes trailing references", function () {
var heap = new Heap({
comparator: comparator,
});

for (var i = 0; i < 10; ++i) {
heap.insert(Math.random());
}

heap.pop();
heap.pop();

expectTrailingReferenceToBeRemoved(heap);
});

it("setting maximum length less than current length removes trailing references", function () {
var heap = new Heap({
comparator: comparator,
});

for (var i = 0; i < 10; ++i) {
heap.insert(Math.random());
}

heap.maximumLength = 5;
expectTrailingReferenceToBeRemoved(heap);
});

it("insert returns the removed element when maximumLength is set", function () {
var heap = new Heap({
comparator: comparator,
Expand Down
37 changes: 37 additions & 0 deletions Specs/Core/ManagedArraySpec.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
import { ManagedArray } from "../../Source/Cesium.js";

describe("Core/ManagedArray", function () {
function expectTrailingReferenceToBeRemoved(managedArray) {
var array = managedArray._array;
var length = managedArray._length;
var reservedLength = array.length;
for (var i = length; i < reservedLength; ++i) {
expect(array[i]).toBeUndefined();
}
}

it("constructor has expected default values", function () {
var array = new ManagedArray();
expect(array.length).toEqual(0);
Expand Down Expand Up @@ -90,6 +99,24 @@ describe("Core/ManagedArray", function () {
}
});

it("pop removes trailing references", function () {
var length = 10;
var array = new ManagedArray(length);
array.set(0, Math.random());
array.set(1, Math.random());
array.set(2, Math.random());
array.pop();
array.pop();
expectTrailingReferenceToBeRemoved(array);
});

it("pop returns undefined if array is empty", function () {
var array = new ManagedArray();
array.push(1);
expect(array.pop()).toBe(1);
expect(array.pop()).toBeUndefined();
});

it("reserve throws if length is less than 0", function () {
var array = new ManagedArray();
expect(function () {
Expand Down Expand Up @@ -130,6 +157,16 @@ describe("Core/ManagedArray", function () {
expect(array.length).toEqual(5);
});

it("resize removes trailing references", function () {
var length = 10;
var array = new ManagedArray(length);
array.set(0, Math.random());
array.set(1, Math.random());
array.set(2, Math.random());
array.resize(1);
expectTrailingReferenceToBeRemoved(array);
});

it("trim", function () {
var array = new ManagedArray(2);
array.reserve(10);
Expand Down