Skip to content

Optimize String#substring. Fix String#substr. Add tests #979

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions std/assembly/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,12 @@ import { idof } from "./builtins";
substr(start: i32, length: i32 = i32.MAX_VALUE): String { // legacy
var intStart: isize = start;
var end: isize = length;
var size: isize = this.length;
if (intStart < 0) intStart = max(size + intStart, 0);
var resultLength = min(max(end, 0), size - intStart);
if (resultLength <= 0) return changetype<String>("");
var out = __alloc(resultLength << 1, idof<String>());
memory.copy(out, changetype<usize>(this) + intStart, resultLength);
var len: isize = this.length;
if (intStart < 0) intStart = max(len + intStart, 0);
var size = min(max(end, 0), len - intStart) << 1;
if (size <= 0) return changetype<String>("");
var out = __alloc(size, idof<String>());
memory.copy(out, changetype<usize>(this) + (intStart << 1), size);
return changetype<String>(out); // retains
}

Expand All @@ -189,17 +189,17 @@ import { idof } from "./builtins";
var finalEnd = min<isize>(max(end, 0), len);
var fromPos = min<isize>(finalStart, finalEnd) << 1;
var toPos = max<isize>(finalStart, finalEnd) << 1;
len = toPos - fromPos;
if (!len) return changetype<String>("");
if (!fromPos && toPos == this.length << 1) return this;
var out = __alloc(len, idof<String>());
memory.copy(out, changetype<usize>(this) + fromPos, len);
var size = toPos - fromPos;
if (!size) return changetype<String>("");
if (!fromPos && toPos == len << 1) return this;
var out = __alloc(size, idof<String>());
memory.copy(out, changetype<usize>(this) + fromPos, size);
return changetype<String>(out); // retains
}

