Skip to content

Commit 1ac8b43

Browse files
authored
Merge pull request #48 from hit9/dev-c-perf
(wip) v1.1.0: C standard mode performance improvements
2 parents 06a32cc + c006b72 commit 1ac8b43

File tree

40 files changed

+983
-397
lines changed

40 files changed

+983
-397
lines changed

benchmark/stm32/README.rst

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Benchmark result
1313

1414
* Microcontroller: `STM32F103ZE <https://www.st.com/en/microcontrolles-microprocessors/stm32f103ze.html>`_,
1515
Arm Cortex-M3 MCU, 72 MHz
16-
* bitproto file: `drone.bitproto <drone.bitproto>`_.
16+
* bitproto file: `drone.bitproto <drone.bitproto>`_, with a `100bytes` length message.
1717

1818
Standard Mode
1919
^^^^^^^^^^^^^
@@ -30,16 +30,16 @@ the optimization mode flag ``-O``, that is to use `the bitproto c lib <../../lib
3030
- Decode cost per call
3131
* - No ``-O``
3232
- 10000
33-
- 170μs
34-
- 163μs
33+
- 144μs
34+
- 143μs
3535
* - ``gcc -O2``
3636
- 10000
37-
- 135μs
38-
- 131μs
37+
- 140μs
38+
- 143μs
3939
* - ``gcc -O3``
4040
- 10000
41-
- 127μs
42-
- 119μs
41+
- 128μs
42+
- 123μs
4343

4444
Optimization Mode
4545
^^^^^^^^^^^^^^^^^
@@ -56,9 +56,9 @@ the optimization mode flag ``-O``, that is not to use `the bitproto c lib <../..
5656
- Decode cost per call
5757
* - No ``-O``
5858
- 10000
59-
- 9μs
60-
- 9μs
59+
- 15μs
60+
- 15μs
6161
* - ``gcc -O2``
6262
- 10000
63-
- 9μs
64-
- 9μs
63+
- 15μs
64+
- 15μs

benchmark/stm32/bpbench/Core/Src/main.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "main.h"
2222

2323
#include <stdarg.h>
24+
#include <stdbool.h>
2425
#include <stdio.h>
2526

2627
#include "drone_bp.h"
@@ -65,6 +66,10 @@ static void MX_USART1_UART_Init(void);
6566
/* Private user code ---------------------------------------------------------*/
6667
/* USER CODE BEGIN 0 */
6768

69+
void soft_assert(bool v) {
70+
if (!v) simple_printf("assert failed !!!!!!!!!!!!!!!!!!!!\r\n");
71+
}
72+
6873
// Printf to uart1.
6974
void simple_printf(const char *fmt, ...) {
7075
char buf[512] = {0};
@@ -132,6 +137,51 @@ void bench() {
132137
bench_decode(n, s);
133138
}
134139

140+
void simple_test() {
141+
struct Drone drone = {0};
142+
143+
drone.status = DRONE_STATUS_RISING;
144+
drone.position.longitude = 2000;
145+
drone.position.latitude = 2000;
146+
drone.position.altitude = 1080;
147+
drone.flight.acceleration[0] = -1001;
148+
drone.power.is_charging = false;
149+
drone.propellers[0].direction = ROTATING_DIRECTION_CLOCK_WISE;
150+
drone.pressure_sensor.pressures[0] = -11;
151+
152+
unsigned char s[BYTES_LENGTH_DRONE] = {0};
153+
154+
EncodeDrone(&drone, s);
155+
156+
unsigned char s_expected[BYTES_LENGTH_DRONE] = {
157+
0x82, 0x3E, 0x00, 0x00, 0x80, 0x3E, 0x00, 0x00, 0xC0, 0x21, 0x00, 0x00,
158+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
159+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
160+
0xB8, 0xE0, 0xFF, 0xFF, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
161+
0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
162+
0x50, 0xFF, 0xFF, 0x0F, 0x00, 0x00, 0x00};
163+
164+
for (int i = 0; i < BYTES_LENGTH_DRONE; i++)
165+
soft_assert(s_expected[i] == s[i]);
166+
167+
struct Drone drone_new = {0};
168+
DecodeDrone(&drone_new, s);
169+
170+
soft_assert(drone.status == drone_new.status);
171+
soft_assert(drone.position.longitude == drone_new.position.longitude);
172+
soft_assert(drone.position.latitude == drone_new.position.latitude);
173+
soft_assert(drone.position.altitude == drone_new.position.altitude);
174+
soft_assert(drone.flight.acceleration[0] ==
175+
drone_new.flight.acceleration[0]);
176+
soft_assert(drone.power.is_charging == drone_new.power.is_charging);
177+
soft_assert(drone.propellers[0].direction ==
178+
drone_new.propellers[0].direction);
179+
soft_assert(drone.pressure_sensor.pressures[0] ==
180+
drone_new.pressure_sensor.pressures[0]);
181+
182+
simple_printf("simple test finished\r\n");
183+
}
184+
135185
/* USER CODE END 0 */
136186

137187
/**
@@ -172,6 +222,8 @@ int main(void) {
172222

173223
/* Infinite loop */
174224
/* USER CODE BEGIN WHILE */
225+
simple_test();
226+
175227
while (1) {
176228
/* USER CODE END WHILE */
177229
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_5);

benchmark/stm32/drone.bitproto

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ message Flight {
8383
TernaryInt32 acceleration = 3;
8484
}
8585

86+
message PressureSensor {
87+
int24[2] pressures = 1;
88+
}
89+
8690
message Drone {
8791
DroneStatus status = 1;
8892
Position position = 2;
@@ -91,4 +95,8 @@ message Drone {
9195
Power power = 5;
9296
Network network = 6;
9397
LandingGear landing_gear = 7;
98+
PressureSensor pressure_sensor = 8;
99+
// 67 bytes => 100bytes
100+
// For optimiation mode, remove the '
101+
byte[33] bytes = 9;
94102
}

0 commit comments

Comments
 (0)