Skip to content

Commit dabd690

Browse files
committed
misc perf improvements and cleanups to libcc2rs
1 parent e35f62a commit dabd690

2 files changed

Lines changed: 49 additions & 64 deletions

File tree

libcc2rs/src/inc.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ impl<T> PostfixInc for Cursor<*mut T> {
1111
#[inline]
1212
fn postfix_inc(&mut self) -> Self {
1313
let clone = self.clone();
14-
self.set_position(self.position() + 1);
14+
self.set_position(self.position().wrapping_add(1));
1515
clone
1616
}
1717
}
@@ -130,26 +130,26 @@ macro_rules! impl_enum_inc_dec {
130130
impl PostfixInc for $t {
131131
fn postfix_inc(&mut self) -> Self {
132132
let copy = *self;
133-
*self = <$t>::from(*self as i32 + 1);
133+
*self = <$t>::from((*self as i32).wrapping_add(1));
134134
copy
135135
}
136136
}
137137
impl PrefixInc for $t {
138138
fn prefix_inc(&mut self) -> Self {
139-
*self = <$t>::from(*self as i32 + 1);
139+
*self = <$t>::from((*self as i32).wrapping_add(1));
140140
*self
141141
}
142142
}
143143
impl PostfixDec for $t {
144144
fn postfix_dec(&mut self) -> Self {
145145
let copy = *self;
146-
*self = <$t>::from(*self as i32 - 1);
146+
*self = <$t>::from((*self as i32).wrapping_sub(1));
147147
copy
148148
}
149149
}
150150
impl PrefixDec for $t {
151151
fn prefix_dec(&mut self) -> Self {
152-
*self = <$t>::from(*self as i32 - 1);
152+
*self = <$t>::from((*self as i32).wrapping_sub(1));
153153
*self
154154
}
155155
}

libcc2rs/src/rc.rs

Lines changed: 44 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ impl<T> Ptr<T> {
241241
fn byte_offset(&self) -> usize {
242242
match &self.kind {
243243
PtrKind::Reinterpreted(_) => self.offset,
244-
_ => self.offset * std::mem::size_of::<T>(),
244+
_ => self.offset.wrapping_mul(std::mem::size_of::<T>()),
245245
}
246246
}
247247

@@ -265,10 +265,7 @@ impl<T> Ptr<T> {
265265
PtrKind::StackArray(ref weak) | PtrKind::HeapArray(ref weak) => {
266266
weak.upgrade().expect("ub: dangling pointer").borrow().len()
267267
}
268-
PtrKind::Reinterpreted(ref data) => {
269-
let step = std::mem::size_of::<T>();
270-
(data.total_byte_len() - self.offset % step) / step
271-
}
268+
PtrKind::Reinterpreted(ref data) => data.total_byte_len() / std::mem::size_of::<T>(),
272269
}
273270
}
274271

@@ -298,7 +295,7 @@ impl<T> Ptr<T> {
298295
kind: self.kind.clone(),
299296
offset: self
300297
.offset
301-
.wrapping_add((offset.wrapping_mul(step as isize)) as usize),
298+
.wrapping_add(offset.wrapping_mul(step as isize) as usize),
302299
}
303300
}
304301