trim(): String {
var length = this.length;
var size: usize = length << 1;
var len = this.length;
var size: usize = len << 1;
while (size && isSpace(load<u16>(changetype<usize>(this) + size - 2))) {
size -= 2;
}
Expand All @@ -208,7 +208,7 @@ import { idof } from "./builtins";
offset += 2; size -= 2;
}
if (!size) return changetype<String>("");
if (!offset && size == length << 1) return this;
if (!offset && size == len << 1) return this;
var out = __alloc(size, idof<String>());
memory.copy(out, changetype<usize>(this) + offset, size);
return changetype<String>(out); // retains
Expand Down
44 changes: 21 additions & 23 deletions tests/compiler/number.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -1360,6 +1360,7 @@
(func $~lib/string/String#substring (; 13 ;) (type $FUNCSIG$iii) (param $0 i32) (param $1 i32) (result i32)
(local $2 i32)
(local $3 i32)
(local $4 i32)
i32.const 0
local.get $0
call $~lib/string/String#get:length
Expand All @@ -1381,54 +1382,51 @@
local.get $2
i32.lt_s
select
local.tee $2
local.tee $1
local.get $3
local.get $2
local.get $1
i32.gt_s
select
i32.const 1
i32.shl
local.tee $1
local.tee $4
local.get $3
local.get $2
local.get $1
local.get $3
local.get $2
local.get $1
i32.lt_s
select
i32.const 1
i32.shl
local.tee $3
local.tee $1
i32.sub
local.tee $2
local.tee $3
i32.eqz
if
i32.const 1288
return
end
local.get $3
if (result i32)
i32.const 0
else
local.get $0
call $~lib/string/String#get:length
i32.const 1
i32.shl
local.get $1
i32.eq
end
i32.const 0
local.get $2
i32.const 1
i32.shl
local.get $4
i32.eq
local.get $1
select
if
local.get $0
return
end
local.get $2
local.get $3
call $~lib/rt/stub/__alloc
local.tee $1
local.tee $2
local.get $0
local.get $3
local.get $1
i32.add
local.get $2
local.get $3
call $~lib/memory/memory.copy
local.get $1
local.get $2
)
(func $~lib/rt/stub/__free (; 14 ;) (type $FUNCSIG$vi) (param $0 i32)
(local $1 i32)
Expand Down
18 changes: 9 additions & 9 deletions tests/compiler/number.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -3171,6 +3171,7 @@
(local $8 i32)
(local $9 i32)
(local $10 i32)
(local $11 i32)
local.get $0
call $~lib/string/String#get:length
local.set $3
Expand Down Expand Up @@ -3231,8 +3232,8 @@
local.get $9
local.get $8
i32.sub
local.set $3
local.get $3
local.set $10
local.get $10
i32.eqz
if
i32.const 1736
Expand All @@ -3243,8 +3244,7 @@
i32.eqz
if (result i32)
local.get $9
local.get $0
call $~lib/string/String#get:length
local.get $3
i32.const 1
i32.shl
i32.eq
Expand All @@ -3256,17 +3256,17 @@
call $~lib/rt/stub/__retain
return
end
local.get $3
local.get $10
i32.const 1
call $~lib/rt/stub/__alloc
local.set $10
local.get $10
local.set $11
local.get $11
local.get $0
local.get $8
i32.add
local.get $3
call $~lib/memory/memory.copy
local.get $10
call $~lib/memory/memory.copy
local.get $11
call $~lib/rt/stub/__retain
)
(func $~lib/rt/stub/__free (; 21 ;) (type $FUNCSIG$vi) (param $0 i32)
Expand Down
44 changes: 21 additions & 23 deletions tests/compiler/resolve-binary.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -1546,6 +1546,7 @@
(func $~lib/string/String#substring (; 15 ;) (type $FUNCSIG$iii) (param $0 i32) (param $1 i32) (result i32)
(local $2 i32)
(local $3 i32)
(local $4 i32)
i32.const 0
local.get $0
call $~lib/string/String#get:length
Expand All @@ -1567,55 +1568,52 @@
local.get $2
i32.lt_s
select
local.tee $2
local.tee $1
local.get $3
local.get $2
local.get $1
i32.gt_s
select
i32.const 1
i32.shl
local.tee $1
local.tee $4
local.get $3
local.get $2
local.get $1
local.get $3
local.get $2
local.get $1
i32.lt_s
select
i32.const 1
i32.shl
local.tee $3
local.tee $1
i32.sub
local.tee $2
local.tee $3
i32.eqz
if
i32.const 1408
return
end
local.get $3
if (result i32)
i32.const 0
else
local.get $0
call $~lib/string/String#get:length
i32.const 1
i32.shl
local.get $1
i32.eq
end
i32.const 0
local.get $2
i32.const 1
i32.shl
local.get $4
i32.eq
local.get $1
select
if
local.get $0
return
end
local.get $2
local.get $3
i32.const 1
call $~lib/rt/stub/__alloc
local.tee $1
local.tee $2
local.get $0
local.get $3
local.get $1
i32.add
local.get $2
local.get $3
call $~lib/memory/memory.copy
local.get $1
local.get $2
)
(func $~lib/rt/stub/__free (; 16 ;) (type $FUNCSIG$vi) (param $0 i32)
(local $1 i32)
Expand Down
18 changes: 9 additions & 9 deletions tests/compiler/resolve-binary.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -4366,6 +4366,7 @@
(local $8 i32)
(local $9 i32)
(local $10 i32)
(local $11 i32)
local.get $0
call $~lib/string/String#get:length
local.set $3
Expand Down Expand Up @@ -4426,8 +4427,8 @@
local.get $9
local.get $8
i32.sub
local.set $3
local.get $3
local.set $10
local.get $10
i32.eqz
if
i32.const 1856
Expand All @@ -4438,8 +4439,7 @@
i32.eqz
if (result i32)
local.get $9
local.get $0
call $~lib/string/String#get:length
local.get $3
i32.const 1
i32.shl
i32.eq
Expand All @@ -4451,17 +4451,17 @@
call $~lib/rt/stub/__retain
return
end
local.get $3
local.get $10
i32.const 1
call $~lib/rt/stub/__alloc
local.set $10
local.get $10
local.set $11
local.get $11
local.get $0
local.get $8
i32.add
local.get $3
call $~lib/memory/memory.copy
local.get $10
call $~lib/memory/memory.copy
local.get $11
call $~lib/rt/stub/__retain
)
(func $~lib/rt/stub/__free (; 24 ;) (type $FUNCSIG$vi) (param $0 i32)
Expand Down
Loading