Skip to content

Commit

Permalink
Improve ply parsing performance (playcanvas#6882)
Browse files Browse the repository at this point in the history
  • Loading branch information
kemchenj authored and slimbuck committed Sep 5, 2024
1 parent 500475c commit 3e2ca88
Showing 1 changed file with 20 additions and 16 deletions.
36 changes: 20 additions & 16 deletions src/framework/parsers/ply.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,25 @@ const readPly = async (reader, propertyFilter = null) => {

// calculate the size of an input element record
const inputSize = element.properties.reduce((a, p) => a + p.byteSize, 0);
const propertyParsingFunctions = element.properties.map((p) => {
/* eslint-disable brace-style */
if (p.storage) {
switch (p.type) {
case 'char': return (streamBuf, c) => { p.storage[c] = streamBuf.getInt8(); };
case 'uchar': return (streamBuf, c) => { p.storage[c] = streamBuf.getUint8(); };
case 'short': return (streamBuf, c) => { p.storage[c] = streamBuf.getInt16(); };
case 'ushort': return (streamBuf, c) => { p.storage[c] = streamBuf.getUint16(); };
case 'int': return (streamBuf, c) => { p.storage[c] = streamBuf.getInt32(); };
case 'uint': return (streamBuf, c) => { p.storage[c] = streamBuf.getUint32(); };
case 'float': return (streamBuf, c) => { p.storage[c] = streamBuf.getFloat32(); };
case 'double': return (streamBuf, c) => { p.storage[c] = streamBuf.getFloat64(); };
default: throw new Error(`Unsupported property data type '${p.type}' in ply header`);
}
} else {
return (streamBuf) => { streamBuf.head += p.byteSize; };
}
/* eslint-enable brace-style */
});
let c = 0;

while (c < element.count) {
Expand All @@ -423,22 +442,7 @@ const readPly = async (reader, propertyFilter = null) => {

for (let n = 0; n < toRead; ++n) {
for (let j = 0; j < element.properties.length; ++j) {
const property = element.properties[j];

if (property.storage) {
switch (property.type) {
case 'char': property.storage[c] = streamBuf.getInt8(); break;
case 'uchar': property.storage[c] = streamBuf.getUint8(); break;
case 'short': property.storage[c] = streamBuf.getInt16(); break;
case 'ushort': property.storage[c] = streamBuf.getUint16(); break;
case 'int': property.storage[c] = streamBuf.getInt32(); break;
case 'uint': property.storage[c] = streamBuf.getUint32(); break;
case 'float': property.storage[c] = streamBuf.getFloat32(); break;
case 'double': property.storage[c] = streamBuf.getFloat64(); break;
}
} else {
streamBuf.head += property.byteSize;
}
propertyParsingFunctions[j](streamBuf, c);
}
c++;
}
Expand Down

0 comments on commit 3e2ca88

Please sign in to comment.