Skip to content

Commit

Permalink
optimized internal matrix ops by speeding up shape & rank determinati…
Browse files Browse the repository at this point in the history
…on assuming well-formed tables
  • Loading branch information
jmoenig committed Jan 10, 2024
1 parent 8b99894 commit af6024a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 11 deletions.
3 changes: 3 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
* fixed speech balloons inside ASK menus
* added safeguard against accidentally querying too many list dimensions (forgetting to transpose a convolution)

2024-01-10
* lists, threads, objects: optimized internal matrix ops by speeding up shape & rank determination assuming well-formed tables

2024-01-09
* pixels library: added Sobel edge-detection variants for left/right/top/bottom
* threads: use the microphone's sample rate for playing back lists of samples once it has been used, otherwise 44.1 kHz
Expand Down
6 changes: 3 additions & 3 deletions snap.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
<script src="src/symbols.js?version=2023-07-13"></script>
<script src="src/widgets.js?version=2023-05-24"></script>
<script src="src/blocks.js?version=2023-11-24"></script>
<script src="src/threads.js?version=2024-01-09"></script>
<script src="src/objects.js?version=2024-01-08"></script>
<script src="src/threads.js?version=2024-01-10"></script>
<script src="src/objects.js?version=2024-01-10"></script>
<script src="src/scenes.js?version=2022-10-29"></script>
<script src="src/gui.js?version=2024-01-24"></script>
<script src="src/paint.js?version=2023-05-24"></script>
<script src="src/lists.js?version=2024-01-08"></script>
<script src="src/lists.js?version=2024-01-10"></script>
<script src="src/byob.js?version=2023-07-14"></script>
<script src="src/tables.js?version=2023-07-05"></script>
<script src="src/sketch.js?version=2023-05-24"></script>
Expand Down
23 changes: 22 additions & 1 deletion src/lists.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Context, ZERO, WHITE*/

// Global settings /////////////////////////////////////////////////////

modules.lists = '2024-January-08';
modules.lists = '2024-January-10';

var List;
var ListWatcherMorph;
Expand Down Expand Up @@ -660,6 +660,14 @@ List.prototype.rank = function () {
return rank;
};

List.prototype.quickRank = function () {
// answer the number of my dimensions
// only look at the first item of each dimension,
// assuming regularly shaped nested lists
var item = this.at(1);
return item instanceof List ? item.quickRank() + 1 : 1;
};

List.prototype.shape = function () {
// answer a list of the maximum size for each dimension
var dim,
Expand All @@ -678,6 +686,19 @@ List.prototype.shape = function () {
return shp;
};

List.prototype.quickShape = function () {
// answer a list of each dimension's size
// only look at the first item of each dimension,
// assuming regularly shaped nested lists
var shp = [],
item = this;
while (item instanceof List) {
shp.push(item.length());
item = item.at(1);
}
return new List(shp);
};

List.prototype.getDimension = function (rank = 0) {
// private - answer a list of all elements of the specified rank
if (rank < 1) {return new List(); }
Expand Down
4 changes: 2 additions & 2 deletions src/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ embedMetadataPNG, SnapExtensions, SnapSerializer, snapEquals*/

/*jshint esversion: 11*/

modules.objects = '2024-January-08';
modules.objects = '2024-January-10';

var SpriteMorph;
var StageMorph;
Expand Down Expand Up @@ -4340,7 +4340,7 @@ SpriteMorph.prototype.doSwitchToCostume = function (id, noShadow) {
h = 0,
stage;
if (id instanceof List) { // try to turn a list of pixels into a costume
if (id.shape().at(2) <= 4) {
if (id.quickShape().at(2) <= 4) {
if (this.costume) {
// recycle dimensions of current costume
w = this.costume.width();
Expand Down
15 changes: 10 additions & 5 deletions src/threads.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ StagePickerMorph, CustomBlockDefinition, CommentMorph*/

/*jshint esversion: 11, bitwise: false, evil: true*/

modules.threads = '2024-January-09';
modules.threads = '2024-January-10';

var ThreadManager;
var Process;
Expand Down Expand Up @@ -2378,6 +2378,11 @@ Process.prototype.reportRank = function (data) {
return data instanceof List ? data.rank() : 0;
};

Process.prototype.reportQuickRank = function (data) {
// private - assume a regularly shaped nested list
return data instanceof List ? data.quickRank() : 0;
};

Process.prototype.reportDimensions = function (data) {
return data instanceof List ? data.shape() : new List();
};
Expand Down Expand Up @@ -4518,7 +4523,7 @@ Process.prototype.hyperMonadic = function (baseOp, arg) {
};

Process.prototype.hyperDyadic = function (baseOp, a, b) {
var match = Math.min(this.reportRank(a), this.reportRank(b));
var match = Math.min(this.reportQuickRank(a), this.reportQuickRank(b));
return this.hyperZip(baseOp, a, b, match, match);
};

Expand All @@ -4528,8 +4533,8 @@ Process.prototype.hyperZip = function (baseOp, a, b, zipa, zipb) {
// this allows to write 2d matrix convolutions for 3+d inputs with
// 2d kernels e.g. for image processing without having to first
// reshape the kernel matrix to match the broadcast shape.
var arank = this.reportRank(a),
brank = this.reportRank(b),
var arank = this.reportQuickRank(a),
brank = this.reportQuickRank(b),
len, i, result;
if (arank === brank || (arank <= zipa && brank <= zipb)) {
if (arank + brank === 0) {
Expand Down Expand Up @@ -7204,7 +7209,7 @@ Process.prototype.reportNewCostume = function (pixels, width, height, name) {
}
if (width <= 0 || height <= 0) {
// try to interpret the pixels as matrix
shp = this.reportDimensions(pixels);
shp = pixels.quickShape();
if (shp.at(2) > 4) {
height = shp.at(1);
width = shp.at(2);
Expand Down

0 comments on commit af6024a

Please sign in to comment.