Skip to content

Commit

Permalink
minor struct array cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mourner committed Oct 19, 2016
1 parent b9b6c63 commit 2230579
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions js/util/struct_array.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ class StructArray {

/**
* Serialize this StructArray instance
* @private
*/
serialize() {
this.trim();
Expand All @@ -90,7 +89,6 @@ class StructArray {

/**
* Return the Struct at the given location in the array.
* @private
* @param {number} index The index of the element.
*/
get(index) {
Expand All @@ -99,7 +97,6 @@ class StructArray {

/**
* Resize the array to discard unused capacity.
* @private
*/
trim() {
if (this.length !== this.capacity) {
Expand Down Expand Up @@ -129,7 +126,6 @@ class StructArray {

/**
* Create TypedArray views for the current ArrayBuffer.
* @private
*/
_refreshViews() {
for (const type of this._usedTypes) {
Expand All @@ -141,7 +137,6 @@ class StructArray {
* Output the `StructArray` between indices `startIndex` and `endIndex` as an array of `StructTypes` to enable sorting
* @param {number} startIndex
* @param {number} endIndex
* @private
*/
toArray(startIndex, endIndex) {
const array = [];
Expand Down Expand Up @@ -196,6 +191,7 @@ const structArrayTypeCache = {};
function StructArrayType(options) {

const key = JSON.stringify(options);

if (structArrayTypeCache[key]) {
return structArrayTypeCache[key];
}
Expand All @@ -214,26 +210,30 @@ function StructArrayType(options) {

const typeSize = sizeOf(member.type);
const memberOffset = offset = align(offset, Math.max(options.alignment, typeSize));
const components = member.components || 1;

maxSize = Math.max(maxSize, typeSize);
offset += typeSize * (member.components || 1);
offset += typeSize * components;

return {
name: member.name,
type: member.type,
components: member.components || 1,
components: components,
offset: memberOffset
};
});

const size = align(offset, Math.max(maxSize, options.alignment));

class StructType extends Struct {}

StructType.prototype.alignment = options.alignment;
StructType.prototype.size = align(offset, Math.max(maxSize, options.alignment));
StructType.prototype.size = size;

for (const member of members) {
for (let c = 0; c < member.components; c++) {
Object.defineProperty(StructType.prototype, member.name + (member.components === 1 ? '' : c), {
const name = member.name + (member.components === 1 ? '' : c);
Object.defineProperty(StructType.prototype, name, {
get: createGetter(member, c),
set: createSetter(member, c)
});
Expand All @@ -244,8 +244,8 @@ function StructArrayType(options) {

StructArrayType.prototype.members = members;
StructArrayType.prototype.StructType = StructType;
StructArrayType.prototype.bytesPerElement = StructType.prototype.size;
StructArrayType.prototype.emplaceBack = createEmplaceBack(members, StructType.prototype.size);
StructArrayType.prototype.bytesPerElement = size;
StructArrayType.prototype.emplaceBack = createEmplaceBack(members, size);
StructArrayType.prototype._usedTypes = usedTypes;

structArrayTypeCache[key] = StructArrayType;
Expand All @@ -265,7 +265,6 @@ function getArrayViewName(type) {
return type.toLowerCase();
}


/*
* > I saw major perf gains by shortening the source of these generated methods (i.e. renaming
* > elementIndex to i) (likely due to v8 inlining heuristics).
Expand Down Expand Up @@ -312,7 +311,6 @@ function createMemberComponentString(member, component) {
const componentOffset = (member.offset / sizeOf(member.type) + component).toFixed(0);
const index = `${elementOffset} + ${componentOffset}`;
return `this._structArray.${getArrayViewName(member.type)}[${index}]`;

}

function createGetter(member, c) {
Expand Down

0 comments on commit 2230579

Please sign in to comment.