Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 0e398c4

Browse files
authoredJun 14, 2020
refactor: Rename dtoa/itoa_stream to dtoa/itoa_buffered & simplify them (#1339)
BREAKING CHANGE: The internal `dtoa/itoa_stream` helpers (unsafe `Number#toString`) in `util/number` have been renamed to `dtoa/itoa_buffered`, don't take a redundant offset argument anymore, and the underlying core implementation helpers are no longer exposed. Usage now is: `dtoa_buffered(buffer + byteOffset, value)`
1 parent e5bfe15 commit 0e398c4

File tree

9 files changed

+851
-847
lines changed

9 files changed

+851
-847
lines changed
 

‎std/assembly/util/number.ts

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ export function utoa32_dec_core(buffer: usize, num: u32, offset: usize): void {
290290

291291
// @ts-ignore: decorator
292292
@inline
293-
export function utoa32_hex_core(buffer: usize, num: u32, offset: usize): void {
293+
function utoa32_hex_core(buffer: usize, num: u32, offset: usize): void {
294294
if (ASC_SHRINK_LEVEL >= 1) {
295295
utoa_hex_simple<u32>(buffer, num, offset);
296296
} else {
@@ -300,7 +300,7 @@ export function utoa32_hex_core(buffer: usize, num: u32, offset: usize): void {
300300

301301
// @ts-ignore: decorator
302302
@inline
303-
export function utoa64_dec_core(buffer: usize, num: u64, offset: usize): void {
303+
function utoa64_dec_core(buffer: usize, num: u64, offset: usize): void {
304304
if (ASC_SHRINK_LEVEL >= 1) {
305305
utoa_dec_simple<u64>(buffer, num, offset);
306306
} else {
@@ -310,15 +310,15 @@ export function utoa64_dec_core(buffer: usize, num: u64, offset: usize): void {
310310

311311
// @ts-ignore: decorator
312312
@inline
313-
export function utoa64_hex_core(buffer: usize, num: u64, offset: usize): void {
313+
function utoa64_hex_core(buffer: usize, num: u64, offset: usize): void {
314314
if (ASC_SHRINK_LEVEL >= 1) {
315315
utoa_hex_simple<u64>(buffer, num, offset);
316316
} else {
317317
utoa_hex_lut(buffer, num, offset);
318318
}
319319
}
320320

321-
export function utoa64_any_core(buffer: usize, num: u64, offset: usize, radix: i32): void {
321+
function utoa64_any_core(buffer: usize, num: u64, offset: usize, radix: i32): void {
322322
const lut = changetype<usize>(ANY_DIGITS);
323323
var base = u64(radix);
324324
if ((radix & (radix - 1)) == 0) { // for radix which pow of two
@@ -710,7 +710,7 @@ function prettify(buffer: usize, length: i32, k: i32): i32 {
710710
}
711711
}
712712

713-
export function dtoa_core(buffer: usize, value: f64): i32 {
713+
function dtoa_core(buffer: usize, value: f64): i32 {
714714
var sign = i32(value < 0);
715715
if (sign) {
716716
value = -value;
@@ -736,8 +736,7 @@ export function dtoa(value: f64): String {
736736
return result;
737737
}
738738

739-
export function itoa_stream<T extends number>(buffer: usize, offset: usize, value: T): u32 {
740-
buffer += offset << 1;
739+
export function itoa_buffered<T extends number>(buffer: usize, value: T): u32 {
741740
var sign: u32 = 0;
742741
if (isSigned<T>()) {
743742
sign = u32(value < 0);
@@ -783,8 +782,7 @@ export function itoa_stream<T extends number>(buffer: usize, offset: usize, valu
783782
return decimals;
784783
}
785784

786-
export function dtoa_stream(buffer: usize, offset: usize, value: f64): u32 {
787-
buffer += offset << 1;
785+
export function dtoa_buffered(buffer: usize, value: f64): u32 {
788786
if (value == 0) {
789787
store<u16>(buffer, CharCode._0);
790788
store<u16>(buffer, CharCode.DOT, 2);

‎std/assembly/util/string.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { itoa32, utoa32, itoa64, utoa64, dtoa, itoa_stream, dtoa_stream, MAX_DOUBLE_LENGTH } from "./number";
1+
import { itoa32, utoa32, itoa64, utoa64, dtoa, itoa_buffered, dtoa_buffered, MAX_DOUBLE_LENGTH } from "./number";
22
import { ipow32 } from "../math";
33

44
// All tables are stored as two staged lookup tables (static tries)
@@ -889,7 +889,7 @@ export function joinIntegerArray<T>(dataStart: usize, length: i32, separator: st
889889
for (let i = 0; i < lastIndex; ++i) {
890890
value = load<T>(dataStart + (<usize>i << alignof<T>()));
891891
// @ts-ignore: type
892-
offset += itoa_stream<T>(changetype<usize>(result), offset, value);
892+
offset += itoa_buffered<T>(changetype<usize>(result) + (<usize>offset << 1), value);
893893
if (sepLen) {
894894
memory.copy(
895895
changetype<usize>(result) + (<usize>offset << 1),
@@ -901,7 +901,7 @@ export function joinIntegerArray<T>(dataStart: usize, length: i32, separator: st
901901
}
902902
value = load<T>(dataStart + (<usize>lastIndex << alignof<T>()));
903903
// @ts-ignore: type
904-
offset += itoa_stream<T>(changetype<usize>(result), offset, value);
904+
offset += itoa_buffered<T>(changetype<usize>(result) + (<usize>offset << 1), value);
905905
if (estLen > offset) return result.substring(0, offset);
906906
return result;
907907
}
@@ -924,10 +924,8 @@ export function joinFloatArray<T>(dataStart: usize, length: i32, separator: stri
924924
var value: T;
925925
for (let i = 0; i < lastIndex; ++i) {
926926
value = load<T>(dataStart + (<usize>i << alignof<T>()));
927-
offset += dtoa_stream(changetype<usize>(result), offset,
928-
// @ts-ignore: type
929-
value
930-
);
927+
// @ts-ignore: type
928+
offset += dtoa_buffered(changetype<usize>(result) + (<usize>offset << 1), value);
931929
if (sepLen) {
932930
memory.copy(
933931
changetype<usize>(result) + (<usize>offset << 1),
@@ -938,10 +936,8 @@ export function joinFloatArray<T>(dataStart: usize, length: i32, separator: stri
938936
}
939937
}
940938
value = load<T>(dataStart + (<usize>lastIndex << alignof<T>()));
941-
offset += dtoa_stream(changetype<usize>(result), offset,
942-
// @ts-ignore: type
943-
value
944-
);
939+
// @ts-ignore: type
940+
offset += dtoa_buffered(changetype<usize>(result) + (<usize>offset << 1), value);
945941
if (estLen > offset) return result.substring(0, offset);
946942
return result;
947943
}

‎std/assembly/wasi/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
import {
99
MAX_DOUBLE_LENGTH,
1010
decimalCount32,
11-
dtoa_stream
11+
dtoa_buffered
1212
} from "util/number";
1313

1414
// @ts-ignore: decorator
@@ -81,19 +81,19 @@ function trace( // eslint-disable-line @typescript-eslint/no-unused-vars
8181
fd_write(2, iovPtr, 1, lenPtr);
8282
if (n) {
8383
store<u8>(bufPtr++, 0x20); // space
84-
changetype<iovec>(iovPtr).buf_len = 1 + String.UTF8.encodeUnsafe(bufPtr, dtoa_stream(bufPtr, 0, a0), bufPtr);
84+
changetype<iovec>(iovPtr).buf_len = 1 + String.UTF8.encodeUnsafe(bufPtr, dtoa_buffered(bufPtr, a0), bufPtr);
8585
fd_write(2, iovPtr, 1, lenPtr);
8686
if (n > 1) {
87-
changetype<iovec>(iovPtr).buf_len = 1 + String.UTF8.encodeUnsafe(bufPtr, dtoa_stream(bufPtr, 0, a1), bufPtr);
87+
changetype<iovec>(iovPtr).buf_len = 1 + String.UTF8.encodeUnsafe(bufPtr, dtoa_buffered(bufPtr, a1), bufPtr);
8888
fd_write(2, iovPtr, 1, lenPtr);
8989
if (n > 2) {
90-
changetype<iovec>(iovPtr).buf_len = 1 + String.UTF8.encodeUnsafe(bufPtr, dtoa_stream(bufPtr, 0, a2), bufPtr);
90+
changetype<iovec>(iovPtr).buf_len = 1 + String.UTF8.encodeUnsafe(bufPtr, dtoa_buffered(bufPtr, a2), bufPtr);
9191
fd_write(2, iovPtr, 1, lenPtr);
9292
if (n > 3) {
93-
changetype<iovec>(iovPtr).buf_len = 1 + String.UTF8.encodeUnsafe(bufPtr, dtoa_stream(bufPtr, 0, a3), bufPtr);
93+
changetype<iovec>(iovPtr).buf_len = 1 + String.UTF8.encodeUnsafe(bufPtr, dtoa_buffered(bufPtr, a3), bufPtr);
9494
fd_write(2, iovPtr, 1, lenPtr);
9595
if (n > 4) {
96-
changetype<iovec>(iovPtr).buf_len = 1 + String.UTF8.encodeUnsafe(bufPtr, dtoa_stream(bufPtr, 0, a4), bufPtr);
96+
changetype<iovec>(iovPtr).buf_len = 1 + String.UTF8.encodeUnsafe(bufPtr, dtoa_buffered(bufPtr, a4), bufPtr);
9797
fd_write(2, iovPtr, 1, lenPtr);
9898
}
9999
}

‎tests/compiler/std/array.optimized.wat

Lines changed: 143 additions & 137 deletions
Large diffs are not rendered by default.

‎tests/compiler/std/array.untouched.wat

Lines changed: 228 additions & 229 deletions
Large diffs are not rendered by default.

‎tests/compiler/std/typedarray.optimized.wat

Lines changed: 170 additions & 158 deletions
Large diffs are not rendered by default.

‎tests/compiler/std/typedarray.untouched.wat

Lines changed: 267 additions & 262 deletions
Large diffs are not rendered by default.

‎tests/compiler/wasi/trace.optimized.wat

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1542,7 +1542,7 @@
15421542
local.get $8
15431543
i32.add
15441544
)
1545-
(func $~lib/util/number/dtoa_stream (param $0 i32) (param $1 f64) (result i32)
1545+
(func $~lib/util/number/dtoa_buffered (param $0 i32) (param $1 f64) (result i32)
15461546
(local $2 i32)
15471547
local.get $1
15481548
f64.const 0
@@ -1773,7 +1773,7 @@
17731773
i32.add
17741774
local.tee $6
17751775
local.get $1
1776-
call $~lib/util/number/dtoa_stream
1776+
call $~lib/util/number/dtoa_buffered
17771777
local.set $9
17781778
local.get $7
17791779
local.get $6
@@ -1797,7 +1797,7 @@
17971797
local.get $6
17981798
local.get $6
17991799
local.get $2
1800-
call $~lib/util/number/dtoa_stream
1800+
call $~lib/util/number/dtoa_buffered
18011801
local.get $6
18021802
call $~lib/string/String.UTF8.encodeUnsafe
18031803
i32.const 1
@@ -1817,7 +1817,7 @@
18171817
local.get $6
18181818
local.get $6
18191819
local.get $3
1820-
call $~lib/util/number/dtoa_stream
1820+
call $~lib/util/number/dtoa_buffered
18211821
local.get $6
18221822
call $~lib/string/String.UTF8.encodeUnsafe
18231823
i32.const 1
@@ -1837,7 +1837,7 @@
18371837
local.get $6
18381838
local.get $6
18391839
local.get $4
1840-
call $~lib/util/number/dtoa_stream
1840+
call $~lib/util/number/dtoa_buffered
18411841
local.get $6
18421842
call $~lib/string/String.UTF8.encodeUnsafe
18431843
i32.const 1
@@ -1857,7 +1857,7 @@
18571857
local.get $6
18581858
local.get $6
18591859
local.get $5
1860-
call $~lib/util/number/dtoa_stream
1860+
call $~lib/util/number/dtoa_buffered
18611861
local.get $6
18621862
call $~lib/string/String.UTF8.encodeUnsafe
18631863
i32.const 1

‎tests/compiler/wasi/trace.untouched.wat

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,11 @@
55
(type $none_=>_none (func))
66
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
77
(type $i32_i32_i32_i32_=>_i32 (func (param i32 i32 i32 i32) (result i32)))
8+
(type $i32_f64_=>_i32 (func (param i32 f64) (result i32)))
89
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
910
(type $i32_i32_f64_f64_f64_f64_f64_=>_none (func (param i32 i32 f64 f64 f64 f64 f64)))
1011
(type $i32_i32_i32_=>_i32 (func (param i32 i32 i32) (result i32)))
11-
(type $i32_i32_f64_=>_i32 (func (param i32 i32 f64) (result i32)))
1212
(type $i32_i64_i32_i64_i32_i64_i32_=>_i32 (func (param i32 i64 i32 i64 i32 i64 i32) (result i32)))
13-
(type $i32_f64_=>_i32 (func (param i32 f64) (result i32)))
1413
(import "wasi_snapshot_preview1" "fd_write" (func $~lib/bindings/wasi_snapshot_preview1/fd_write (param i32 i32 i32 i32) (result i32)))
1514
(import "wasi_snapshot_preview1" "proc_exit" (func $~lib/bindings/wasi_snapshot_preview1/proc_exit (param i32)))
1615
(memory $0 1)
@@ -3186,15 +3185,9 @@
31863185
local.get $2
31873186
i32.add
31883187
)
3189-
(func $~lib/util/number/dtoa_stream (param $0 i32) (param $1 i32) (param $2 f64) (result i32)
3190-
(local $3 i32)
3191-
local.get $0
3188+
(func $~lib/util/number/dtoa_buffered (param $0 i32) (param $1 f64) (result i32)
3189+
(local $2 i32)
31923190
local.get $1
3193-
i32.const 1
3194-
i32.shl
3195-
i32.add
3196-
local.set $0
3197-
local.get $2
31983191
f64.const 0
31993192
f64.eq
32003193
if
@@ -3210,15 +3203,15 @@
32103203
i32.const 3
32113204
return
32123205
end
3213-
local.get $2
3214-
local.get $2
3206+
local.get $1
3207+
local.get $1
32153208
f64.sub
32163209
f64.const 0
32173210
f64.eq
32183211
i32.eqz
32193212
if
3220-
local.get $2
3221-
local.get $2
3213+
local.get $1
3214+
local.get $1
32223215
f64.ne
32233216
if
32243217
local.get $0
@@ -3233,11 +3226,11 @@
32333226
i32.const 3
32343227
return
32353228
else
3236-
local.get $2
3229+
local.get $1
32373230
f64.const 0
32383231
f64.lt
3239-
local.set $3
3240-
local.get $3
3232+
local.set $2
3233+
local.get $2
32413234
if
32423235
local.get $0
32433236
i32.const 45
@@ -3254,14 +3247,14 @@
32543247
i64.const 34058970405077102
32553248
i64.store offset=8
32563249
i32.const 8
3257-
local.get $3
3250+
local.get $2
32583251
i32.add
32593252
return
32603253
end
32613254
unreachable
32623255
end
32633256
local.get $0
3264-
local.get $2
3257+
local.get $1
32653258
call $~lib/util/number/dtoa_core
32663259
)
32673260
(func $~lib/wasi/index/abort (param $0 i32) (param $1 i32) (param $2 i32) (param $3 i32)
@@ -3563,9 +3556,8 @@
35633556
i32.const 1
35643557
local.get $11
35653558
local.get $11
3566-
i32.const 0
35673559
local.get $2
3568-
call $~lib/util/number/dtoa_stream
3560+
call $~lib/util/number/dtoa_buffered
35693561
local.get $11
35703562
i32.const 0
35713563
call $~lib/string/String.UTF8.encodeUnsafe
@@ -3585,9 +3577,8 @@
35853577
i32.const 1
35863578
local.get $11
35873579
local.get $11
3588-
i32.const 0
35893580
local.get $3
3590-
call $~lib/util/number/dtoa_stream
3581+
call $~lib/util/number/dtoa_buffered
35913582
local.get $11
35923583
i32.const 0
35933584
call $~lib/string/String.UTF8.encodeUnsafe
@@ -3607,9 +3598,8 @@
36073598
i32.const 1
36083599
local.get $11
36093600
local.get $11
3610-
i32.const 0
36113601
local.get $4
3612-
call $~lib/util/number/dtoa_stream
3602+
call $~lib/util/number/dtoa_buffered
36133603
local.get $11
36143604
i32.const 0
36153605
call $~lib/string/String.UTF8.encodeUnsafe
@@ -3629,9 +3619,8 @@
36293619
i32.const 1
36303620
local.get $11
36313621
local.get $11
3632-
i32.const 0
36333622
local.get $5
3634-
call $~lib/util/number/dtoa_stream
3623+
call $~lib/util/number/dtoa_buffered
36353624
local.get $11
36363625
i32.const 0
36373626
call $~lib/string/String.UTF8.encodeUnsafe
@@ -3651,9 +3640,8 @@
36513640
i32.const 1
36523641
local.get $11
36533642
local.get $11
3654-
i32.const 0
36553643
local.get $6
3656-
call $~lib/util/number/dtoa_stream
3644+
call $~lib/util/number/dtoa_buffered
36573645
local.get $11
36583646
i32.const 0
36593647
call $~lib/string/String.UTF8.encodeUnsafe

0 commit comments

Comments
 (0)
Please sign in to comment.