@@ -309,21 +306,17 @@ impl<T> Ptr<T> {
309306

310307
#[inline]
311308
pub fn to_last(&self) -> Self {
312-
let step = self.elem_step();
313-
let base = self.offset % step;
314309
Self {
315310
kind: self.kind.clone(),
316-
offset: base + (self.len() - 1) * step,
311+
offset: self.len().wrapping_sub(1).wrapping_mul(self.elem_step()),
317312
}
318313
}
319314

320315
#[inline]
321316
pub fn to_end(&self) -> Self {
322-
let step = self.elem_step();
323-
let base = self.offset % step;
324317
Self {
325318
kind: self.kind.clone(),
326-
offset: base + self.len() * step,
319+
offset: self.len().wrapping_mul(self.elem_step()),
327320
}
328321
}
329322

@@ -554,46 +547,38 @@ impl<T: Clone> Ptr<T> {
554547
where
555548
F: FnMut(Ptr<T>, Ptr<T>) -> bool,
556549
{
550+
fn sort<T: Clone, F: FnMut(Ptr<T>, Ptr<T>) -> bool>(
551+
slice: &mut [T],
552+
offset: usize,
553+
last: usize,
554+
cmp: &mut F,
555+
) {
556+
slice[offset..last].sort_by(|a, b| {
557+
let val_a = Rc::new(RefCell::new(a.clone()));
558+
let val_b = Rc::new(RefCell::new(b.clone()));
559+
if cmp(val_a.as_pointer(), val_b.as_pointer()) {
560+
std::cmp::Ordering::Less
561+
} else if cmp(val_b.as_pointer(), val_a.as_pointer()) {
562+
std::cmp::Ordering::Greater
563+
} else {
564+
std::cmp::Ordering::Equal
565+
}
566+
});
567+
}
557568
match self.kind {
558569
PtrKind::Null => panic!("ub: dereference of null pointer"),
559570
PtrKind::StackSingle(_) | PtrKind::HeapSingle(_) => {
560571
panic!("only vecs and arrays can be sorted")
561572
}
562573
PtrKind::Vec(ref weak) => {
563574
let strong = weak.upgrade().expect("ub: dangling pointer");
564-
(*strong.borrow_mut())[self.get_offset()..last].sort_by(|a, b| {
565-
if cmp(
566-
Rc::new(RefCell::new(a.clone())).as_pointer(),
567-
Rc::new(RefCell::new(b.clone())).as_pointer(),
568-
) {
569-
std::cmp::Ordering::Less
570-
} else if cmp(
571-
Rc::new(RefCell::new(b.clone())).as_pointer(),
572-
Rc::new(RefCell::new(a.clone())).as_pointer(),
573-
) {
574-
std::cmp::Ordering::Greater
575-
} else {
576-
std::cmp::Ordering::Equal
577-
}
578-
});
575+
let mut borrow = strong.borrow_mut();
576+
sort(&mut borrow, self.get_offset(), last, &mut cmp);
579577
}
580578
PtrKind::StackArray(ref weak) | PtrKind::HeapArray(ref weak) => {
581579
let strong = weak.upgrade().expect("ub: dangling pointer");
582-
(*strong.borrow_mut())[self.get_offset()..last].sort_by(|a, b| {
583-
if cmp(
584-
Rc::new(RefCell::new(a.clone())).as_pointer(),
585-
Rc::new(RefCell::new(b.clone())).as_pointer(),
586-
) {
587-
std::cmp::Ordering::Less
588-
} else if cmp(
589-
Rc::new(RefCell::new(b.clone())).as_pointer(),
590-
Rc::new(RefCell::new(a.clone())).as_pointer(),
591-
) {
592-
std::cmp::Ordering::Greater
593-
} else {
594-
std::cmp::Ordering::Equal
595-
}
596-
});
580+
let mut borrow = strong.borrow_mut();
581+
sort(&mut borrow, self.get_offset(), last, &mut cmp);
597582
}
598583
PtrKind::Reinterpreted(..) => {
599584
panic!("sorting not supported for reinterpreted pointers")
@@ -617,10 +602,9 @@ where
617602
impl<T> Iterator for Ptr<T> {
618603
type Item = Ptr<T>;
619604
fn next(&mut self) -> Option<Self::Item> {
620-
let step = self.elem_step();
621-
if self.offset / step < self.len() {
605+
if self.get_offset() < self.len() {
622606
let value = self.clone();
623-
self.offset += step;
607+
self.offset += self.elem_step();
624608
Some(value)
625609
} else {
626610
None
@@ -672,8 +656,8 @@ pub struct StringIterator<T> {
672656
impl<T> Iterator for StringIterator<T> {
673657
type Item = Ptr<T>;
674658
fn next(&mut self) -> Option<Self::Item> {
675-
// skip the null terminator
676-
if self.ptr.get_offset() + 1 < self.ptr.len() {
659+
// stop before the null terminator at the last position
660+
if self.ptr.get_offset().wrapping_add(1) < self.ptr.len() {
677661
let value = self.ptr.clone();
678662
self.ptr += 1;
679663
Some(value)
@@ -702,10 +686,10 @@ impl Iterator for CStringIterator {
702686
}
703687

704688
impl<T> Sub for Ptr<T> {
705-
type Output = usize;
689+
type Output = isize;
706690
fn sub(self, other: Self) -> Self::Output {
707691
assert!(self.kind == other.kind, "ub: invalid subtraction");
708-
(self.offset / self.elem_step()).wrapping_sub(other.offset / other.elem_step())
692+
(self.get_offset() as isize).wrapping_sub(other.get_offset() as isize)
709693
}
710694
}
711695

@@ -907,13 +891,14 @@ impl<T> fmt::Debug for Ptr<T> {
907891
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
908892
let addr = match &self.kind {
909893
PtrKind::Null => 0,
910-
PtrKind::StackSingle(w) | PtrKind::HeapSingle(w) => (Weak::as_ptr(w) as usize)
911-
.wrapping_add(self.offset.wrapping_mul(std::mem::size_of::<T>())),
912-
PtrKind::StackArray(w) | PtrKind::HeapArray(w) => (Weak::as_ptr(w) as usize)
913-
.wrapping_add(self.offset.wrapping_mul(std::mem::size_of::<T>())),
914-
PtrKind::Vec(w) => (Weak::as_ptr(w) as usize)
915-
.wrapping_add(self.offset.wrapping_mul(std::mem::size_of::<T>())),
916-
PtrKind::Reinterpreted(data) => data.address().wrapping_add(self.offset),
894+
PtrKind::StackSingle(w) | PtrKind::HeapSingle(w) => {
895+
(Weak::as_ptr(w) as usize).wrapping_add(self.byte_offset())
896+
}
897+
PtrKind::StackArray(w) | PtrKind::HeapArray(w) => {
898+
(Weak::as_ptr(w) as usize).wrapping_add(self.byte_offset())
899+
}
900+
PtrKind::Vec(w) => (Weak::as_ptr(w) as usize).wrapping_add(self.byte_offset()),
901+
PtrKind::Reinterpreted(data) => data.address().wrapping_add(self.byte_offset()),
917902
};
918903
write!(f, "0x{:x}", addr)
919904
}
@@ -922,7 +907,7 @@ impl<T> fmt::Debug for Ptr<T> {
922907
impl fmt::Display for Ptr<u8> {
923908
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
924909
match &self.kind {
925-
PtrKind::Null => write!(f, ""),
910+
PtrKind::Null => write!(f, "NULL"),
926911
_ => {
927912
for value in self {
928913
let ch = value.read();
@@ -976,7 +961,7 @@ impl Ptr<u8> {
976961
let va = a.read();
977962
let vb = b.read();
978963
if va != vb {
979-
return va as i32 - vb as i32;
964+
return (va as i32).wrapping_sub(vb as i32);
980965
}
981966
a += 1;
982967
b += 1;
@@ -1010,7 +995,7 @@ impl Ptr<u8> {
1010995
raw[start..end].to_vec()
1011996
}
1012997
PtrKind::Reinterpreted(ref data) => {
1013-
let mut buf = vec![0u8; end - start];
998+
let mut buf = vec![0u8; end.wrapping_sub(start)];
1014999
data.read_bytes(start, &mut buf);
10151000
buf
10161001
}

0 commit comments

Comments
 (0)