Skip to content

Commit 426feeb

Browse files
committed
use vector compares in more places
1 parent 8e69b06 commit 426feeb

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

ember_json/reader.mojo

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ from sys.intrinsics import likely
55

66
alias Bytes = String._buffer_type
77
alias Byte = UInt8
8+
alias ByteVec = SIMD[DType.uint8, _]
89

910

1011
@always_inline
1112
fn to_byte(s: String) -> Byte:
1213
return Byte(ord(s))
1314

14-
15+
var spaces = ByteVec[8](SPACE, NEWLINE, TAB, LINE_FEED)
1516
@always_inline
1617
fn is_space(char: Byte) -> Bool:
17-
return char == SPACE or char == NEWLINE or char == TAB or char == LINE_FEED
18-
18+
return char in spaces
1919

2020
@always_inline
2121
fn bytes_to_string[origin: MutableOrigin, //](b: Span[Byte, origin]) -> String:
@@ -83,10 +83,11 @@ struct Reader:
8383

8484
@always_inline
8585
fn read_word(inout self) -> Span[Byte, __origin_of(self._data)]:
86+
var end_chars = ByteVec[4](COMMA, RCURLY, RBRACKET)
8687
@always_inline
8788
@parameter
8889
fn func(c: Byte) -> Bool:
89-
return not is_space(c) and c != COMMA and c != RCURLY and c != RBRACKET
90+
return not is_space(c) and not c in end_chars
9091

9192
return self.read_while[func]()
9293

ember_json/value.mojo

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from .object import Object
22
from .array import Array
3-
from .reader import Reader, Byte, Bytes, bytes_to_string, byte_to_string, compare_bytes, to_byte, compare_simd
3+
from .reader import Reader, Byte, Bytes, bytes_to_string, byte_to_string, compare_bytes, to_byte, compare_simd, ByteVec
44
from utils import Variant, Span
55
from .constants import *
66
from sys.intrinsics import unlikely
@@ -24,8 +24,6 @@ struct Null(Stringable, EqualityComparableCollectionElement, Formattable, Repres
2424
fn format_to(self, inout writer: Formatter):
2525
writer.write(self.__str__())
2626

27-
alias CharVec = SIMD[DType.uint8, _]
28-
2927
@always_inline
3028
fn validate_string[origin: MutableOrigin](b: Span[Byte, origin]) raises:
3129
alias SOL = to_byte("/")
@@ -37,8 +35,8 @@ fn validate_string[origin: MutableOrigin](b: Span[Byte, origin]) raises:
3735
alias U = to_byte("u")
3836

3937
# can't be alias for some reason
40-
var acceptable_escapes = CharVec[16](QUOTE, RSOL, SOL, B, F, N, R, T, U)
41-
var control_chars = CharVec[4](NEWLINE, TAB, LINE_FEED)
38+
var acceptable_escapes = ByteVec[16](QUOTE, RSOL, SOL, B, F, N, R, T, U)
39+
var control_chars = ByteVec[4](NEWLINE, TAB, LINE_FEED)
4240
i = 0
4341
while i < len(b):
4442
var char = b[i]
@@ -75,7 +73,7 @@ var NULL = SIMD[DType.uint8, 4](to_byte("n"), to_byte("u"), to_byte("l"), to_byt
7573
@always_inline
7674
@parameter
7775
fn is_numerical_component(char: Byte) -> Bool:
78-
var componenents = CharVec[8](DOT, LOW_E, UPPER_E, PLUS, NEG)
76+
var componenents = ByteVec[8](DOT, LOW_E, UPPER_E, PLUS, NEG)
7977
return isdigit(char) or char in componenents
8078

8179
@always_inline
@@ -84,8 +82,8 @@ fn _read_number(inout reader: Reader) raises -> Variant[Int, Float64]:
8482
var is_float = False
8583
var first_digit_found = False
8684
var leading_zero = False
87-
var float_parts = CharVec[4](DOT, LOW_E, UPPER_E)
88-
var sign_parts = CharVec[2](PLUS, NEG)
85+
var float_parts = ByteVec[4](DOT, LOW_E, UPPER_E)
86+
var sign_parts = ByteVec[2](PLUS, NEG)
8987
for i in range(len(num)):
9088
var b = num[i]
9189
if b in float_parts:

0 commit comments

Comments
 (0)