Skip to content

Commit

Permalink
JS VMs struggle to recognize when the condition part of a for loop is…
Browse files Browse the repository at this point in the history
… constant, especially w.r.t. GLctx.getProgramParameter(), so make sure that no repeated computation is done in such paths. (#20439)
  • Loading branch information
juj authored Aug 29, 2024
1 parent 9445d6a commit aec91e5
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 44 deletions.
53 changes: 33 additions & 20 deletions src/library_webgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -2182,7 +2182,8 @@ for (/**@suppress{duplicate}*/var i = 0; i <= {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
// maps integer locations back to uniform name strings, so that we can lazily fetch uniform array locations
program.uniformArrayNamesById = {};

for (i = 0; i < GLctx.getProgramParameter(program, 0x8B86/*GL_ACTIVE_UNIFORMS*/); ++i) {
var numActiveUniforms = GLctx.getProgramParameter(program, 0x8B86/*GL_ACTIVE_UNIFORMS*/);
for (i = 0; i < numActiveUniforms; ++i) {
var u = GLctx.getActiveUniform(program, i);
var nm = u.name;
var sz = u.size;
Expand Down Expand Up @@ -2499,8 +2500,9 @@ for (/**@suppress{duplicate}*/var i = 0; i <= {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
#if GL_POOL_TEMP_BUFFERS
if (count <= {{{ GL_POOL_TEMP_BUFFERS_SIZE / 2 }}}) {
// avoid allocation when uploading few enough uniforms
var view = miniTempWebGLIntBuffers[2*count];
for (var i = 0; i < 2*count; i += 2) {
count *= 2;
var view = miniTempWebGLIntBuffers[count];
for (var i = 0; i < count; i += 2) {
view[i] = {{{ makeGetValue('value', '4*i', 'i32') }}};
view[i+1] = {{{ makeGetValue('value', '4*i+4', 'i32') }}};
}
Expand Down Expand Up @@ -2541,8 +2543,9 @@ for (/**@suppress{duplicate}*/var i = 0; i <= {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
#if GL_POOL_TEMP_BUFFERS
if (count <= {{{ GL_POOL_TEMP_BUFFERS_SIZE / 3 }}}) {
// avoid allocation when uploading few enough uniforms
var view = miniTempWebGLIntBuffers[3*count];
for (var i = 0; i < 3*count; i += 3) {
count *= 3;
var view = miniTempWebGLIntBuffers[count];
for (var i = 0; i < count; i += 3) {
view[i] = {{{ makeGetValue('value', '4*i', 'i32') }}};
view[i+1] = {{{ makeGetValue('value', '4*i+4', 'i32') }}};
view[i+2] = {{{ makeGetValue('value', '4*i+8', 'i32') }}};
Expand Down Expand Up @@ -2584,8 +2587,9 @@ for (/**@suppress{duplicate}*/var i = 0; i <= {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
#if GL_POOL_TEMP_BUFFERS
if (count <= {{{ GL_POOL_TEMP_BUFFERS_SIZE / 4 }}}) {
// avoid allocation when uploading few enough uniforms
var view = miniTempWebGLIntBuffers[4*count];
for (var i = 0; i < 4*count; i += 4) {
count *= 4;
var view = miniTempWebGLIntBuffers[count];
for (var i = 0; i < count; i += 4) {
view[i] = {{{ makeGetValue('value', '4*i', 'i32') }}};
view[i+1] = {{{ makeGetValue('value', '4*i+4', 'i32') }}};
view[i+2] = {{{ makeGetValue('value', '4*i+8', 'i32') }}};
Expand Down Expand Up @@ -2669,8 +2673,9 @@ for (/**@suppress{duplicate}*/var i = 0; i <= {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
#if GL_POOL_TEMP_BUFFERS
if (count <= {{{ GL_POOL_TEMP_BUFFERS_SIZE / 2 }}}) {
// avoid allocation when uploading few enough uniforms
var view = miniTempWebGLFloatBuffers[2*count];
for (var i = 0; i < 2*count; i += 2) {
count *= 2;
var view = miniTempWebGLFloatBuffers[count];
for (var i = 0; i < count; i += 2) {
view[i] = {{{ makeGetValue('value', '4*i', 'float') }}};
view[i+1] = {{{ makeGetValue('value', '4*i+4', 'float') }}};
}
Expand Down Expand Up @@ -2711,8 +2716,9 @@ for (/**@suppress{duplicate}*/var i = 0; i <= {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
#if GL_POOL_TEMP_BUFFERS
if (count <= {{{ GL_POOL_TEMP_BUFFERS_SIZE / 3 }}}) {
// avoid allocation when uploading few enough uniforms
var view = miniTempWebGLFloatBuffers[3*count];
for (var i = 0; i < 3*count; i += 3) {
count *= 3;
var view = miniTempWebGLFloatBuffers[count];
for (var i = 0; i < count; i += 3) {
view[i] = {{{ makeGetValue('value', '4*i', 'float') }}};
view[i+1] = {{{ makeGetValue('value', '4*i+4', 'float') }}};
view[i+2] = {{{ makeGetValue('value', '4*i+8', 'float') }}};
Expand Down Expand Up @@ -2758,7 +2764,8 @@ for (/**@suppress{duplicate}*/var i = 0; i <= {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
// hoist the heap out of the loop for size and for pthreads+growth.
var heap = HEAPF32;
value = {{{ getHeapOffset('value', 'float') }}};
for (var i = 0; i < 4 * count; i += 4) {
count *= 4;
for (var i = 0; i < count; i += 4) {
var dst = value + i;
view[i] = heap[dst];
view[i + 1] = heap[dst + 1];
Expand Down Expand Up @@ -2802,8 +2809,9 @@ for (/**@suppress{duplicate}*/var i = 0; i <= {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
#if GL_POOL_TEMP_BUFFERS
if (count <= {{{ GL_POOL_TEMP_BUFFERS_SIZE / 4 }}}) {
// avoid allocation when uploading few enough uniforms
var view = miniTempWebGLFloatBuffers[4*count];
for (var i = 0; i < 4*count; i += 4) {
count *= 4;
var view = miniTempWebGLFloatBuffers[count];
for (var i = 0; i < count; i += 4) {
view[i] = {{{ makeGetValue('value', '4*i', 'float') }}};
view[i+1] = {{{ makeGetValue('value', '4*i+4', 'float') }}};
view[i+2] = {{{ makeGetValue('value', '4*i+8', 'float') }}};
Expand Down Expand Up @@ -2846,8 +2854,9 @@ for (/**@suppress{duplicate}*/var i = 0; i <= {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
#if GL_POOL_TEMP_BUFFERS
if (count <= {{{ GL_POOL_TEMP_BUFFERS_SIZE / 9 }}}) {
// avoid allocation when uploading few enough uniforms
var view = miniTempWebGLFloatBuffers[9*count];
for (var i = 0; i < 9*count; i += 9) {
count *= 9;
var view = miniTempWebGLFloatBuffers[count];
for (var i = 0; i < count; i += 9) {
view[i] = {{{ makeGetValue('value', '4*i', 'float') }}};
view[i+1] = {{{ makeGetValue('value', '4*i+4', 'float') }}};
view[i+2] = {{{ makeGetValue('value', '4*i+8', 'float') }}};
Expand Down Expand Up @@ -2899,7 +2908,8 @@ for (/**@suppress{duplicate}*/var i = 0; i <= {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
// hoist the heap out of the loop for size and for pthreads+growth.
var heap = HEAPF32;
value = {{{ getHeapOffset('value', 'float') }}};
for (var i = 0; i < 16 * count; i += 16) {
count *= 16;
for (var i = 0; i < count; i += 16) {
var dst = value + i;
view[i] = heap[dst];
view[i + 1] = heap[dst + 1];
Expand Down Expand Up @@ -3351,21 +3361,24 @@ for (/**@suppress{duplicate}*/var i = 0; i <= {{{ GL_POOL_TEMP_BUFFERS_SIZE }}};
{{{ makeSetValue('p', '0', 'log.length + 1', 'i32') }}};
} else if (pname == 0x8B87 /* GL_ACTIVE_UNIFORM_MAX_LENGTH */) {
if (!program.maxUniformLength) {
for (var i = 0; i < GLctx.getProgramParameter(program, 0x8B86/*GL_ACTIVE_UNIFORMS*/); ++i) {
var numActiveUniforms = GLctx.getProgramParameter(program, 0x8B86/*GL_ACTIVE_UNIFORMS*/);
for (var i = 0; i < numActiveUniforms; ++i) {
program.maxUniformLength = Math.max(program.maxUniformLength, GLctx.getActiveUniform(program, i).name.length+1);
}
}
{{{ makeSetValue('p', '0', 'program.maxUniformLength', 'i32') }}};
} else if (pname == 0x8B8A /* GL_ACTIVE_ATTRIBUTE_MAX_LENGTH */) {
if (!program.maxAttributeLength) {
for (var i = 0; i < GLctx.getProgramParameter(program, 0x8B89/*GL_ACTIVE_ATTRIBUTES*/); ++i) {
var numActiveAttributes = GLctx.getProgramParameter(program, 0x8B89/*GL_ACTIVE_ATTRIBUTES*/);
for (var i = 0; i < numActiveAttributes; ++i) {
program.maxAttributeLength = Math.max(program.maxAttributeLength, GLctx.getActiveAttrib(program, i).name.length+1);
}
}
{{{ makeSetValue('p', '0', 'program.maxAttributeLength', 'i32') }}};
} else if (pname == 0x8A35 /* GL_ACTIVE_UNIFORM_BLOCK_MAX_NAME_LENGTH */) {
if (!program.maxUniformBlockNameLength) {
for (var i = 0; i < GLctx.getProgramParameter(program, 0x8A36/*GL_ACTIVE_UNIFORM_BLOCKS*/); ++i) {
var numActiveUniformBlocks = GLctx.getProgramParameter(program, 0x8A36/*GL_ACTIVE_UNIFORM_BLOCKS*/);
for (var i = 0; i < numActiveUniformBlocks; ++i) {
program.maxUniformBlockNameLength = Math.max(program.maxUniformBlockNameLength, GLctx.getActiveUniformBlockName(program, i).length+1);
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/code_size/embind_hello_wasm.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"a.js": 9920,
"a.js.gz": 4354,
"a.wasm": 7715,
"a.wasm.gz": 3512,
"a.wasm.gz": 3508,
"total": 18187,
"total_gz": 8246
"total_gz": 8242
}
8 changes: 4 additions & 4 deletions test/code_size/hello_webgl2_wasm.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 454,
"a.html.gz": 328,
"a.js": 4521,
"a.js.gz": 2308,
"a.js": 4531,
"a.js.gz": 2312,
"a.wasm": 10399,
"a.wasm.gz": 6695,
"total": 15374,
"total_gz": 9331
"total": 15384,
"total_gz": 9335
}
8 changes: 4 additions & 4 deletions test/code_size/hello_webgl2_wasm2js.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"a.html": 346,
"a.html.gz": 262,
"a.js": 22193,
"a.js.gz": 11583,
"total": 22539,
"total_gz": 11845
"a.js": 22203,
"a.js.gz": 11588,
"total": 22549,
"total_gz": 11850
}
8 changes: 4 additions & 4 deletions test/code_size/hello_webgl_wasm.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 454,
"a.html.gz": 328,
"a.js": 4059,
"a.js.gz": 2153,
"a.js": 4069,
"a.js.gz": 2158,
"a.wasm": 10399,
"a.wasm.gz": 6695,
"total": 14912,
"total_gz": 9176
"total": 14922,
"total_gz": 9181
}
8 changes: 4 additions & 4 deletions test/code_size/hello_webgl_wasm2js.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"a.html": 346,
"a.html.gz": 262,
"a.js": 21719,
"a.js.gz": 11419,
"total": 22065,
"total_gz": 11681
"a.js": 21729,
"a.js.gz": 11423,
"total": 22075,
"total_gz": 11685
}
4 changes: 2 additions & 2 deletions test/code_size/math_wasm.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"a.js": 110,
"a.js.gz": 125,
"a.wasm": 2719,
"a.wasm.gz": 1674,
"a.wasm.gz": 1673,
"total": 3381,
"total_gz": 2179
"total_gz": 2178
}
4 changes: 2 additions & 2 deletions test/code_size/random_printf_wasm.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"a.html": 12690,
"a.html.gz": 6857,
"a.html.gz": 6855,
"total": 12690,
"total_gz": 6857
"total_gz": 6855
}
4 changes: 2 additions & 2 deletions test/code_size/random_printf_wasm2js.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"a.html": 17277,
"a.html.gz": 7489,
"a.html.gz": 7486,
"total": 17277,
"total_gz": 7489
"total_gz": 7486
}

0 comments on commit aec91e5

Please sign in to comment.