Skip to content
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
73 changes: 73 additions & 0 deletions encoding/ascii/ascii_bench_test.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Copyright 2026 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

///|
let ascii_bench_size = 100_000

///|
let ascii_bench_bytes : Bytes = Bytes::make(ascii_bench_size, b'A')

///|
let ascii_bench_large_size = 1_000_000

///|
let ascii_bench_large_bytes : Bytes = Bytes::make(ascii_bench_large_size, b'A')

///|
let ascii_bench_bytes_15 : Bytes = Bytes::make(15, b'A')

///|
let ascii_bench_bytes_16 : Bytes = Bytes::make(16, b'A')

///|
let ascii_bench_bytes_32 : Bytes = Bytes::make(32, b'A')

///|
let ascii_bench_bytes_64 : Bytes = Bytes::make(64, b'A')

///|
test "bench ASCII decode n=15" (it : @bench.T) {
let bytes = ascii_bench_bytes_15
it.bench(fn() { it.keep((try! @ascii.decode(bytes)).length()) })
}

///|
test "bench ASCII decode n=16" (it : @bench.T) {
let bytes = ascii_bench_bytes_16
it.bench(fn() { it.keep((try! @ascii.decode(bytes)).length()) })
}

///|
test "bench ASCII decode n=32" (it : @bench.T) {
let bytes = ascii_bench_bytes_32
it.bench(fn() { it.keep((try! @ascii.decode(bytes)).length()) })
}

///|
test "bench ASCII decode n=64" (it : @bench.T) {
let bytes = ascii_bench_bytes_64
it.bench(fn() { it.keep((try! @ascii.decode(bytes)).length()) })
}

///|
test "bench ASCII decode n=100000" (it : @bench.T) {
let bytes = ascii_bench_bytes
it.bench(fn() { it.keep((try! @ascii.decode(bytes)).length()) })
}

