Skip to content

Commit 316e35a

Browse files
fix: #559 (line 16 of kernel-run-shortcut.js)
fix: Slight memory leak, and loss of first kernel when switching kernels fix: Feed `value.constructor` to `this.onUpdateValueMismatch()` where it was missing
1 parent 78b6e30 commit 316e35a

26 files changed

+350
-148
lines changed

dist/gpu-browser-core.js

Lines changed: 77 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*
55
* GPU Accelerated JavaScript
66
*
7-
* @version 2.4.5
8-
* @date Thu Jan 02 2020 13:02:31 GMT-0500 (Eastern Standard Time)
7+
* @version 2.4.6
8+
* @date Thu Jan 02 2020 19:46:21 GMT-0500 (Eastern Standard Time)
99
*
1010
* @license MIT
1111
* The MIT License
@@ -1406,6 +1406,10 @@ class CPUKernel extends Kernel {
14061406
return combinedKernel;
14071407
}
14081408

1409+
static getSignature(kernel, argumentTypes) {
1410+
return 'cpu' + (argumentTypes.length > 0 ? ':' + argumentTypes.join(',') : '');
1411+
}
1412+
14091413
constructor(source, settings) {
14101414
super(source, settings);
14111415
this.mergeSettings(source.settings || settings);
@@ -1518,6 +1522,7 @@ class CPUKernel extends Kernel {
15181522
} catch (e) {
15191523
console.error('An error occurred compiling the javascript: ', e);
15201524
}
1525+
this.buildSignature(arguments);
15211526
this.built = true;
15221527
}
15231528

@@ -4535,6 +4540,10 @@ class GLKernel extends Kernel {
45354540
throw new Error(`"setupFeatureChecks" not defined on ${ this.name }`);
45364541
}
45374542

4543+
static getSignature(kernel, argumentTypes) {
4544+
return kernel.getVariablePrecisionString() + (argumentTypes.length > 0 ? ':' + argumentTypes.join(',') : '');
4545+
}
4546+
45384547
setFixIntegerDivisionAccuracy(fix) {
45394548
this.fixIntegerDivisionAccuracy = fix;
45404549
return this;
@@ -6058,6 +6067,7 @@ class Kernel {
60586067
this.onIstanbulCoverageVariable = null;
60596068
this.removeIstanbulCoverage = false;
60606069
this.built = false;
6070+
this.signature = null;
60616071
}
60626072

60636073
mergeSettings(settings) {
@@ -6427,6 +6437,39 @@ class Kernel {
64276437
}
64286438
};
64296439
}
6440+
6441+
buildSignature(args) {
6442+
const Constructor = this.constructor;
6443+
this.signature = Constructor.getSignature(this, Constructor.getArgumentTypes(this, args));
6444+
}
6445+
6446+
static getArgumentTypes(kernel, args) {
6447+
const argumentTypes = new Array(args.length);
6448+
for (let i = 0; i < args.length; i++) {
6449+
const arg = args[i];
6450+
const type = kernel.argumentTypes[i];
6451+
if (arg.type) {
6452+
argumentTypes[i] = arg.type;
6453+
} else {
6454+
switch (type) {
6455+
case 'Number':
6456+
case 'Integer':
6457+
case 'Float':
6458+
case 'ArrayTexture(1)':
6459+
argumentTypes[i] = utils.getVariableType(arg);
6460+
break;
6461+
default:
6462+
argumentTypes[i] = type;
6463+
}
6464+
}
6465+
}
6466+
return argumentTypes;
6467+
}
6468+
6469+
static getSignature(kernel, argumentTypes) {
6470+
throw new Error(`"getSignature" not implemented on ${ this.name }`);
6471+
return argumentTypes.length > 0 ? ':' + argumentTypes.join(',') : '';
6472+
}
64306473
}
64316474

