-
-
Notifications
You must be signed in to change notification settings - Fork 36.1k
flatten uuid strings #13094
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
flatten uuid strings #13094
Conversation
var _Math = {
generateUUID: ( function () {
// http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/21963136#21963136
var lut = [];
for ( var i = 0; i < 256; i ++ ) {
lut[ i ] = ( i < 16 ? '0' : '' ) + ( i ).toString( 16 ).toUpperCase();
}
return function generateUUID() {
var d0 = Math.random() * 0xffffffff | 0;
var d1 = Math.random() * 0xffffffff | 0;
var d2 = Math.random() * 0xffffffff | 0;
var d3 = Math.random() * 0xffffffff | 0;
return lut[ d0 & 0xff ] + lut[ d0 >> 8 & 0xff ] + lut[ d0 >> 16 & 0xff ] + lut[ d0 >> 24 & 0xff ] + '-' +
lut[ d1 & 0xff ] + lut[ d1 >> 8 & 0xff ] + '-' + lut[ d1 >> 16 & 0x0f | 0x40 ] + lut[ d1 >> 24 & 0xff ] + '-' +
lut[ d2 & 0x3f | 0x80 ] + lut[ d2 >> 8 & 0xff ] + '-' + lut[ d2 >> 16 & 0xff ] + lut[ d2 >> 24 & 0xff ] +
lut[ d3 & 0xff ] + lut[ d3 >> 8 & 0xff ] + lut[ d3 >> 16 & 0xff ] + lut[ d3 >> 24 & 0xff ];
};
} )(),
generateUUID2: ( function () {
// http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/21963136#21963136
var lut = [];
for ( var i = 0; i < 256; i ++ ) {
lut[ i ] = ( i < 16 ? '0' : '' ) + ( i ).toString( 16 );
}
return function generateUUID() {
var d0 = Math.random() * 0xffffffff | 0;
var d1 = Math.random() * 0xffffffff | 0;
var d2 = Math.random() * 0xffffffff | 0;
var d3 = Math.random() * 0xffffffff | 0;
return String( lut[ d0 & 0xff ] + lut[ d0 >> 8 & 0xff ] + lut[ d0 >> 16 & 0xff ] + lut[ d0 >> 24 & 0xff ] + '-' +
lut[ d1 & 0xff ] + lut[ d1 >> 8 & 0xff ] + '-' + lut[ d1 >> 16 & 0x0f | 0x40 ] + lut[ d1 >> 24 & 0xff ] + '-' +
lut[ d2 & 0x3f | 0x80 ] + lut[ d2 >> 8 & 0xff ] + '-' + lut[ d2 >> 16 & 0xff ] + lut[ d2 >> 24 & 0xff ] +
lut[ d3 & 0xff ] + lut[ d3 >> 8 & 0xff ] + lut[ d3 >> 16 & 0xff ] + lut[ d3 >> 24 & 0xff ] ).toUpperCase();
};
} )()
};
function run( func ) {
var loop = 0x100000;
var startTime = performance.now();
for ( var i = 0; i < loop; i ++ ) {
func();
}
var endTime = performance.now();
console.log( ( endTime - startTime ).toFixed( 2 ) + 'ms, loop=' + loop );
}
run(_Math.generateUUID);
run(_Math.generateUUID);
run(_Math.generateUUID2);
run(_Math.generateUUID2); |
|
Goes from ~350ms to ~740ms here... 😕 |
|
why does 3js use uuids at all? can't it just use counters? |
Mainly for serialising/deserialising. |
ok, and why can't you do geometries[123] instead of geometries['XXX-YYY-SOMETHING'] while deserialising? |
|
I'm wondering if uppercasing the uuid is worth it though. As far as I understand, uppercase uuid consumes 10x more memory than lowercase? |
Lets not derail this thread. |
|
in the context of #13069 .toLowerCase() would probably have the same effect |
|
the performance hit is why i looked at removing the requirement for the uuid's with the lazy_uuid PR. The WeakMap.get( object ) is probably a simpler lookup just on the object itself rather than having to get an object property as the uuid lookup in WebGLProperties & Attributes so that is probably a gain too. @mrdoob the case of the uuid doesn't matter. It just gets the VM to create a more compact copy as a side effect - hence the hacky nature. |
|
How about keeping the ( function () {
// http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/21963136#21963136
var lut = [];
for ( var i = 0; i < 256; i ++ ) {
lut[ i ] = ( i < 16 ? '0' : '' ) + ( i ).toString( 16 ).toUpperCase();
}
return function generateUUID() {
var d0 = Math.random() * 0xffffffff | 0;
var d1 = Math.random() * 0xffffffff | 0;
var d2 = Math.random() * 0xffffffff | 0;
var d3 = Math.random() * 0xffffffff | 0;
return String( lut[ d0 & 0xff ] + lut[ d0 >> 8 & 0xff ] + lut[ d0 >> 16 & 0xff ] + lut[ d0 >> 24 & 0xff ] + '-' +
lut[ d1 & 0xff ] + lut[ d1 >> 8 & 0xff ] + '-' + lut[ d1 >> 16 & 0x0f | 0x40 ] + lut[ d1 >> 24 & 0xff ] + '-' +
lut[ d2 & 0x3f | 0x80 ] + lut[ d2 >> 8 & 0xff ] + '-' + lut[ d2 >> 16 & 0xff ] + lut[ d2 >> 24 & 0xff ] +
lut[ d3 & 0xff ] + lut[ d3 >> 8 & 0xff ] + lut[ d3 >> 16 & 0xff ] + lut[ d3 >> 24 & 0xff ] );
};
} )()No performance changes with that code here. Does this solve the memory issue? |
|
For testing... var _Math = {
generateUUID: ( function () {
// http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/21963136#21963136
var lut = [];
for ( var i = 0; i < 256; i ++ ) {
lut[ i ] = ( i < 16 ? '0' : '' ) + ( i ).toString( 16 ).toUpperCase();
}
return function generateUUID() {
var d0 = Math.random() * 0xffffffff | 0;
var d1 = Math.random() * 0xffffffff | 0;
var d2 = Math.random() * 0xffffffff | 0;
var d3 = Math.random() * 0xffffffff | 0;
return lut[ d0 & 0xff ] + lut[ d0 >> 8 & 0xff ] + lut[ d0 >> 16 & 0xff ] + lut[ d0 >> 24 & 0xff ] + '-' +
lut[ d1 & 0xff ] + lut[ d1 >> 8 & 0xff ] + '-' + lut[ d1 >> 16 & 0x0f | 0x40 ] + lut[ d1 >> 24 & 0xff ] + '-' +
lut[ d2 & 0x3f | 0x80 ] + lut[ d2 >> 8 & 0xff ] + '-' + lut[ d2 >> 16 & 0xff ] + lut[ d2 >> 24 & 0xff ] +
lut[ d3 & 0xff ] + lut[ d3 >> 8 & 0xff ] + lut[ d3 >> 16 & 0xff ] + lut[ d3 >> 24 & 0xff ];
};
} )(),
generateUUID2: ( function () {
// http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/21963136#21963136
var lut = [];
for ( var i = 0; i < 256; i ++ ) {
lut[ i ] = ( i < 16 ? '0' : '' ) + ( i ).toString( 16 ).toUpperCase();
}
return function generateUUID() {
var d0 = Math.random() * 0xffffffff | 0;
var d1 = Math.random() * 0xffffffff | 0;
var d2 = Math.random() * 0xffffffff | 0;
var d3 = Math.random() * 0xffffffff | 0;
return String(
lut[ d0 & 0xff ] + lut[ d0 >> 8 & 0xff ] + lut[ d0 >> 16 & 0xff ] + lut[ d0 >> 24 & 0xff ] + '-' +
lut[ d1 & 0xff ] + lut[ d1 >> 8 & 0xff ] + '-' + lut[ d1 >> 16 & 0x0f | 0x40 ] + lut[ d1 >> 24 & 0xff ] + '-' +
lut[ d2 & 0x3f | 0x80 ] + lut[ d2 >> 8 & 0xff ] + '-' + lut[ d2 >> 16 & 0xff ] + lut[ d2 >> 24 & 0xff ] +
lut[ d3 & 0xff ] + lut[ d3 >> 8 & 0xff ] + lut[ d3 >> 16 & 0xff ] + lut[ d3 >> 24 & 0xff ]
);
};
} )()
};
function run( func ) {
var loop = 0x100000;
var startTime = performance.now();
for ( var i = 0; i < loop; i ++ ) {
func();
}
var endTime = performance.now();
console.log( ( endTime - startTime ).toFixed( 2 ) + 'ms, loop=' + loop );
}
run(_Math.generateUUID);
run(_Math.generateUUID);
run(_Math.generateUUID2);
run(_Math.generateUUID2); |
|
out of interest just tried to do this with Uint8Array and TextDecoder, and it's only 15 times slower :D then I thought if I inline setHex, I can gain something, but profiler says -23% max compared to this, toUpperCase x2 perfrmance drop does not look that bad at all. |
|
BTW, So, this uuid memory consumption issue is new to Three.js(>=r88). |
|
If UUIDs are not needed, I would recommend using an approach similar to https://github.com/mcollina/hyperid/blob/master/hyperid.js: generate something unique and then concatenate with a counter. |
|
Number tech is interesting... I tried four patterns with the following code on my Windows10 + Chrome.
I expected 3.'s and 4.'s result are same as the 2.'s result but seems like a lot of concatenated strings are left in heap. |
|
I was half expecting this to be a recent issue, since I hadn't seen the problem before when looking at heap size in general. I would check what you get out from Number( uuid ). console.log( Number ( new THREE.Object3D().uuid ) ); produces NaN. |
|
Probably what |
|
@takahirox and from 42 vs 38 mb it does not seem it produces anything at all |
|
3 works now by replacing Probably we need to let Bad news, |
|
Would you reopen this because of #13069 (comment)? |
| var d3 = Math.random() * 0xffffffff | 0; | ||
| return lut[ d0 & 0xff ] + lut[ d0 >> 8 & 0xff ] + lut[ d0 >> 16 & 0xff ] + lut[ d0 >> 24 & 0xff ] + '-' + | ||
|
|
||
| return String( lut[ d0 & 0xff ] + lut[ d0 >> 8 & 0xff ] + lut[ d0 >> 16 & 0xff ] + lut[ d0 >> 24 & 0xff ] + '-' + |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer removing "String", not strong preference tho.
|
I can't reopen this PR because @aardgoose deleted the branch. |
|
Oops, then I make a new one. |

Workaround for #13069 issues