Skip to content

Commit

Permalink
feat: reduce string conversion overhead for bluetooth addresses (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Jun 14, 2023
1 parent af5a060 commit 558c93f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
17 changes: 13 additions & 4 deletions src/bluetooth_data_tools/_utils_impl.pyx
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
from libcpp.string cimport string

import cython

from libc.stdlib cimport malloc


cdef extern from "stdint.h":
ctypedef unsigned long long uint64_t

cdef extern from "utils_wrapper.h":
string _cpp_uint64_to_bdaddr(uint64_t addr)
void _uint64_to_bdaddr(uint64_t addr, char *bdaddr)

cdef char* uint64_to_bdaddr(uint64_t address):
cdef char* bdaddr = <char *> malloc((18) * sizeof(char))
if not bdaddr:
return NULL # malloc failed
_uint64_to_bdaddr(address, bdaddr)
return bdaddr



def _int_to_bluetooth_address(addr: int) -> str:
return _cpp_uint64_to_bdaddr(addr).decode('ascii')
return uint64_to_bdaddr(addr).decode('ascii')
21 changes: 10 additions & 11 deletions src/bluetooth_data_tools/utils.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
"""Bluetooth utils."""


from __future__ import annotations

from functools import lru_cache

def int_to_bluetooth_address(address: int) -> str:
"""Convert an integer to a bluetooth address."""
mac_hex = f"{address:012X}"
return f"{mac_hex[0:2]}:{mac_hex[2:4]}:{mac_hex[4:6]}:{mac_hex[6:8]}:{mac_hex[8:10]}:{mac_hex[10:12]}" # noqa: E501
try:
from ._utils_impl import _int_to_bluetooth_address # noqa: F811 F401

int_to_bluetooth_address = lru_cache(maxsize=256)(_int_to_bluetooth_address)

try:
from ._utils_impl import ( # type: ignore[no-redef] # noqa: F811 F401
_int_to_bluetooth_address as int_to_bluetooth_address,
)
except ImportError:
pass

@lru_cache(maxsize=256)
def int_to_bluetooth_address(address: int) -> str:
"""Convert an integer to a bluetooth address."""
mac_hex = f"{address:012X}"
return f"{mac_hex[0:2]}:{mac_hex[2:4]}:{mac_hex[4:6]}:{mac_hex[6:8]}:{mac_hex[8:10]}:{mac_hex[10:12]}" # noqa: E501
16 changes: 4 additions & 12 deletions src/bluetooth_data_tools/utils_wrapper.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@


#include <stdint.h>
#include <string>
#include <memory>
#include <stdio.h>

#define BDADDR_SIZE 18 // 17 + '\0'

using namespace std;


std::string _cpp_uint64_to_bdaddr(uint64_t address) {
std::unique_ptr<char[]> buf( new char[ BDADDR_SIZE ] );
std::snprintf( buf.get(), BDADDR_SIZE, "%02X:%02X:%02X:%02X:%02X:%02X", (uint8_t) (address >> 40) & 0xff,
void _uint64_to_bdaddr(uint64_t address, char *bdaddr) {
snprintf(bdaddr, 18, "%02X:%02X:%02X:%02X:%02X:%02X", (uint8_t) (address >> 40) & 0xff,
(uint8_t) (address >> 32) & 0xff, (uint8_t) (address >> 24) & 0xff,
(uint8_t) (address >> 16) & 0xff, (uint8_t) (address >> 8) & 0xff,
(uint8_t) (address >> 0) & 0xff );
return std::string( buf.get(), buf.get() + BDADDR_SIZE - 1 ); // We don't want the '\0' inside
(uint8_t) (address >> 0) & 0xff);
}

0 comments on commit 558c93f

Please sign in to comment.