Skip to content

Commit

Permalink
Reduce allocs
Browse files Browse the repository at this point in the history
  • Loading branch information
taks committed Feb 5, 2025
1 parent ad6a654 commit 3afa7db
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 23 deletions.
9 changes: 2 additions & 7 deletions src/client/ble_remote_characteristic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,8 @@ impl BLERemoteCharacteristic {

pub(crate) unsafe fn notify(&mut self, om: *mut esp_idf_sys::os_mbuf) {
if let Some(no_notify) = self.state.on_notify.as_mut() {
let mut buf = Vec::with_capacity(esp_idf_sys::BLE_ATT_ATTR_MAX_LEN as _);

for om in OsMBuf(om).iter() {
buf.extend_from_slice(om.as_slice());
}

no_notify(&buf);
let om = OsMBuf(om);
no_notify(om.as_flat().as_slice());
}
}
}
Expand Down
10 changes: 4 additions & 6 deletions src/server/ble_characteristic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,10 +347,8 @@ impl BLECharacteristic {
}
}
sys::BLE_GATT_ACCESS_OP_WRITE_CHR => {
let mut buf = Vec::with_capacity(sys::BLE_ATT_ATTR_MAX_LEN as _);
for om in OsMBuf(ctxt.om).iter() {
buf.extend_from_slice(om.as_slice());
}
let om = OsMBuf(ctxt.om);
let buf = om.as_flat();

let mut notify = false;

Expand All @@ -360,7 +358,7 @@ impl BLECharacteristic {
let desc = crate::utilities::ble_gap_conn_find(conn_handle).unwrap();
let mut arg = OnWriteArgs {
current_data: (*characteristic.get()).value.as_slice(),
recv_data: &buf,
recv_data: buf.as_slice(),
desc: &desc,
reject: false,
error_code: 0,
Expand All @@ -374,7 +372,7 @@ impl BLECharacteristic {
notify = arg.notify;
}
}
characteristic.set_value(&buf);
characteristic.set_value(buf.as_slice());
if notify {
characteristic.notify();
}
Expand Down
13 changes: 5 additions & 8 deletions src/server/ble_descriptor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{utilities::OsMBuf, BLEConnDesc};
use alloc::{boxed::Box, vec::Vec};
use alloc::boxed::Box;
use bitflags::bitflags;
use core::{cell::UnsafeCell, ffi::c_void};
use esp_idf_svc::sys as esp_idf_sys;
Expand Down Expand Up @@ -117,19 +117,16 @@ impl BLEDescriptor {
}
}
esp_idf_sys::BLE_GATT_ACCESS_OP_WRITE_DSC => {
let mut buf = Vec::with_capacity(esp_idf_sys::BLE_ATT_ATTR_MAX_LEN as _);

for om in OsMBuf(ctxt.om).iter() {
buf.extend_from_slice(om.as_slice());
}
let om = OsMBuf(ctxt.om);
let buf = om.as_flat();

unsafe {
let descriptor = UnsafeCell::new(&mut descriptor);
if let Some(callback) = &mut (*descriptor.get()).on_write {
let desc = crate::utilities::ble_gap_conn_find(conn_handle).unwrap();
let mut arg = OnWriteDescriptorArgs {
current_data: (*descriptor.get()).value.as_slice(),
recv_data: &buf,
recv_data: buf.as_slice(),
desc: &desc,
reject: false,
error_code: 0,
Expand All @@ -141,7 +138,7 @@ impl BLEDescriptor {
}
}
}
descriptor.set_value(&buf);
descriptor.set_value(buf.as_slice());

0
}
Expand Down
38 changes: 36 additions & 2 deletions src/utilities/os_mbuf.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use alloc::vec::Vec;
use core::ffi::c_int;
use esp_idf_svc::sys;
use sys::os_mbuf;

#[cfg(not(esp_idf_soc_esp_nimble_controller))]
use sys::os_mbuf_append as _os_mbuf_append;
Expand All @@ -9,7 +9,7 @@ use sys::os_mbuf_append as _os_mbuf_append;
use sys::r_os_mbuf_append as _os_mbuf_append;

#[derive(Copy, Clone)]
pub(crate) struct OsMBuf(pub *mut os_mbuf);
pub(crate) struct OsMBuf(pub *mut sys::os_mbuf);

#[allow(unused)]
impl OsMBuf {
Expand Down Expand Up @@ -38,6 +38,26 @@ impl OsMBuf {
pub fn iter(&self) -> OsMBufIterator {
OsMBufIterator(*self)
}

#[inline]
fn has_next(&self) -> bool {
unsafe { !(*self.0).om_next.sle_next.is_null() }
}

#[inline]
pub fn as_flat(&self) -> FlatData<'_> {
if (self.has_next()) {
let mut buf = Vec::with_capacity(self.entire_len() as _);

for mbuf in self.iter() {
buf.extend_from_slice(mbuf.as_slice());
}

FlatData::Vec(buf)
} else {
FlatData::Slice(self.as_slice())
}
}
}

pub(crate) struct OsMBufIterator(OsMBuf);
Expand All @@ -57,3 +77,17 @@ impl Iterator for OsMBufIterator {
}
}
}

pub(crate) enum FlatData<'a> {
Vec(Vec<u8>),
Slice(&'a [u8]),
}

impl FlatData<'_> {
pub fn as_slice(&self) -> &[u8] {
match self {
FlatData::Vec(v) => v.as_slice(),
FlatData::Slice(s) => s,
}
}
}

0 comments on commit 3afa7db

Please # to comment.