Skip to content

Commit 368c9e7

Browse files
authored
Fix: simplify insert (#281)
simplify insert cleaner unsized_list
1 parent 1f9589b commit 368c9e7

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- Reduced unsized map CU cost by 50% when key already exists (#281)
13+
- Use full result path in unsized_type proc macro (#281)
14+
1015
## [0.26.1] - 2025-10-02
1116

1217
### Fixed

star_frame/src/unsize/impls/unsized_list.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,8 @@ where
282282
})?;
283283

284284
for item in rest.iter_mut() {
285-
item.as_mut_offset().0 =
286-
unsafe { item.as_mut_offset().0.unchecked_sub(change) }
285+
let item = item.as_mut_offset();
286+
item.0 = unsafe { item.0.unchecked_sub(change) };
287287
}
288288
}
289289
}
@@ -301,8 +301,8 @@ where
301301
})?;
302302

303303
for item in rest.iter_mut() {
304-
item.as_mut_offset().0 =
305-
unsafe { item.as_mut_offset().0.unchecked_add(change) }
304+
let item = item.as_mut_offset();
305+
item.0 = unsafe { item.0.unchecked_add(change) };
306306
}
307307
}
308308
}

star_frame/src/unsize/impls/unsized_map.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{
1313
UnsizedListWithOffsetIterMut,
1414
},
1515
init::UnsizedInit,
16-
FromOwned,
16+
FromOwned, UnsizedTypeMut,
1717
},
1818
};
1919
use std::{collections::BTreeMap, iter::FusedIterator};
@@ -189,12 +189,13 @@ where
189189
pub fn insert<I>(&mut self, key: K, value: I) -> Result<bool>
190190
where
191191
V: UnsizedInit<I>,
192+
V::Mut<'top>: UnsizedTypeMut<UnsizedType = V>,
192193
{
193194
match self.get_index(&key) {
194195
Ok(existing_index) => {
195-
// TODO: optimize this by just modifying bytes to fit and then writing to them
196-
self.list().remove(existing_index)?;
197-
self.list().insert_with_offset(existing_index, value, key)?;
196+
let mut list = self.list();
197+
let mut item = list.index_exclusive(existing_index)?;
198+
item.set_from_init(value)?;
198199
Ok(false)
199200
}
200201
Err(insertion_index) => {

star_frame_proc/src/unsize/struct_impl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ impl UnsizedStructContext {
654654
if self.args.owned_type.is_some() {
655655
return None;
656656
}
657-
Paths!(prelude, default);
657+
Paths!(prelude, default, result);
658658
UnsizedStructContext!(self => struct_type, sized_field_idents, unsized_field_types, unsized_field_idents, sized_type);
659659

660660
let from_owned_generics =
@@ -694,7 +694,7 @@ impl UnsizedStructContext {
694694
}
695695

696696
#[inline]
697-
fn from_owned(owned: Self::Owned, bytes: &mut &mut [u8]) -> Result<usize> {
697+
fn from_owned(owned: Self::Owned, bytes: &mut &mut [u8]) -> #result<usize> {
698698
let size = {
699699
#sized_from_owned #(<#unsized_field_types as #prelude::FromOwned>::from_owned(owned.#unsized_field_idents, bytes)? +)* 0
700700
};

0 commit comments

Comments
 (0)