-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: reduce string conversion overhead for bluetooth addresses (#12)
- Loading branch information
Showing
3 changed files
with
27 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |