Faster company id table

Following up on the [loose end from the initial
PR](https://github.com/google/bumble/pull/207#discussion_r1278015116),
we can avoid accessing the Python company id map at runtime by doing
code gen ahead of time.

Using an example to do the code gen avoids even the small build slowdown
from invoking the code gen logic in build.rs, but more importantly,
means that it's still a totally boring normal build that won't require
any IDE setup, etc, to work for everyone. Since the company ID list
changes rarely, and there's a test to ensure it always matches, this
seems like a good trade.
This commit is contained in:
Marshall Pierce
2023-08-03 10:06:02 -06:00
parent 0301b1a999
commit e2c1ad5342
11 changed files with 2921 additions and 129 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -14,40 +14,8 @@
//! Assigned numbers from the Bluetooth spec.
use crate::wrapper::core::Uuid16;
use lazy_static::lazy_static;
use pyo3::{
intern,
types::{PyDict, PyModule},
PyResult, Python,
};
use std::collections;
mod company_ids;
mod services;
pub use company_ids::COMPANY_IDS;
pub use services::SERVICE_IDS;
lazy_static! {
/// Assigned company IDs
pub static ref COMPANY_IDS: collections::HashMap<Uuid16, String> = load_company_ids()
.expect("Could not load company ids -- are Bumble's Python sources available?");
}
fn load_company_ids() -> PyResult<collections::HashMap<Uuid16, String>> {
// this takes about 4ms on a fast machine -- slower than constructing in rust, but not slow
// enough to worry about
Python::with_gil(|py| {
PyModule::import(py, intern!(py, "bumble.company_ids"))?
.getattr(intern!(py, "COMPANY_IDENTIFIERS"))?
.downcast::<PyDict>()?
.into_iter()
.map(|(k, v)| {
Ok((
Uuid16::from_be_bytes(k.extract::<u16>()?.to_be_bytes()),
v.str()?.to_str()?.to_string(),
))
})
.collect::<PyResult<collections::HashMap<_, _>>>()
})
}

View File

@@ -59,7 +59,7 @@ impl AdvertisingData {
}
/// 16-bit UUID
#[derive(PartialEq, Eq, Hash)]
#[derive(PartialEq, Eq, Hash, Clone, Copy)]
pub struct Uuid16 {
/// Big-endian bytes
uuid: [u8; 2],