Skip to content

Commit 22b5314

Browse files
committed
perf(utils): require Index: Copy for ListAccessorCell
Reduces the static and dynamic instruction count. For the test target `qemu_mps2_an385`, the observed `.text` size reduction was up to 456 bytes (`qemu_sifive_u_rv64`, `kernel_tests::mutex_misc`) or 7.89% (`qemu_mps2_an385`, `kernel_tests::event_group_order_fifo`).
1 parent 8e352c8 commit 22b5314

File tree

1 file changed

+27
-28
lines changed

1 file changed

+27
-28
lines changed

src/r3_kernel/src/utils/intrusive_list.rs

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ where
247247
Pool: ops::Index<Index, Output = Element>,
248248
MapLink: Fn(&Element) -> &LinkCell,
249249
LinkCell: CellLike<CellKey, Target = Option<Link<Index>>>,
250-
Index: PartialEq + Clone,
250+
Index: PartialEq + Copy,
251251
{
252252
pub fn new(head: HeadCell, pool: &'a Pool, map_link: MapLink, cell_key: CellKey) -> Self {
253253
ListAccessorCell {
@@ -286,7 +286,7 @@ where
286286
Pool: ops::Index<Index, Output = Element>,
287287
MapLink: Fn(&Element) -> &LinkCell,
288288
LinkCell: CellLike<CellKey, Target = Option<Link<Index>>>,
289-
Index: PartialEq + Clone,
289+
Index: PartialEq + Copy,
290290
InconsistencyHandler: HandleInconsistency,
291291
{
292292
pub fn head_cell(&self) -> &HeadCell {
@@ -357,7 +357,7 @@ where
357357
item: Index,
358358
at: Option<Index>,
359359
) -> Result<(), InsertError<InconsistencyHandler::Output>> {
360-
if (self.map_link)(&self.pool[item.clone()])
360+
if (self.map_link)(&self.pool[item])
361361
.get(&self.cell_key)
362362
.is_some()
363363
{
@@ -377,43 +377,42 @@ where
377377
(first, false)
378378
};
379379

380-
let prev = (self.map_link)(&self.pool[next.clone()])
380+
let prev = (self.map_link)(&self.pool[next])
381381
.get(&self.cell_key)
382382
.ok_or_else(on_inconsistency)?
383383
.prev;
384384

385385
// prev.next = item
386-
(self.map_link)(&self.pool[prev.clone()]).modify(&mut self.cell_key, |l| match l {
386+
(self.map_link)(&self.pool[prev]).modify(&mut self.cell_key, |l| match l {
387387
// Don't replace this part with the following code:
388388
//
389-
// l.as_mut().ok_or_else(on_inconsistency)?.next = item.clone();
389+
// l.as_mut().ok_or_else(on_inconsistency)?.next = item;
390390
//
391391
// When `HandleInconsistencyUnchecked` is in use, the above code
392392
// can be "optimized" to the following code, which is very
393393
// inefficient:
394394
//
395-
// l.as_mut().unwrap_or(0).next = item.clone();
395+
// l.as_mut().unwrap_or(0).next = item;
396396
//
397397
Some(l) => {
398-
l.next = item.clone();
398+
l.next = item;
399399
Ok::<(), InconsistencyHandler::Output>(())
400400
}
401401
None => Err(on_inconsistency()),
402402
})?;
403403

404404
// next.prev = item
405-
(self.map_link)(&self.pool[next.clone()]).modify(&mut self.cell_key, |l| match l {
405+
(self.map_link)(&self.pool[next]).modify(&mut self.cell_key, |l| match l {
406406
Some(l) => {
407-
l.prev = item.clone();
407+
l.prev = item;
408408
Ok::<(), InconsistencyHandler::Output>(())
409409
}
410410
None => Err(on_inconsistency()),
411411
})?;
412412

413413
// item.prev = prev
414414
// item.next = next
415-
(self.map_link)(&self.pool[item.clone()])
416-
.set(&mut self.cell_key, Some(Link { prev, next }));
415+
(self.map_link)(&self.pool[item]).set(&mut self.cell_key, Some(Link { prev, next }));
417416

418417
if update_first {
419418
head.first = Some(item);
@@ -422,12 +421,12 @@ where
422421
} else {
423422
debug_assert!(at.is_none());
424423

425-
let link = (self.map_link)(&self.pool[item.clone()]);
424+
let link = (self.map_link)(&self.pool[item]);
426425
link.set(
427426
&mut self.cell_key,
428427
Some(Link {
429-
prev: item.clone(),
430-
next: item.clone(),
428+
prev: item,
429+
next: item,
431430
}),
432431
);
433432

@@ -461,7 +460,7 @@ where
461460
&mut self,
462461
item: Index,
463462
) -> Result<Index, ItemError<InconsistencyHandler::Output>> {
464-
if (self.map_link)(&self.pool[item.clone()])
463+
if (self.map_link)(&self.pool[item])
465464
.get(&self.cell_key)
466465
.is_none()
467466
{
@@ -479,9 +478,9 @@ where
479478
}}
480479

481480
let link: Link<Index> = {
482-
let link_ref = (self.map_link)(&self.pool[item.clone()]);
481+
let link_ref = (self.map_link)(&self.pool[item]);
483482
let mut head = self.head();
484-
if head.first.as_ref() == Some(&item) {
483+
if head.first == Some(item) {
485484
let next = link_ref
486485
.get(&self.cell_key)
487486
.ok_or_else(on_inconsistency!())?
@@ -509,26 +508,26 @@ where
509508
let on_inconsistency = on_inconsistency!();
510509

511510
// link.prev.next = link.next
512-
(self.map_link)(&self.pool[link.prev.clone()]).modify(&mut self.cell_key, |l| match l {
511+
(self.map_link)(&self.pool[link.prev]).modify(&mut self.cell_key, |l| match l {
513512
Some(l) => {
514-
l.next = link.next.clone();
513+
l.next = link.next;
515514
Ok::<(), InconsistencyHandler::Output>(())
516515
}
517516
None => Err(on_inconsistency()),
518517
})?;
519518

520519
// link.next.prev = link.prev
521-
(self.map_link)(&self.pool[link.next.clone()]).modify(&mut self.cell_key, |l| match l {
520+
(self.map_link)(&self.pool[link.next]).modify(&mut self.cell_key, |l| match l {
522521
Some(l) => {
523-
l.prev = link.prev.clone();
522+
l.prev = link.prev;
524523
Ok::<(), InconsistencyHandler::Output>(())
525524
}
526525
None => Err(on_inconsistency()),
527526
})?;
528527

529528
// item.prev = null
530529
// item.next = null
531-
(self.map_link)(&self.pool[item.clone()]).set(&mut self.cell_key, None);
530+
(self.map_link)(&self.pool[item]).set(&mut self.cell_key, None);
532531

533532
Ok(item)
534533
}
@@ -564,7 +563,7 @@ where
564563
.get(&self.cell_key)
565564
.ok_or(ItemError::NotLinked)?
566565
.next;
567-
Ok(if Some(&next) == self.head().first.as_ref() {
566+
Ok(if Some(next) == self.head().first {
568567
None
569568
} else {
570569
Some(next)
@@ -574,7 +573,7 @@ where
574573
/// Get the previous element of the specified element.
575574
#[inline]
576575
pub fn prev(&self, i: Index) -> Result<Option<Index>, ItemError<InconsistencyHandler::Output>> {
577-
Ok(if Some(&i) == self.head().first.as_ref() {
576+
Ok(if Some(i) == self.head().first {
578577
None
579578
} else {
580579
Some(
@@ -623,18 +622,18 @@ where
623622
MapLink: 'a + Fn(&Element) -> &LinkCell,
624623
Element: 'a + 'b,
625624
LinkCell: CellLike<CellKey, Target = Option<Link<Index>>>,
626-
Index: PartialEq + Clone,
625+
Index: PartialEq + Copy,
627626
InconsistencyHandler: HandleInconsistency,
628627
{
629628
type Item = Result<(Index, &'a Element), InconsistencyHandler::Output>;
630629

631630
fn next(&mut self) -> Option<Self::Item> {
632631
if let Some(next) = self.next.take() {
633-
self.next = match self.accessor.next(next.clone()) {
632+
self.next = match self.accessor.next(next) {
634633
Ok(x) => x,
635634
Err(_) => return Some(Err(self.accessor.inconsistency_handler.on_inconsistency())),
636635
};
637-
Some(Ok((next.clone(), &self.accessor.pool[next])))
636+
Some(Ok((next, &self.accessor.pool[next])))
638637
} else {
639638
None
640639
}

0 commit comments

Comments
 (0)