Skip to content

Commit fd7c2c7

Browse files
committed
Add early returns for string comparisions
1 parent aabb7a6 commit fd7c2c7

File tree

5 files changed

+135
-43
lines changed

5 files changed

+135
-43
lines changed

std/assembly/string.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,9 @@ export class String {
134134

135135
@operator(">")
136136
private static __gt(left: String, right: String): bool {
137-
if (left === null || right === null) return false;
137+
if (left === right || left === null || right === null) {
138+
return false;
139+
}
138140

139141
var leftLength = left.length;
140142
var rightLength = right.length;
@@ -152,8 +154,9 @@ export class String {
152154

153155
@operator(">=")
154156
private static __gte(left: String, right: String): bool {
157+
if (left === right) return true;
155158
if (left === null) return right === null;
156-
else if (right === null) return false;
159+
if (right === null) return false;
157160

158161
var leftLength = left.length;
159162
var rightLength = right.length;
@@ -171,7 +174,9 @@ export class String {
171174

172175
@operator("<")
173176
private static __lt(left: String, right: String): bool {
174-
if (left === null || right === null) return false;
177+
if (left === right || left === null || right === null) {
178+
return false;
179+
}
175180

176181
var leftLength = left.length;
177182
var rightLength = right.length;
@@ -189,6 +194,7 @@ export class String {
189194

190195
@operator("<=")
191196
private static __lte(left: String, right: String): bool {
197+
if (left === right) return true;
192198
if (left === null) return right === null;
193199
else if (right === null) return false;
194200

tests/compiler/std/array-access.optimized.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@
125125
(call $abort
126126
(i32.const 0)
127127
(i32.const 8)
128-
(i32.const 235)
128+
(i32.const 241)
129129
(i32.const 4)
130130
)
131131
(unreachable)

tests/compiler/std/array-access.untouched.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@
195195
(call $abort
196196
(i32.const 0)
197197
(i32.const 8)
198-
(i32.const 235)
198+
(i32.const 241)
199199
(i32.const 4)
200200
)
201201
(unreachable)

tests/compiler/std/string.optimized.wat

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@
157157
(call $abort
158158
(i32.const 0)
159159
(i32.const 72)
160-
(i32.const 235)
160+
(i32.const 241)
161161
(i32.const 4)
162162
)
163163
(unreachable)
@@ -377,7 +377,7 @@
377377
(call $abort
378378
(i32.const 0)
379379
(i32.const 72)
380-
(i32.const 214)
380+
(i32.const 220)
381381
(i32.const 4)
382382
)
383383
(unreachable)
@@ -1088,7 +1088,7 @@
10881088
(call $abort
10891089
(i32.const 0)
10901090
(i32.const 72)
1091-
(i32.const 584)
1091+
(i32.const 590)
10921092
(i32.const 10)
10931093
)
10941094
(unreachable)
@@ -3339,8 +3339,20 @@
33393339
(i32.and
33403340
(if (result i32)
33413341
(tee_local $2
3342-
(i32.eqz
3343-
(get_local $0)
3342+
(i32.and
3343+
(if (result i32)
3344+
(tee_local $2
3345+
(i32.eq
3346+
(get_local $0)
3347+
(get_local $1)
3348+
)
3349+
)
3350+
(get_local $2)
3351+
(i32.eqz
3352+
(get_local $0)
3353+
)
3354+
)
3355+
(i32.const 1)
33443356
)
33453357
)
33463358
(get_local $2)
@@ -3408,21 +3420,32 @@
34083420
(local $2 i32)
34093421
(local $3 i32)
34103422
(if
3411-
(get_local $0)
3412-
(if
3413-
(i32.eqz
3414-
(get_local $1)
3415-
)
3416-
(return
3417-
(i32.const 0)
3418-
)
3423+
(i32.eq
3424+
(get_local $0)
3425+
(get_local $1)
3426+
)
3427+
(return
3428+
(i32.const 1)
3429+
)
3430+
)
3431+
(if
3432+
(i32.eqz
3433+
(get_local $0)
34193434
)
34203435
(return
34213436
(i32.eqz
34223437
(get_local $1)
34233438
)
34243439
)
34253440
)
3441+
(if
3442+
(i32.eqz
3443+
(get_local $1)
3444+
)
3445+
(return
3446+
(i32.const 0)
3447+
)
3448+
)
34263449
(set_local $2
34273450
(i32.load
34283451
(get_local $1)
@@ -3482,8 +3505,20 @@
34823505
(i32.and
34833506
(if (result i32)
34843507
(tee_local $2
3485-
(i32.eqz
3486-
(get_local $0)
3508+
(i32.and
3509+
(if (result i32)
3510+
(tee_local $2
3511+
(i32.eq
3512+
(get_local $0)
3513+
(get_local $1)
3514+
)
3515+
)
3516+
(get_local $2)
3517+
(i32.eqz
3518+
(get_local $0)
3519+
)
3520+
)
3521+
(i32.const 1)
34873522
)
34883523
)
34893524
(get_local $2)
@@ -3550,6 +3585,15 @@
35503585
(func $~lib/string/String.__lte (; 27 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
35513586
(local $2 i32)
35523587
(local $3 i32)
3588+
(if
3589+
(i32.eq
3590+
(get_local $0)
3591+
(get_local $1)
3592+
)
3593+
(return
3594+
(i32.const 1)
3595+
)
3596+
)
35533597
(if
35543598
(get_local $0)
35553599
(if
@@ -3628,7 +3672,7 @@
36283672
(call $abort
36293673
(i32.const 0)
36303674
(i32.const 72)
3631-
(i32.const 385)
3675+
(i32.const 391)
36323676
(i32.const 4)
36333677
)
36343678
(unreachable)
@@ -3643,7 +3687,7 @@
36433687
(call $abort
36443688
(i32.const 0)
36453689
(i32.const 72)
3646-
(i32.const 386)
3690+
(i32.const 392)
36473691
(i32.const 4)
36483692
)
36493693
(unreachable)
@@ -3663,7 +3707,7 @@
36633707
(call $abort
36643708
(i32.const 0)
36653709
(i32.const 72)
3666-
(i32.const 388)
3710+
(i32.const 394)
36673711
(i32.const 4)
36683712
)
36693713
(unreachable)

tests/compiler/std/string.untouched.wat

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@
206206
(call $abort
207207
(i32.const 0)
208208
(i32.const 72)
209-
(i32.const 235)
209+
(i32.const 241)
210210
(i32.const 4)
211211
)
212212
(unreachable)
@@ -464,7 +464,7 @@
464464
(call $abort
465465
(i32.const 0)
466466
(i32.const 72)
467-
(i32.const 214)
467+
(i32.const 220)
468468
(i32.const 4)
469469
)
470470
(unreachable)
@@ -1266,7 +1266,7 @@
12661266
(call $abort
12671267
(i32.const 0)
12681268
(i32.const 72)
1269-
(i32.const 584)
1269+
(i32.const 590)
12701270
(i32.const 10)
12711271
)
12721272
(unreachable)
@@ -3838,9 +3838,21 @@
38383838
(i32.and
38393839
(if (result i32)
38403840
(tee_local $2
3841-
(i32.eq
3842-
(get_local $0)
3843-
(i32.const 0)
3841+
(i32.and
3842+
(if (result i32)
3843+
(tee_local $2
3844+
(i32.eq
3845+
(get_local $0)
3846+
(get_local $1)
3847+
)
3848+
)
3849+
(get_local $2)
3850+
(i32.eq
3851+
(get_local $0)
3852+
(i32.const 0)
3853+
)
3854+
)
3855+
(i32.const 1)
38443856
)
38453857
)
38463858
(get_local $2)
@@ -3921,6 +3933,15 @@
39213933
(local $4 i32)
39223934
(local $5 i32)
39233935
(local $6 i32)
3936+
(if
3937+
(i32.eq
3938+
(get_local $0)
3939+
(get_local $1)
3940+
)
3941+
(return
3942+
(i32.const 1)
3943+
)
3944+
)
39243945
(if
39253946
(i32.eq
39263947
(get_local $0)
@@ -3932,14 +3953,14 @@
39323953
(i32.const 0)
39333954
)
39343955
)
3935-
(if
3936-
(i32.eq
3937-
(get_local $1)
3938-
(i32.const 0)
3939-
)
3940-
(return
3941-
(i32.const 0)
3942-
)
3956+
)
3957+
(if
3958+
(i32.eq
3959+
(get_local $1)
3960+
(i32.const 0)
3961+
)
3962+
(return
3963+
(i32.const 0)
39433964
)
39443965
)
39453966
(set_local $2
@@ -4014,9 +4035,21 @@
40144035
(i32.and
40154036
(if (result i32)
40164037
(tee_local $2
4017-
(i32.eq
4018-
(get_local $0)
4019-
(i32.const 0)
4038+
(i32.and
4039+
(if (result i32)
4040+
(tee_local $2
4041+
(i32.eq
4042+
(get_local $0)
4043+
(get_local $1)
4044+
)
4045+
)
4046+
(get_local $2)
4047+
(i32.eq
4048+
(get_local $0)
4049+
(i32.const 0)
4050+
)
4051+
)
4052+
(i32.const 1)
40204053
)
40214054
)
40224055
(get_local $2)
@@ -4097,6 +4130,15 @@
40974130
(local $4 i32)
40984131
(local $5 i32)
40994132
(local $6 i32)
4133+
(if
4134+
(i32.eq
4135+
(get_local $0)
4136+
(get_local $1)
4137+
)
4138+
(return
4139+
(i32.const 1)
4140+
)
4141+
)
41004142
(if
41014143
(i32.eq
41024144
(get_local $0)
@@ -4195,7 +4237,7 @@
41954237
(call $abort
41964238
(i32.const 0)
41974239
(i32.const 72)
4198-
(i32.const 385)
4240+
(i32.const 391)
41994241
(i32.const 4)
42004242
)
42014243
(unreachable)
@@ -4212,7 +4254,7 @@
42124254
(call $abort
42134255
(i32.const 0)
42144256
(i32.const 72)
4215-
(i32.const 386)
4257+
(i32.const 392)
42164258
(i32.const 4)
42174259
)
42184260
(unreachable)
@@ -4237,7 +4279,7 @@
42374279
(call $abort
42384280
(i32.const 0)
42394281
(i32.const 72)
4240-
(i32.const 388)
4282+
(i32.const 394)
42414283
(i32.const 4)
42424284
)
42434285
(unreachable)

0 commit comments

Comments
 (0)