64326475
module.exports = {
@@ -9102,7 +9145,7 @@ class WebGLKernelValueSingleArray extends WebGLKernelValue {
91029145

91039146
updateValue(value) {
91049147
if (value.constructor !== this.initialValueConstructor) {
9105-
this.onUpdateValueMismatch();
9148+
this.onUpdateValueMismatch(value.constructor);
91069149
return;
91079150
}
91089151
const { context: gl } = this;
@@ -9333,7 +9376,7 @@ class WebGLKernelValueSingleArray3DI extends WebGLKernelValue {
93339376

93349377
updateValue(value) {
93359378
if (value.constructor !== this.initialValueConstructor) {
9336-
this.onUpdateValueMismatch();
9379+
this.onUpdateValueMismatch(value.constructor);
93379380
return;
93389381
}
93399382
const { context: gl } = this;
@@ -9416,7 +9459,7 @@ class WebGLKernelValueSingleInput extends WebGLKernelValue {
94169459

94179460
updateValue(input) {
94189461
if (input.constructor !== this.initialValueConstructor) {
9419-
this.onUpdateValueMismatch();
9462+
this.onUpdateValueMismatch(input.constructor);
94209463
return;
94219464
}
94229465
const { context: gl } = this;
@@ -9471,7 +9514,7 @@ class WebGLKernelValueUnsignedArray extends WebGLKernelValue {
94719514

94729515
updateValue(value) {
94739516
if (value.constructor !== this.initialValueConstructor) {
9474-
this.onUpdateValueMismatch();
9517+
this.onUpdateValueMismatch(value.constructor);
94759518
return;
94769519
}
94779520
const { context: gl } = this;
@@ -9527,7 +9570,7 @@ class WebGLKernelValueUnsignedInput extends WebGLKernelValue {
95279570

95289571
updateValue(input) {
95299572
if (input.constructor !== this.initialValueConstructor) {
9530-
this.onUpdateValueMismatch();
9573+
this.onUpdateValueMismatch(value.constructor);
95319574
return;
95329575
}
95339576
const { context: gl } = this;
@@ -10064,6 +10107,7 @@ class WebGLKernel extends GLKernel {
1006410107
) {
1006510108
this._setupSubOutputTextures();
1006610109
}
10110+
this.buildSignature(arguments);
1006710111
this.built = true;
1006810112
}
1006910113

@@ -12004,7 +12048,7 @@ class WebGL2KernelValueSingleArray extends WebGLKernelValueSingleArray {
1200412048

1200512049
updateValue(value) {
1200612050
if (value.constructor !== this.initialValueConstructor) {
12007-
this.onUpdateValueMismatch();
12051+
this.onUpdateValueMismatch(value.constructor);
1200812052
return;
1200912053
}
1201012054
const { context: gl } = this;
@@ -12030,7 +12074,7 @@ const { WebGLKernelValueSingleArray1DI } = require('../../web-gl/kernel-value/si
1203012074
class WebGL2KernelValueSingleArray1DI extends WebGLKernelValueSingleArray1DI {
1203112075
updateValue(value) {
1203212076
if (value.constructor !== this.initialValueConstructor) {
12033-
this.onUpdateValueMismatch();
12077+
this.onUpdateValueMismatch(value.constructor);
1203412078
return;
1203512079
}
1203612080
const { context: gl } = this;
@@ -12064,7 +12108,7 @@ const { WebGLKernelValueSingleArray2DI } = require('../../web-gl/kernel-value/si
1206412108
class WebGL2KernelValueSingleArray2DI extends WebGLKernelValueSingleArray2DI {
1206512109
updateValue(value) {
1206612110
if (value.constructor !== this.initialValueConstructor) {
12067-
this.onUpdateValueMismatch();
12111+
this.onUpdateValueMismatch(value.constructor);
1206812112
return;
1206912113
}
1207012114
const { context: gl } = this;
@@ -12098,7 +12142,7 @@ const { WebGLKernelValueSingleArray3DI } = require('../../web-gl/kernel-value/si
1209812142
class WebGL2KernelValueSingleArray3DI extends WebGLKernelValueSingleArray3DI {
1209912143
updateValue(value) {
1210012144
if (value.constructor !== this.initialValueConstructor) {
12101-
this.onUpdateValueMismatch();
12145+
this.onUpdateValueMismatch(value.constructor);
1210212146
return;
1210312147
}
1210412148
const { context: gl } = this;
@@ -12795,6 +12839,7 @@ module.exports = lib;
1279512839
},{"./index":107}],106:[function(require,module,exports){
1279612840
const { gpuMock } = require('gpu-mock.js');
1279712841
const { utils } = require('./utils');
12842+
const { Kernel } = require('./backend/kernel');
1279812843
const { CPUKernel } = require('./backend/cpu/kernel');
1279912844
const { HeadlessGLKernel } = require('./backend/headless-gl/kernel');
1280012845
const { WebGL2Kernel } = require('./backend/web-gl2/kernel');
@@ -12998,6 +13043,9 @@ class GPU {
1299813043
console.warn('Switching kernels');
1299913044
}
1300013045
let newOutput = null;
13046+
if (kernel.signature && !switchableKernels[kernel.signature]) {
13047+
switchableKernels[kernel.signature] = kernel;
13048+
}
1300113049
if (kernel.dynamicOutput) {
1300213050
for (let i = reasons.length - 1; i >= 0; i--) {
1300313051
const reason = reasons[i];
@@ -13006,32 +13054,16 @@ class GPU {
1300613054
}
1300713055
}
1300813056
}
13009-
const argumentTypes = new Array(args.length);
13010-
for (let i = 0; i < args.length; i++) {
13011-
const arg = args[i];
13012-
const type = kernel.argumentTypes[i];
13013-
if (arg.type) {
13014-
argumentTypes[i] = arg.type;
13015-
} else {
13016-
switch (type) {
13017-
case 'Number':
13018-
case 'Integer':
13019-
case 'Float':
13020-
case 'ArrayTexture(1)':
13021-
argumentTypes[i] = utils.getVariableType(arg);
13022-
break;
13023-
default:
13024-
argumentTypes[i] = type;
13025-
}
13026-
}
13027-
}
13028-
const signature = kernel.getVariablePrecisionString() + (argumentTypes.length > 0 ? ':' + argumentTypes.join(',') : '');
13057+
13058+
const Constructor = kernel.constructor;
13059+
const argumentTypes = Constructor.getArgumentTypes(kernel, args);
13060+
const signature = Constructor.getSignature(kernel, argumentTypes);
1302913061
const existingKernel = switchableKernels[signature];
1303013062
if (existingKernel) {
1303113063
return existingKernel;
1303213064
}
1303313065

13034-
const newKernel = switchableKernels[signature] = new kernel.constructor(source, {
13066+
const newKernel = switchableKernels[signature] = new Constructor(source, {
1303513067
argumentTypes,
1303613068
constantTypes: kernel.constantTypes,
1303713069
graphical: kernel.graphical,
@@ -13258,7 +13290,7 @@ module.exports = {
1325813290
kernelOrder,
1325913291
kernelTypes
1326013292
};
13261-
},{"./backend/cpu/kernel":7,"./backend/headless-gl/kernel":33,"./backend/web-gl/kernel":68,"./backend/web-gl2/kernel":103,"./kernel-run-shortcut":109,"./utils":112,"gpu-mock.js":3}],107:[function(require,module,exports){
13293+
},{"./backend/cpu/kernel":7,"./backend/headless-gl/kernel":33,"./backend/kernel":35,"./backend/web-gl/kernel":68,"./backend/web-gl2/kernel":103,"./kernel-run-shortcut":109,"./utils":112,"gpu-mock.js":3}],107:[function(require,module,exports){
1326213294
const { GPU } = require('./gpu');
1326313295
const { alias } = require('./alias');
1326413296
const { utils } = require('./utils');
@@ -13382,6 +13414,7 @@ function kernelRunShortcut(kernel) {
1338213414
if (kernel.switchingKernels) {
1338313415
const reasons = kernel.resetSwitchingKernels();
1338413416
const newKernel = kernel.onRequestSwitchKernel(reasons, arguments, kernel);
13417+
shortcut.kernel = kernel = newKernel;
1338513418
result = newKernel.run.apply(newKernel, arguments);
1338613419
}
1338713420
if (kernel.renderKernels) {
@@ -13409,43 +13442,40 @@ function kernelRunShortcut(kernel) {
1340913442
shortcut.replaceKernel = function(replacementKernel) {
1341013443
kernel = replacementKernel;
1341113444
bindKernelToShortcut(kernel, shortcut);
13412-
shortcut.kernel = kernel;
1341313445
};
1341413446

1341513447
bindKernelToShortcut(kernel, shortcut);
13416-
shortcut.kernel = kernel;
1341713448
return shortcut;
1341813449
}
1341913450

1342013451
function bindKernelToShortcut(kernel, shortcut) {
13452+
if (shortcut.kernel) {
13453+
shortcut.kernel = kernel;
13454+
return;
13455+
}
1342113456
const properties = utils.allPropertiesOf(kernel);
1342213457
for (let i = 0; i < properties.length; i++) {
1342313458
const property = properties[i];
1342413459
if (property[0] === '_' && property[1] === '_') continue;
1342513460
if (typeof kernel[property] === 'function') {
1342613461
if (property.substring(0, 3) === 'add' || property.substring(0, 3) === 'set') {
1342713462
shortcut[property] = function() {
13428-
kernel[property].apply(kernel, arguments);
13463+
shortcut.kernel[property].apply(shortcut.kernel, arguments);
1342913464
return shortcut;
1343013465
};
1343113466
} else {
13432-
if (property === 'toString') {
13433-
shortcut.toString = function() {
13434-
return kernel.toString.apply(kernel, arguments);
13435-
};
13436-
} else {
13437-
shortcut[property] = kernel[property].bind(kernel);
13438-
}
13467+
shortcut[property] = function() {
13468+
return shortcut.kernel[property].apply(shortcut.kernel, arguments);
13469+
};
1343913470
}
1344013471
} else {
13441-
shortcut.__defineGetter__(property, () => {
13442-
return kernel[property];
13443-
});
13472+
shortcut.__defineGetter__(property, () => shortcut.kernel[property]);
1344413473
shortcut.__defineSetter__(property, (value) => {
13445-
kernel[property] = value;
13474+
shortcut.kernel[property] = value;
1344613475
});
1344713476
}
1344813477
}
13478+
shortcut.kernel = kernel;
1344913479
}
1345013480
module.exports = {
1345113481
kernelRunShortcut

dist/gpu-browser-core.min.js

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)