Skip to content

Commit 6d0b5d9

Browse files
MaxGraeydcodeIO
authored andcommitted
Minor string comparision optimizations (AssemblyScript#77)
1 parent 1013c21 commit 6d0b5d9

File tree

7 files changed

+151
-148
lines changed

7 files changed

+151
-148
lines changed

std/assembly/string.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ export class String {
111111
@operator("==")
112112
private static __eq(left: String, right: String): bool {
113113
if (left === right) return true;
114-
if (left === null) return right === null;
115-
if (right === null) return false;
114+
if (left === null || right === null) return false;
116115

117116
var leftLength = left.length;
118117
if (leftLength != right.length) return false;
@@ -150,8 +149,7 @@ export class String {
150149
@operator(">=")
151150
private static __gte(left: String, right: String): bool {
152151
if (left === right) return true;
153-
if (left === null) return right === null;
154-
if (right === null) return false;
152+
if (left === null || right === null) return false;
155153

156154
var leftLength = left.length;
157155
var rightLength = right.length;
@@ -188,8 +186,7 @@ export class String {
188186
@operator("<=")
189187
private static __lte(left: String, right: String): bool {
190188
if (left === right) return true;
191-
if (left === null) return right === null;
192-
if (right === null) return false;
189+
if (left === null || right === null) return false;
193190

194191
var leftLength = left.length;
195192
var rightLength = right.length;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@
140140
(call $abort
141141
(i32.const 0)
142142
(i32.const 40)
143-
(i32.const 234)
143+
(i32.const 231)
144144
(i32.const 4)
145145
)
146146
(unreachable)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@
293293
(call $abort
294294
(i32.const 0)
295295
(i32.const 40)
296-
(i32.const 234)
296+
(i32.const 231)
297297
(i32.const 4)
298298
)
299299
(unreachable)

tests/compiler/std/array.optimized.wat

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5320,18 +5320,19 @@
53205320
)
53215321
)
53225322
(if
5323-
(i32.eqz
5324-
(get_local $0)
5325-
)
5326-
(return
5327-
(i32.eqz
5328-
(get_local $1)
5323+
(i32.and
5324+
(if (result i32)
5325+
(tee_local $2
5326+
(i32.eqz
5327+
(get_local $0)
5328+
)
5329+
)
5330+
(get_local $2)
5331+
(i32.eqz
5332+
(get_local $1)
5333+
)
53295334
)
5330-
)
5331-
)
5332-
(if
5333-
(i32.eqz
5334-
(get_local $1)
5335+
(i32.const 1)
53355336
)
53365337
(return
53375338
(i32.const 0)

tests/compiler/std/array.untouched.wat

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10378,6 +10378,7 @@
1037810378
)
1037910379
(func $~lib/string/String.__eq (; 106 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
1038010380
(local $2 i32)
10381+
(local $3 i32)
1038110382
(if
1038210383
(i32.eq
1038310384
(get_local $0)
@@ -10388,34 +10389,34 @@
1038810389
)
1038910390
)
1039010391
(if
10391-
(i32.eq
10392-
(get_local $0)
10393-
(i32.const 0)
10394-
)
10395-
(return
10396-
(i32.eq
10397-
(get_local $1)
10398-
(i32.const 0)
10392+
(i32.and
10393+
(if (result i32)
10394+
(tee_local $2
10395+
(i32.eq
10396+
(get_local $0)
10397+
(i32.const 0)
10398+
)
10399+
)
10400+
(get_local $2)
10401+
(i32.eq
10402+
(get_local $1)
10403+
(i32.const 0)
10404+
)
1039910405
)
10400-
)
10401-
)
10402-
(if
10403-
(i32.eq
10404-
(get_local $1)
10405-
(i32.const 0)
10406+
(i32.const 1)
1040610407
)
1040710408
(return
1040810409
(i32.const 0)
1040910410
)
1041010411
)
10411-
(set_local $2
10412+
(set_local $3
1041210413
(i32.load
1041310414
(get_local $0)
1041410415
)
1041510416
)
1041610417
(if
1041710418
(i32.ne
10418-
(get_local $2)
10419+
(get_local $3)
1041910420
(i32.load
1042010421
(get_local $1)
1042110422
)
@@ -10436,7 +10437,7 @@
1043610437
(i32.const 4)
1043710438
)
1043810439
(i32.shl
10439-
(get_local $2)
10440+
(get_local $3)
1044010441
(i32.const 1)
1044110442
)
1044210443
)

tests/compiler/std/string.optimized.wat

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@
161161
(call $abort
162162
(i32.const 0)
163163
(i32.const 72)
164-
(i32.const 234)
164+
(i32.const 231)
165165
(i32.const 4)
166166
)
167167
(unreachable)
@@ -381,7 +381,7 @@
381381
(call $abort
382382
(i32.const 0)
383383
(i32.const 72)
384-
(i32.const 213)
384+
(i32.const 210)
385385
(i32.const 4)
386386
)
387387
(unreachable)
@@ -1092,7 +1092,7 @@
10921092
(call $abort
10931093
(i32.const 0)
10941094
(i32.const 72)
1095-
(i32.const 469)
1095+
(i32.const 466)
10961096
(i32.const 10)
10971097
)
10981098
(unreachable)
@@ -3293,18 +3293,19 @@
32933293
)
32943294
)
32953295
(if
3296-
(i32.eqz
3297-
(get_local $0)
3298-
)
3299-
(return
3300-
(i32.eqz
3301-
(get_local $1)
3296+
(i32.and
3297+
(if (result i32)
3298+
(tee_local $2
3299+
(i32.eqz
3300+
(get_local $0)
3301+
)
3302+
)
3303+
(get_local $2)
3304+
(i32.eqz
3305+
(get_local $1)
3306+
)
33023307
)
3303-
)
3304-
)
3305-
(if
3306-
(i32.eqz
3307-
(get_local $1)
3308+
(i32.const 1)
33083309
)
33093310
(return
33103311
(i32.const 0)
@@ -3447,45 +3448,46 @@
34473448
)
34483449
)
34493450
(if
3450-
(i32.eqz
3451-
(get_local $0)
3452-
)
3453-
(return
3454-
(i32.eqz
3455-
(get_local $1)
3451+
(i32.and
3452+
(if (result i32)
3453+
(tee_local $2
3454+
(i32.eqz
3455+
(get_local $0)
3456+
)
3457+
)
3458+
(get_local $2)
3459+
(i32.eqz
3460+
(get_local $1)
3461+
)
34563462
)
3457-
)
3458-
)
3459-
(if
3460-
(i32.eqz
3461-
(get_local $1)
3463+
(i32.const 1)
34623464
)
34633465
(return
34643466
(i32.const 0)
34653467
)
34663468
)
3467-
(set_local $2
3469+
(set_local $3
34683470
(i32.load
34693471
(get_local $1)
34703472
)
34713473
)
34723474
(if
34733475
(i32.eqz
3474-
(tee_local $3
3476+
(tee_local $2
34753477
(i32.load
34763478
(get_local $0)
34773479
)
34783480
)
34793481
)
34803482
(return
34813483
(i32.eqz
3482-
(get_local $2)
3484+
(get_local $3)
34833485
)
34843486
)
34853487
)
34863488
(if
34873489
(i32.eqz
3488-
(get_local $2)
3490+
(get_local $3)
34893491
)
34903492
(return
34913493
(i32.const 1)
@@ -3503,11 +3505,11 @@
35033505
)
35043506
(i32.shl
35053507
(select
3506-
(get_local $3)
35073508
(get_local $2)
3509+
(get_local $3)
35083510
(i32.lt_s
3509-
(get_local $3)
35103511
(get_local $2)
3512+
(get_local $3)
35113513
)
35123514
)
35133515
(i32.const 1)
@@ -3613,18 +3615,19 @@
36133615
)
36143616
)
36153617
(if
3616-
(i32.eqz
3617-
(get_local $0)
3618-
)
3619-
(return
3620-
(i32.eqz
3621-
(get_local $1)
3618+
(i32.and
3619+
(if (result i32)
3620+
(tee_local $2
3621+
(i32.eqz
3622+
(get_local $0)
3623+
)
3624+
)
3625+
(get_local $2)
3626+
(i32.eqz
3627+
(get_local $1)
3628+
)
36223629
)
3623-
)
3624-
)
3625-
(if
3626-
(i32.eqz
3627-
(get_local $1)
3630+
(i32.const 1)
36283631
)
36293632
(return
36303633
(i32.const 0)
@@ -3694,7 +3697,7 @@
36943697
(call $abort
36953698
(i32.const 0)
36963699
(i32.const 72)
3697-
(i32.const 386)
3700+
(i32.const 383)
36983701
(i32.const 4)
36993702
)
37003703
(unreachable)
@@ -3729,7 +3732,7 @@
37293732
(call $abort
37303733
(i32.const 0)
37313734
(i32.const 72)
3732-
(i32.const 391)
3735+
(i32.const 388)
37333736
(i32.const 6)
37343737
)
37353738
(unreachable)

0 commit comments

Comments
 (0)