Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use fastcrc if available #943

Merged
merged 3 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
strategy:
fail-fast: false # don't cancel if a job from the matrix fails
matrix:
python-version: ['3.5', '3.6', '3.7', '3.8', '3.9', '3.10', '3.11', '3.12']
python-version: ['3.6', '3.7', '3.8', '3.9', '3.10', '3.11', '3.12']

steps:
- uses: actions/checkout@v3
Expand Down
3 changes: 3 additions & 0 deletions generator/C/test/posix/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,6 @@ testmav2.0_minimal: testmav.c

sha256_test: sha256_test.c
$(CC) $(CFLAGS) -I../../include_v2.0 -o $@ sha256_test.c

parse_tlog: parse_tlog.c
$(CC) $(CFLAGS) -I../../include_v2.0 -I../../include_v2.0/ardupilotmega -o $@ parse_tlog.c
59 changes: 59 additions & 0 deletions generator/C/test/posix/parse_tlog.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
parse a tlog, for performance testing
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
#include <stddef.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#define MAVLINK_USE_CONVENIENCE_FUNCTIONS
#define MAVLINK_USE_MESSAGE_INFO
#define MAVLINK_COMM_NUM_BUFFERS 2

// this trick allows us to make mavlink_message_t as small as possible
// for this dialect, which saves some memory
#include <version.h>
#define MAVLINK_MAX_PAYLOAD_LEN MAVLINK_MAX_DIALECT_PAYLOAD_SIZE

#include <mavlink_types.h>
static mavlink_system_t mavlink_system = {42,11,};

#define MAVLINK_ASSERT(x) assert(x)
static void comm_send_ch(mavlink_channel_t chan, uint8_t c) {}

#include <mavlink.h>

int main(int argc, const char *argv[])
{
if (argc < 2) {
printf("Usage: parse_tlog FILENAME\n");
exit(1);
}
const char *filename = argv[1];
int fd = open(filename, O_RDONLY);
if (fd == -1) {
perror(filename);
exit(1);
}
mavlink_channel_t chan = MAVLINK_COMM_0;
char c;
uint32_t msg_count = 0;
mavlink_message_t last_msg;
mavlink_status_t status;

while (read(fd, &c, 1) == 1) {
if (mavlink_parse_char(chan, c, &last_msg, &status)) {
msg_count++;
}
}
printf("parsed %u messages\n", (unsigned)msg_count);

return 0;
}

61 changes: 60 additions & 1 deletion generator/mavcrc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@
import sys
from builtins import object

try:
import fastcrc
mcrf4xx = fastcrc.crc16.mcrf4xx
except Exception:
mcrf4xx = None

class x25crc(object):
class x25crc_slow(object):
"""CRC-16/MCRF4XX - based on checksum.h from mavlink library"""

def __init__(self, buf=None):
Expand Down Expand Up @@ -39,3 +44,57 @@ def accumulate_str(self, buf):
Provided for backwards compatibility. accumulate now also works on strings.
"""
return self.accumulate(buf)

class x25crc_fast(object):
"""CRC-16/MCRF4XX - based on checksum.h from mavlink library"""
def __init__(self, buf=None):
self.crc = 0xFFFF
if buf is not None:
self.accumulate(buf)

def accumulate(self, buf):
"""add in some more bytes (it also accepts strings)"""
if sys.version_info[0] == 2:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we really be adding Py2 compat here?

if type(buf) is str:
buf = bytearray(buf)
elif type(buf).__name__ == 'unicode':
# we can't use the identifier unicode in python3
buf = bytearray(buf.encode())
elif type(buf) is str:
buf = bytes(buf.encode())
elif type(buf) is list:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should all of these be isinstance?

buf = bytes(buf)
self.crc = mcrf4xx(buf, self.crc)

def accumulate_str(self, buf):
"""
Provided for backwards compatibility. accumulate now also works on strings.
"""
return self.accumulate(buf)

if mcrf4xx is not None:
x25crc = x25crc_fast
else:
x25crc = x25crc_slow

if __name__ == "__main__":
import random, time

for i in range(100):
nbytes = random.randint(1000,5000)
b = random.randbytes(nbytes)

t1 = time.time()
slow = x25crc_slow()
t2 = time.time()
slow_t = t2 - t1

t1 = time.time()
c = x25crc()
t2 = time.time()
fast_t = t2 - t1

slow.accumulate(b)
c.accumulate(b)
assert slow.crc == c.crc
print("Speedup %.2f" % (slow_t/fast_t))
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ lxml>=3.6.0
future>=0.15.2
wheel>=0.37.1
setuptools>=42
fastcrc

# dev dependencies:
pytest<=7.4.4
syrupy; python_version>='3.6'
syrupy; python_version>='3.6'
Loading