///|
test "bench ASCII decode n=1000000" (it : @bench.T) {
let bytes = ascii_bench_large_bytes
it.bench(fn() { it.keep((try! @ascii.decode(bytes)).length()) })
}
88 changes: 88 additions & 0 deletions encoding/ascii/decode.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ pub suberror Malformed {
///|
fn unsafe_fixedarray_uint16_to_string(buffer : FixedArray[UInt16]) -> String = "%string.unsafe_from_uint16_fixedarray"

///|
#cfg(target="js")
#warnings("-unused_value")
fn suppress_unused_v128_import_on_js() -> Unit {
ignore(@v128.i8x16_splat(0))
}

///|
#cfg(not(target="js"))
fn unsafe_fixedarray_from_bytes(bytes : Bytes) -> FixedArray[Byte] = "%identity"

///|
fn finish_string(buffer : FixedArray[UInt16], len : Int) -> String {
if len == buffer.length() {
Expand All @@ -32,10 +43,87 @@ fn finish_string(buffer : FixedArray[UInt16], len : Int) -> String {
}
}

///|
#cfg(not(target="js"))
#inline
fn decode_scalar(bytes : BytesView) -> String raise Malformed {
let t : FixedArray[UInt16] = FixedArray::make(bytes.length(), 0)
let tlen = for tlen = 0, bs = bytes {
match (tlen, bs) {
(tlen, []) => break tlen
(tlen, [0..=0x7F as b, .. rest]) => {
t.unsafe_set(tlen, b.to_uint16())
continue tlen + 1, rest
}
(_, _ as bytes) => raise Malformed(bytes)
}
}
finish_string(t, tlen)
}

///|
#cfg(not(target="js"))
fn decode_v128(bytes : BytesView) -> String raise Malformed {
let length = bytes.length()
let result = FixedArray::make(length * 2, b'\x00')
let source = unsafe_fixedarray_from_bytes(bytes.data())
let source_start = bytes.start_offset()
let source_end = source_start + length
let mut source_offset = source_start
let mut result_offset = 0
while source_offset + 16 <= source_end {
let block = @v128.v128_load(source, source_offset)
let non_ascii = @v128.i8x16_bitmask(block)
if non_ascii != 0 {
let malformed_offset = source_offset - source_start + non_ascii.ctz()
raise Malformed(bytes[malformed_offset:])
}
@v128.v128_store(
result,
result_offset,
@v128.i16x8_extend_low_i8x16_u(block),
)
@v128.v128_store(
result,
result_offset + 16,
@v128.i16x8_extend_high_i8x16_u(block),
)
source_offset = source_offset + 16
result_offset = result_offset + 32
}
while source_offset < source_end {
let byte = source.unsafe_get(source_offset)
if byte > b'\x7F' {
raise Malformed(bytes[source_offset - source_start:])
}
result.unsafe_set(result_offset, byte)
result.unsafe_set(result_offset + 1, b'\x00')
source_offset = source_offset + 1
result_offset = result_offset + 2
}
result.unsafe_reinterpret_as_bytes().to_unchecked_string()
}

///|
/// Decodes an ASCII byte array into a string.
///
/// Raises `Malformed` if any byte is outside the ASCII range.
#cfg(not(target="js"))
#inline
pub fn decode(bytes : BytesView) -> String raise Malformed {
// The scalar loop is faster below this crossover point on supported targets.
if bytes.length() >= 64 {
decode_v128(bytes)
} else {
decode_scalar(bytes)
}
}

///|
/// Decodes an ASCII byte array into a string.
///
/// Raises `Malformed` if any byte is outside the ASCII range.
#cfg(target="js")
pub fn decode(bytes : BytesView) -> String raise Malformed {
let t : FixedArray[UInt16] = FixedArray::make(bytes.length(), 0)
let tlen = for tlen = 0, bs = bytes {
Expand Down
79 changes: 79 additions & 0 deletions encoding/ascii/decode_v128_wbtest.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright 2026 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

///|
fn v128_outcome(view : BytesView) -> (String?, Bytes?) {
(Some(decode_v128(view)), None) catch {
Malformed(rest) => (None, Some(rest.to_owned()))
}
}

///|
fn scalar_outcome(view : BytesView) -> (String?, Bytes?) {
(Some(decode_scalar(view)), None) catch {
Malformed(rest) => (None, Some(rest.to_owned()))
}
}

///|
/// The SIMD decoder must agree with the scalar decoder — same string on
/// success, same remaining view on failure — on arbitrary payloads seen
/// through views with arbitrary (unaligned) start offsets.
test "quickcheck: V128 decode agrees with scalar decode" {
@quickcheck.check(count=300, (input : (Array[Int], Int, Bool)) => {
let (seeds, pad_seed, all_valid) = input
let payload = seeds.map(seed => {
if all_valid || (seed & 0xF) != 0xF {
(seed & 0x7F).to_byte()
} else {
(0x80 | (seed & 0x7F)).to_byte()
}
})
let pad = pad_seed % 17
let pad = if pad < 0 { pad + 17 } else { pad }
let full : Array[Byte] = []
for _ in 0..<pad {
full.push(b'\xFF')
}
full.append(payload)
for _ in 0..<16 {
full.push(b'\xFF')
}
let view = Bytes::from_array(full)[pad:pad + payload.length()]
v128_outcome(view) == scalar_outcome(view)
})
}

///|
test "V128 decode handles a view and scalar tail" {
let bytes = b"!0123456789ABCDEF?"
inspect(decode_v128(bytes[1:]), content="0123456789ABCDEF?")
}

///|
test "V128 decode reports the first non-ASCII byte in a block" {
let bytes = b"0123456789ABC\xffDEF"
try {
let _ = decode_v128(bytes)
panic()
} catch {
Malformed(rest) =>
inspect(
rest,
content=(
#|b"\xffDEF"
),
)
}
}
41 changes: 41 additions & 0 deletions encoding/ascii/finish_string_wbtest.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright 2026 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

///|
/// Every decoder finishes through `finish_string`; it must keep exactly the
/// first `len` code units, both when the buffer is full and when it must
/// truncate.
test "quickcheck: finish_string keeps exactly the first len code units" {
@quickcheck.check(count=100, (input : (Array[Int], Int)) => {
let units = input.0.map(x => (x & 0x7F).to_uint16())
let buffer = FixedArray::makei(units.length(), i => units[i])
let take = if units.is_empty() {
0
} else {
let r = input.1 % (units.length() + 1)
if r < 0 {
r + units.length() + 1
} else {
r
}
}
let expected = StringBuilder(size_hint=take)
for i in 0..<take {
expected.write_char(
units[i].to_uint().reinterpret_as_int().unsafe_to_char(),
)
}
finish_string(buffer, take) == expected.to_string()
})
}
14 changes: 14 additions & 0 deletions encoding/ascii/moon.pkg
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
import {
"moonbitlang/core/builtin",
"moonbitlang/core/debug",
"moonbitlang/core/v128",
}

import {
"moonbitlang/core/bench",
"moonbitlang/core/quickcheck",
} for "test"

import {
"moonbitlang/core/quickcheck",
} for "wbtest"

options(
targets: { "decode_v128_wbtest.mbt": [ "not", "js" ] },
)
Loading
Loading