Skip to content

Commit 1bc1f6b

Browse files
committed
Optimize repeat loop
1 parent fd7c2c7 commit 1bc1f6b

File tree

3 files changed

+57
-56
lines changed

3 files changed

+57
-56
lines changed

std/assembly/string.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -396,15 +396,18 @@ export class String {
396396
if (count === 0 || !this.length) return EMPTY;
397397
if (count === 1) return this;
398398

399-
var str = changetype<String>(this);
400-
var result = EMPTY;
399+
var result = changetype<String>(this);
400+
var isOdd = count & 1;
401401

402-
while (count) {
403-
if (count & 1) result = result.concat(str);
404-
if (count > 1) str = str.concat(str);
402+
while (count > 1) {
403+
result = result.concat(result);
405404
count >>= 1;
406405
}
407406

407+
if (isOdd) {
408+
return result.concat(this);
409+
}
410+
408411
return result;
409412
}
410413

tests/compiler/std/string.optimized.wat

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,7 +1088,7 @@
10881088
(call $abort
10891089
(i32.const 0)
10901090
(i32.const 72)
1091-
(i32.const 590)
1091+
(i32.const 593)
10921092
(i32.const 10)
10931093
)
10941094
(unreachable)
@@ -3664,6 +3664,7 @@
36643664
)
36653665
(func $~lib/string/String#repeat (; 28 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
36663666
(local $2 i32)
3667+
(local $3 i32)
36673668
(if
36683669
(i32.eqz
36693670
(get_local $0)
@@ -3744,34 +3745,25 @@
37443745
)
37453746
)
37463747
(set_local $2
3747-
(i32.const 332)
3748+
(get_local $0)
3749+
)
3750+
(set_local $3
3751+
(i32.and
3752+
(get_local $1)
3753+
(i32.const 1)
3754+
)
37483755
)
37493756
(loop $continue|0
37503757
(if
3751-
(get_local $1)
3758+
(i32.gt_s
3759+
(get_local $1)
3760+
(i32.const 1)
3761+
)
37523762
(block
3753-
(if
3754-
(i32.and
3755-
(get_local $1)
3756-
(i32.const 1)
3757-
)
3758-
(set_local $2
3759-
(call $~lib/string/String#concat
3760-
(get_local $2)
3761-
(get_local $0)
3762-
)
3763-
)
3764-
)
3765-
(if
3766-
(i32.gt_s
3767-
(get_local $1)
3768-
(i32.const 1)
3769-
)
3770-
(set_local $0
3771-
(call $~lib/string/String#concat
3772-
(get_local $0)
3773-
(get_local $0)
3774-
)
3763+
(set_local $2
3764+
(call $~lib/string/String#concat
3765+
(get_local $2)
3766+
(get_local $2)
37753767
)
37763768
)
37773769
(set_local $1
@@ -3784,6 +3776,15 @@
37843776
)
37853777
)
37863778
)
3779+
(if
3780+
(get_local $3)
3781+
(return
3782+
(call $~lib/string/String#concat
3783+
(get_local $2)
3784+
(get_local $0)
3785+
)
3786+
)
3787+
)
37873788
(get_local $2)
37883789
)
37893790
(func $~lib/string/String#repeat|trampoline (; 29 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)

tests/compiler/std/string.untouched.wat

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,7 +1266,7 @@
12661266
(call $abort
12671267
(i32.const 0)
12681268
(i32.const 72)
1269-
(i32.const 590)
1269+
(i32.const 593)
12701270
(i32.const 10)
12711271
)
12721272
(unreachable)
@@ -4320,36 +4320,24 @@
43204320
(get_local $0)
43214321
)
43224322
(set_local $4
4323-
(i32.const 332)
4323+
(i32.and
4324+
(get_local $1)
4325+
(i32.const 1)
4326+
)
43244327
)
43254328
(block $break|0
43264329
(loop $continue|0
43274330
(if
4328-
(get_local $1)
4331+
(i32.gt_s
4332+
(get_local $1)
4333+
(i32.const 1)
4334+
)
43294335
(block
43304336
(block
4331-
(if
4332-
(i32.and
4333-
(get_local $1)
4334-
(i32.const 1)
4335-
)
4336-
(set_local $4
4337-
(call $~lib/string/String#concat
4338-
(get_local $4)
4339-
(get_local $3)
4340-
)
4341-
)
4342-
)
4343-
(if
4344-
(i32.gt_s
4345-
(get_local $1)
4346-
(i32.const 1)
4347-
)
4348-
(set_local $3
4349-
(call $~lib/string/String#concat
4350-
(get_local $3)
4351-
(get_local $3)
4352-
)
4337+
(set_local $3
4338+
(call $~lib/string/String#concat
4339+
(get_local $3)
4340+
(get_local $3)
43534341
)
43544342
)
43554343
(set_local $1
@@ -4364,8 +4352,17 @@
43644352
)
43654353
)
43664354
)
4367-
(return
4355+
(if
43684356
(get_local $4)
4357+
(return
4358+
(call $~lib/string/String#concat
4359+
(get_local $3)
4360+
(get_local $0)
4361+
)
4362+
)
4363+
)
4364+
(return
4365+
(get_local $3)
43694366
)
43704367
)
43714368
(func $~lib/string/String#repeat|trampoline (; 29 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)

0 commit comments

Comments
 (0)