From 6d0b5d92c2956501a37dcf32fbcf4b87beaafbf9 Mon Sep 17 00:00:00 2001 From: Max Graey Date: Tue, 17 Apr 2018 00:02:55 +0300 Subject: [PATCH] Minor string comparision optimizations (#77) --- std/assembly/string.ts | 9 +- tests/compiler/std/array-access.optimized.wat | 2 +- tests/compiler/std/array-access.untouched.wat | 2 +- tests/compiler/std/array.optimized.wat | 23 +-- tests/compiler/std/array.untouched.wat | 35 ++-- tests/compiler/std/string.optimized.wat | 91 ++++----- tests/compiler/std/string.untouched.wat | 179 +++++++++--------- 7 files changed, 172 insertions(+), 169 deletions(-) diff --git a/std/assembly/string.ts b/std/assembly/string.ts index e8ca36b7..3d97250d 100644 --- a/std/assembly/string.ts +++ b/std/assembly/string.ts @@ -111,8 +111,7 @@ export class String { @operator("==") private static __eq(left: String, right: String): bool { if (left === right) return true; - if (left === null) return right === null; - if (right === null) return false; + if (left === null || right === null) return false; var leftLength = left.length; if (leftLength != right.length) return false; @@ -150,8 +149,7 @@ export class String { @operator(">=") private static __gte(left: String, right: String): bool { if (left === right) return true; - if (left === null) return right === null; - if (right === null) return false; + if (left === null || right === null) return false; var leftLength = left.length; var rightLength = right.length; @@ -188,8 +186,7 @@ export class String { @operator("<=") private static __lte(left: String, right: String): bool { if (left === right) return true; - if (left === null) return right === null; - if (right === null) return false; + if (left === null || right === null) return false; var leftLength = left.length; var rightLength = right.length; diff --git a/tests/compiler/std/array-access.optimized.wat b/tests/compiler/std/array-access.optimized.wat index 037310b6..dc63f443 100644 --- a/tests/compiler/std/array-access.optimized.wat +++ b/tests/compiler/std/array-access.optimized.wat @@ -140,7 +140,7 @@ (call $abort (i32.const 0) (i32.const 40) - (i32.const 234) + (i32.const 231) (i32.const 4) ) (unreachable) diff --git a/tests/compiler/std/array-access.untouched.wat b/tests/compiler/std/array-access.untouched.wat index 211da89b..aa42c1ba 100644 --- a/tests/compiler/std/array-access.untouched.wat +++ b/tests/compiler/std/array-access.untouched.wat @@ -293,7 +293,7 @@ (call $abort (i32.const 0) (i32.const 40) - (i32.const 234) + (i32.const 231) (i32.const 4) ) (unreachable) diff --git a/tests/compiler/std/array.optimized.wat b/tests/compiler/std/array.optimized.wat index 9bc411e2..aaabc73e 100644 --- a/tests/compiler/std/array.optimized.wat +++ b/tests/compiler/std/array.optimized.wat @@ -5320,18 +5320,19 @@ ) ) (if - (i32.eqz - (get_local $0) - ) - (return - (i32.eqz - (get_local $1) + (i32.and + (if (result i32) + (tee_local $2 + (i32.eqz + (get_local $0) + ) + ) + (get_local $2) + (i32.eqz + (get_local $1) + ) ) - ) - ) - (if - (i32.eqz - (get_local $1) + (i32.const 1) ) (return (i32.const 0) diff --git a/tests/compiler/std/array.untouched.wat b/tests/compiler/std/array.untouched.wat index 144e0b8e..e5dfdc0f 100644 --- a/tests/compiler/std/array.untouched.wat +++ b/tests/compiler/std/array.untouched.wat @@ -10378,6 +10378,7 @@ ) (func $~lib/string/String.__eq (; 106 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32) (local $2 i32) + (local $3 i32) (if (i32.eq (get_local $0) @@ -10388,34 +10389,34 @@ ) ) (if - (i32.eq - (get_local $0) - (i32.const 0) - ) - (return - (i32.eq - (get_local $1) - (i32.const 0) + (i32.and + (if (result i32) + (tee_local $2 + (i32.eq + (get_local $0) + (i32.const 0) + ) + ) + (get_local $2) + (i32.eq + (get_local $1) + (i32.const 0) + ) ) - ) - ) - (if - (i32.eq - (get_local $1) - (i32.const 0) + (i32.const 1) ) (return (i32.const 0) ) ) - (set_local $2 + (set_local $3 (i32.load (get_local $0) ) ) (if (i32.ne - (get_local $2) + (get_local $3) (i32.load (get_local $1) ) @@ -10436,7 +10437,7 @@ (i32.const 4) ) (i32.shl - (get_local $2) + (get_local $3) (i32.const 1) ) ) diff --git a/tests/compiler/std/string.optimized.wat b/tests/compiler/std/string.optimized.wat index a535b7d7..ad017436 100644 --- a/tests/compiler/std/string.optimized.wat +++ b/tests/compiler/std/string.optimized.wat @@ -161,7 +161,7 @@ (call $abort (i32.const 0) (i32.const 72) - (i32.const 234) + (i32.const 231) (i32.const 4) ) (unreachable) @@ -381,7 +381,7 @@ (call $abort (i32.const 0) (i32.const 72) - (i32.const 213) + (i32.const 210) (i32.const 4) ) (unreachable) @@ -1092,7 +1092,7 @@ (call $abort (i32.const 0) (i32.const 72) - (i32.const 469) + (i32.const 466) (i32.const 10) ) (unreachable) @@ -3293,18 +3293,19 @@ ) ) (if - (i32.eqz - (get_local $0) - ) - (return - (i32.eqz - (get_local $1) + (i32.and + (if (result i32) + (tee_local $2 + (i32.eqz + (get_local $0) + ) + ) + (get_local $2) + (i32.eqz + (get_local $1) + ) ) - ) - ) - (if - (i32.eqz - (get_local $1) + (i32.const 1) ) (return (i32.const 0) @@ -3447,31 +3448,32 @@ ) ) (if - (i32.eqz - (get_local $0) - ) - (return - (i32.eqz - (get_local $1) + (i32.and + (if (result i32) + (tee_local $2 + (i32.eqz + (get_local $0) + ) + ) + (get_local $2) + (i32.eqz + (get_local $1) + ) ) - ) - ) - (if - (i32.eqz - (get_local $1) + (i32.const 1) ) (return (i32.const 0) ) ) - (set_local $2 + (set_local $3 (i32.load (get_local $1) ) ) (if (i32.eqz - (tee_local $3 + (tee_local $2 (i32.load (get_local $0) ) @@ -3479,13 +3481,13 @@ ) (return (i32.eqz - (get_local $2) + (get_local $3) ) ) ) (if (i32.eqz - (get_local $2) + (get_local $3) ) (return (i32.const 1) @@ -3503,11 +3505,11 @@ ) (i32.shl (select - (get_local $3) (get_local $2) + (get_local $3) (i32.lt_s - (get_local $3) (get_local $2) + (get_local $3) ) ) (i32.const 1) @@ -3613,18 +3615,19 @@ ) ) (if - (i32.eqz - (get_local $0) - ) - (return - (i32.eqz - (get_local $1) + (i32.and + (if (result i32) + (tee_local $2 + (i32.eqz + (get_local $0) + ) + ) + (get_local $2) + (i32.eqz + (get_local $1) + ) ) - ) - ) - (if - (i32.eqz - (get_local $1) + (i32.const 1) ) (return (i32.const 0) @@ -3694,7 +3697,7 @@ (call $abort (i32.const 0) (i32.const 72) - (i32.const 386) + (i32.const 383) (i32.const 4) ) (unreachable) @@ -3729,7 +3732,7 @@ (call $abort (i32.const 0) (i32.const 72) - (i32.const 391) + (i32.const 388) (i32.const 6) ) (unreachable) diff --git a/tests/compiler/std/string.untouched.wat b/tests/compiler/std/string.untouched.wat index c7133f22..d182e190 100644 --- a/tests/compiler/std/string.untouched.wat +++ b/tests/compiler/std/string.untouched.wat @@ -211,7 +211,7 @@ (call $abort (i32.const 0) (i32.const 72) - (i32.const 234) + (i32.const 231) (i32.const 4) ) (unreachable) @@ -469,7 +469,7 @@ (call $abort (i32.const 0) (i32.const 72) - (i32.const 213) + (i32.const 210) (i32.const 4) ) (unreachable) @@ -1271,7 +1271,7 @@ (call $abort (i32.const 0) (i32.const 72) - (i32.const 469) + (i32.const 466) (i32.const 10) ) (unreachable) @@ -3770,6 +3770,7 @@ ) (func $~lib/string/String.__eq (; 22 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32) (local $2 i32) + (local $3 i32) (if (i32.eq (get_local $0) @@ -3780,34 +3781,34 @@ ) ) (if - (i32.eq - (get_local $0) - (i32.const 0) - ) - (return - (i32.eq - (get_local $1) - (i32.const 0) + (i32.and + (if (result i32) + (tee_local $2 + (i32.eq + (get_local $0) + (i32.const 0) + ) + ) + (get_local $2) + (i32.eq + (get_local $1) + (i32.const 0) + ) ) - ) - ) - (if - (i32.eq - (get_local $1) - (i32.const 0) + (i32.const 1) ) (return (i32.const 0) ) ) - (set_local $2 + (set_local $3 (i32.load (get_local $0) ) ) (if (i32.ne - (get_local $2) + (get_local $3) (i32.load (get_local $1) ) @@ -3828,7 +3829,7 @@ (i32.const 4) ) (i32.shl - (get_local $2) + (get_local $3) (i32.const 1) ) ) @@ -3960,64 +3961,64 @@ ) ) (if - (i32.eq - (get_local $0) - (i32.const 0) - ) - (return - (i32.eq - (get_local $1) - (i32.const 0) + (i32.and + (if (result i32) + (tee_local $2 + (i32.eq + (get_local $0) + (i32.const 0) + ) + ) + (get_local $2) + (i32.eq + (get_local $1) + (i32.const 0) + ) ) - ) - ) - (if - (i32.eq - (get_local $1) - (i32.const 0) + (i32.const 1) ) (return (i32.const 0) ) ) - (set_local $2 - (i32.load - (get_local $0) - ) - ) (set_local $3 (i32.load - (get_local $1) + (get_local $0) ) ) - (if - (i32.eqz - (get_local $2) - ) - (return - (i32.eqz - (get_local $3) - ) + (set_local $4 + (i32.load + (get_local $1) ) ) (if (i32.eqz (get_local $3) ) + (return + (i32.eqz + (get_local $4) + ) + ) + ) + (if + (i32.eqz + (get_local $4) + ) (return (i32.const 1) ) ) (set_local $6 (select - (tee_local $4 - (get_local $2) - ) - (tee_local $5 + (tee_local $2 (get_local $3) ) - (i32.lt_s + (tee_local $5 (get_local $4) + ) + (i32.lt_s + (get_local $2) (get_local $5) ) ) @@ -4157,64 +4158,64 @@ ) ) (if - (i32.eq - (get_local $0) - (i32.const 0) - ) - (return - (i32.eq - (get_local $1) - (i32.const 0) + (i32.and + (if (result i32) + (tee_local $2 + (i32.eq + (get_local $0) + (i32.const 0) + ) + ) + (get_local $2) + (i32.eq + (get_local $1) + (i32.const 0) + ) ) - ) - ) - (if - (i32.eq - (get_local $1) - (i32.const 0) + (i32.const 1) ) (return (i32.const 0) ) ) - (set_local $2 - (i32.load - (get_local $0) - ) - ) (set_local $3 + (i32.load + (get_local $0) + ) + ) + (set_local $4 (i32.load (get_local $1) ) ) + (if + (i32.eqz + (get_local $4) + ) + (return + (i32.eqz + (get_local $3) + ) + ) + ) (if (i32.eqz (get_local $3) ) - (return - (i32.eqz - (get_local $2) - ) - ) - ) - (if - (i32.eqz - (get_local $2) - ) (return (i32.const 1) ) ) (set_local $6 (select - (tee_local $4 - (get_local $2) - ) - (tee_local $5 + (tee_local $2 (get_local $3) ) - (i32.lt_s + (tee_local $5 (get_local $4) + ) + (i32.lt_s + (get_local $2) (get_local $5) ) ) @@ -4256,7 +4257,7 @@ (call $abort (i32.const 0) (i32.const 72) - (i32.const 386) + (i32.const 383) (i32.const 4) ) (unreachable) @@ -4294,7 +4295,7 @@ (call $abort (i32.const 0) (i32.const 72) - (i32.const 391) + (i32.const 388) (i32.const 6) ) (unreachable)