Skip to content

Commit 3968f3f

Browse files
committed
impl: Improve song loading performance
- Initialize cells in the `add_block()` function in the reverse order; - When opening a song, the song's notes will be put into a custom-growth array before being added to the program in the reverse order/
1 parent 97335af commit 3968f3f

2 files changed

Lines changed: 65 additions & 26 deletions

File tree

scripts/add_block/add_block.gml

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,19 @@ function add_block() {
1616

1717
// Initialize cells
1818
if (xx >= arraylength) {
19-
for (a = arraylength; a <= xx; a += 1) {
20-
colfirst[a] = -1
21-
collast[a] = -1
22-
colamount[a] = 0
23-
for (b = 0; b < max(yy + 1, arrayheight); b += 1) song_exists[a, b] = 0
24-
}
25-
arraylength = xx + 1
19+
for (a = xx; a >= arraylength; --a) {
20+
colfirst[@ a] = -1
21+
collast[@ a] = -1
22+
colamount[@ a] = 0
23+
for (b = max(yy + 1, arrayheight) - 1; b >= 0; --b) song_exists[@ a, b] = 0
24+
}
25+
arraylength = xx + 1
2626
}
2727
if (yy >= arrayheight) {
28-
for (a = 0; a < arraylength; a += 1) {
29-
for (b = arrayheight; b <= yy ;b += 1) {
30-
rowamount[b] = 0
31-
song_exists[a, b] = 0
28+
for (a = arraylength - 1; a >= 0; --a) {
29+
for (b = yy; b >= arrayheight; --b) {
30+
rowamount[@ b] = 0
31+
song_exists[@ a, b] = 0
3232
}
3333
}
3434
arrayheight = yy + 1
@@ -37,16 +37,16 @@ function add_block() {
3737
if (song_exists[xx, yy]) return 0
3838

3939
// Add block
40-
song_exists[xx, yy] = 1
41-
song_ins[xx, yy] = ins
42-
song_key[xx, yy] = key
43-
song_vel[xx, yy] = vel
44-
song_pan[xx, yy] = pan
45-
song_pit[xx, yy] = pit
46-
song_played[xx, yy] = 0
47-
song_added[xx, yy] = 0
48-
colamount[xx] += 1
49-
rowamount[yy] += 1
40+
song_exists[@ xx, yy] = 1
41+
song_ins[@ xx, yy] = ins
42+
song_key[@ xx, yy] = key
43+
song_vel[@ xx, yy] = vel
44+
song_pan[@ xx, yy] = pan
45+
song_pit[@ xx, yy] = pit
46+
song_played[@xx, yy] = 0
47+
song_added[@ xx, yy] = 0
48+
colamount[@ xx] += 1
49+
rowamount[@ yy] += 1
5050
if (!insnum) {
5151
ins.num_blocks++
5252
if (ins.user) block_custom += 1
@@ -56,7 +56,7 @@ function add_block() {
5656
totalblocks += 1
5757

5858
if (colfirst[xx] = -1 || yy < colfirst[xx]) colfirst[xx] = yy
59-
if (yy > collast[xx]) collast[xx] = yy
59+
if (yy > collast[xx]) collast[@ xx] = yy
6060

6161
if (xx >= enda) enda = xx
6262
if (yy >= endb) endb = yy

scripts/load_song/load_song.gml

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function load_song() {
3333

3434
byte1 = buffer_read_byte()
3535
byte2 = buffer_read_byte()
36-
36+
3737
if (byte1 = 0 && byte2 = 0) {
3838
song_nbs_version = buffer_read_byte()
3939
if (language != 1) {if (show_oldwarning && song_nbs_version < nbs_version) message("Warning: You are opening an older NBS file. Saving this file will make it incompatible with older Note Block Studio versions.","Warning")}
@@ -102,6 +102,11 @@ function load_song() {
102102
loopstart = buffer_read_short()
103103
}
104104
}
105+
106+
// Song's notes will be put into the following array before being added to the program
107+
var notes = []
108+
var notes_at = 0
109+
105110
// Note blocks
106111
ca = -1
107112
while (1) {
@@ -125,9 +130,29 @@ function load_song() {
125130
pan = 100
126131
pit = 0
127132
}
128-
add_block(ca, cb, ins, median(0, key, 87), vel, pan, pit, true)
133+
array_grow_then_set(notes, notes_at++, {
134+
a: ca,
135+
b: cb,
136+
ins: ins,
137+
key: median(0, key, 87),
138+
vel: vel,
139+
pan: vel,
140+
pit: pit,
141+
});
129142
}
130143
}
144+
array_resize(notes, notes_at)
145+
146+
if (array_length(notes) > 0) {
147+
// Add blocks in reverse order to improve performance
148+
var length = array_length(notes)
149+
for (var i = length - 1; i >= 0; --i) {
150+
var note = notes[i]
151+
add_block(note.a, note.b, note.ins, note.key, note.vel, note.pan, note.pit, true)
152+
delete notes[i]
153+
}
154+
}
155+
131156
if (buffer_is_eof()) { // End?
132157
buffer_delete(buffer)
133158
add_to_recent(fn)
@@ -206,6 +231,20 @@ function load_song() {
206231
blocks_set_instruments()
207232
io_clear()
208233

209-
210-
211234
}
235+
236+
function array_grow_then_set(array, index, value) {
237+
// Behave like array_set(), except it allows custom growth of the array
238+
239+
var length = array_length(array)
240+
if (index >= length) {
241+
var new_size = index + 1
242+
var new_length = new_size * 3 / 2; // 1.5x growth factor
243+
//var new_length = (new_size + (new_size >> 3) + 6) & ~3; // Python-style growth
244+
show_debug_message(string(length) + " -> " + string(floor(new_length)))
245+
for (var i = new_length - 1; i >= length; --i) {
246+
array[@ i] = 0
247+
}
248+
}
249+
array[@ index] = value
250+
}

0 commit comments

Comments
 (0)