|
| 1 | +// Copyright 2026 International Digital Economy Academy |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +///| |
| 16 | +let utf8_is_valid_bench_size = 4096 |
| 17 | + |
| 18 | +///| |
| 19 | +fn utf8_is_valid_ascii_bytes() -> Bytes { |
| 20 | + Bytes::makei(utf8_is_valid_bench_size, i => (0x20 + i % 0x5F).to_byte()) |
| 21 | +} |
| 22 | + |
| 23 | +///| |
| 24 | +fn utf8_is_valid_mixed_bytes() -> Bytes { |
| 25 | + Bytes::makei(utf8_is_valid_bench_size, i => { |
| 26 | + match i % 8 { |
| 27 | + 0 => b'\xC3' |
| 28 | + 1 => b'\xA9' |
| 29 | + 2 => b'\xE4' |
| 30 | + 3 => b'\xB8' |
| 31 | + 4 => b'\xAD' |
| 32 | + _ => (0x41 + i % 26).to_byte() |
| 33 | + } |
| 34 | + }) |
| 35 | +} |
| 36 | + |
| 37 | +///| |
| 38 | +test "bench utf8.is_valid ascii n=4096" (it : @bench.T) { |
| 39 | + let bytes = utf8_is_valid_ascii_bytes() |
| 40 | + it.bench(fn() { it.keep(@utf8.is_valid(bytes)) }) |
| 41 | +} |
| 42 | + |
| 43 | +///| |
| 44 | +test "bench utf8.decode ascii n=4096" (it : @bench.T) { |
| 45 | + let bytes = utf8_is_valid_ascii_bytes() |
| 46 | + it.bench(fn() { it.keep(try! @utf8.decode(bytes)) }) |
| 47 | +} |
| 48 | + |
| 49 | +///| |
| 50 | +test "bench utf8.is_valid mixed n=4096" (it : @bench.T) { |
| 51 | + let bytes = utf8_is_valid_mixed_bytes() |
| 52 | + it.bench(fn() { it.keep(@utf8.is_valid(bytes)) }) |
| 53 | +} |
| 54 | + |
| 55 | +///| |
| 56 | +test "bench utf8.decode mixed n=4096" (it : @bench.T) { |
| 57 | + let bytes = utf8_is_valid_mixed_bytes() |
| 58 | + it.bench(fn() { it.keep(try! @utf8.decode(bytes)) }) |
| 59 | +} |
0 commit comments