Skip to content

Commit d0aa097

Browse files
committed
Remove initial set_len in stage2
Signed-off-by: Heinz N. Gies <heinz@licenser.net>
1 parent d426844 commit d0aa097

File tree

2 files changed

+36
-48
lines changed

2 files changed

+36
-48
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ proptest-regressions
66
.baseline
77
.current
88
.cargo
9+
.vscode

src/stage2.rs

Lines changed: 35 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,6 @@ macro_rules! get {
3939
}};
4040
}
4141

42-
#[cfg(feature = "safe")]
43-
macro_rules! get_mut {
44-
($a:expr, $i:expr) => {
45-
&mut $a[$i]
46-
};
47-
}
48-
49-
#[cfg(not(feature = "safe"))]
50-
macro_rules! get_mut {
51-
($a:expr, $i:expr) => {{
52-
unsafe { $a.get_unchecked_mut($i) }
53-
}};
54-
}
55-
5642
#[cfg_attr(not(feature = "no-inline"), inline(always))]
5743
#[allow(clippy::cast_ptr_alignment, unused_unsafe)]
5844
pub fn is_valid_false_atom(loc: &[u8]) -> bool {
@@ -126,10 +112,6 @@ impl<'de> Deserializer<'de> {
126112
// a invalid json might exceed this `[[[[[[` and we need to protect against that.
127113
let mut res: Vec<Node<'de>> = Vec::with_capacity(structural_indexes.len());
128114
let mut stack = Vec::with_capacity(structural_indexes.len());
129-
unsafe {
130-
stack.set_len(structural_indexes.len());
131-
res.set_len(structural_indexes.len());
132-
}
133115

134116
let mut depth: usize = 0;
135117
let mut last_start = 1;
@@ -166,7 +148,7 @@ impl<'de> Deserializer<'de> {
166148
macro_rules! insert_res {
167149
($t:expr) => {
168150
unsafe {
169-
std::ptr::write(get_mut!(res, r_i), $t);
151+
res.as_mut_ptr().add(r_i).write($t);
170152
r_i += 1;
171153
}
172154
};
@@ -175,8 +157,8 @@ impl<'de> Deserializer<'de> {
175157
() => {
176158
unsafe {
177159
res.set_len(r_i);
178-
return Ok(res);
179160
}
161+
return Ok(res);
180162
};
181163
}
182164
macro_rules! update_char {
@@ -308,7 +290,10 @@ impl<'de> Deserializer<'de> {
308290
update_char!();
309291
match c {
310292
b'{' => {
311-
*get_mut!(stack, depth) = (StackState::Start, last_start, cnt);
293+
unsafe {
294+
let s: *mut (StackState, usize, usize) = stack.as_mut_ptr();
295+
s.add(depth).write((StackState::Start, last_start, cnt));
296+
}
312297

313298
last_start = r_i;
314299
insert_res!(Node::Object(0, 0));
@@ -332,7 +317,10 @@ impl<'de> Deserializer<'de> {
332317
}
333318
}
334319
b'[' => {
335-
*get_mut!(stack, depth) = (StackState::Start, last_start, cnt);
320+
unsafe {
321+
let s: *mut (StackState, usize, usize) = stack.as_mut_ptr();
322+
s.add(depth).write((StackState::Start, last_start, cnt));
323+
}
336324

337325
last_start = r_i;
338326
insert_res!(Node::Array(0, 0));
@@ -474,8 +462,8 @@ impl<'de> Deserializer<'de> {
474462
}
475463
b'{' => {
476464
unsafe {
477-
*stack.get_unchecked_mut(depth) =
478-
(StackState::Object, last_start, cnt);
465+
let s: *mut (StackState, usize, usize) = stack.as_mut_ptr();
466+
s.add(depth).write((StackState::Object, last_start, cnt));
479467
}
480468
last_start = r_i;
481469
insert_res!(Node::Object(0, 0));
@@ -485,8 +473,8 @@ impl<'de> Deserializer<'de> {
485473
}
486474
b'[' => {
487475
unsafe {
488-
*stack.get_unchecked_mut(depth) =
489-
(StackState::Object, last_start, cnt);
476+
let s: *mut (StackState, usize, usize) = stack.as_mut_ptr();
477+
s.add(depth).write((StackState::Object, last_start, cnt));
490478
}
491479
last_start = r_i;
492480
insert_res!(Node::Array(0, 0));
@@ -506,7 +494,7 @@ impl<'de> Deserializer<'de> {
506494
}
507495
depth -= 1;
508496
unsafe {
509-
match res.get_unchecked_mut(last_start) {
497+
match *res.as_mut_ptr().add(last_start) {
510498
Node::Array(ref mut len, ref mut end)
511499
| Node::Object(ref mut len, ref mut end) => {
512500
*len = cnt;
@@ -515,24 +503,23 @@ impl<'de> Deserializer<'de> {
515503
_ => unreachable!(),
516504
};
517505
}
518-
519-
let (a_state, a_last_start, a_cnt) = unsafe { stack.get_unchecked(depth) };
520-
// let (a_state, a_last_start, a_cnt) = unsafe { };
521-
//s2try!(stack.pop().ok_or_else(|| Error::generic(ErrorType::Syntax)));
522-
523-
last_start = *a_last_start;
524-
cnt = *a_cnt;
525-
526-
match &a_state {
527-
StackState::Object => object_continue!(),
528-
StackState::Array => array_continue!(),
529-
StackState::Start => {
530-
if i == structural_indexes.len() {
531-
success!();
506+
unsafe {
507+
let a = stack.as_ptr().add(depth);
508+
509+
last_start = (*a).1;
510+
cnt = (*a).2;
511+
512+
match (*a).0 {
513+
StackState::Object => object_continue!(),
514+
StackState::Array => array_continue!(),
515+
StackState::Start => {
516+
if i == structural_indexes.len() {
517+
success!();
518+
}
519+
fail!();
532520
}
533-
fail!();
534-
}
535-
};
521+
};
522+
}
536523
}
537524

538525
////////////////////////////// ARRAY STATES /////////////////////////////
@@ -583,8 +570,8 @@ impl<'de> Deserializer<'de> {
583570
}
584571
b'{' => {
585572
unsafe {
586-
*stack.get_unchecked_mut(depth) =
587-
(StackState::Array, last_start, cnt);
573+
let s: *mut (StackState, usize, usize) = stack.as_mut_ptr();
574+
s.add(depth).write((StackState::Array, last_start, cnt));
588575
}
589576
last_start = r_i;
590577
insert_res!(Node::Object(0, 0));
@@ -594,8 +581,8 @@ impl<'de> Deserializer<'de> {
594581
}
595582
b'[' => {
596583
unsafe {
597-
*stack.get_unchecked_mut(depth) =
598-
(StackState::Array, last_start, cnt);
584+
let s: *mut (StackState, usize, usize) = stack.as_mut_ptr();
585+
s.add(depth).write((StackState::Array, last_start, cnt));
599586
}
600587
last_start = r_i;
601588
insert_res!(Node::Array(0, 0));

0 commit comments

Comments
 (0)