Skip to content

Commit

Permalink
Add brotly as compression algorithm and make it optional, as well as …
Browse files Browse the repository at this point in the history
…zstd due to binary file size
  • Loading branch information
kripton committed Jun 11, 2021
1 parent 7897b20 commit b12855e
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@
[submodule "lib/zlib"]
path = lib/zlib
url = https://github.com/kripton/zlib.git
[submodule "lib/brotli"]
path = lib/brotli
url = https://github.com/kripton/brotli.git
1 change: 1 addition & 0 deletions lib/brotli
Submodule brotli added at 6ea61d
20 changes: 20 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,16 @@ project(rp2040-compressiontest)
## Init the pico-sdk. Will instruct the user what to do if deps are missing
pico_sdk_init()

## Needed so we can fit all compression algos into the Picos flash
## See below. However, needs to be set here in order to be effective
add_compile_options(-Os)

## Include the CMakeLists-files of (non-pico-sdk)-libraries we will be using
include(../lib/heatshrink/CMakeLists.txt)
include(../lib/zlib/interfaceLibForPicoSDK.cmake)
include(../lib/zstd/interfaceLibForPicoSDK.cmake)
include(../lib/snappy/interfaceLibForPicoSDK.cmake)
include(../lib/brotli/interfaceLibForPicoSDK.cmake)

## Add our own C/C++ files here
## Sorted alphabetically
Expand All @@ -24,9 +29,23 @@ add_executable(${CMAKE_PROJECT_NAME}
algo-zlib.c
algo-zstd.c
algo-snappy.c
algo-brotli.c
main.c
)

## Unfortunately, including all algortihms would result in a binary file
## too large for the Pico board (~2.7MB vs. 2MB available)
## We could try -Os but that contradicts our benchmarking effort ... UPDATE:
## just trying it revealed that the code is not that much slower.
## Using -flto broke the build :/
## This is why you can either exclude ZSTD OR BROTLI, which both add a
## remarkable amount to the size of the UF2 file.
## When using -Os (see above), all algos just fit
target_compile_definitions(${CMAKE_PROJECT_NAME}
PUBLIC HAVE_ALGO_ZSTD=1
PUBLIC HAVE_ALGO_BROTLI=1
)

## Config for the heatshrink library
target_compile_definitions(${CMAKE_PROJECT_NAME}
PUBLIC HEATSHRINK_DYNAMIC_ALLOC=0
Expand Down Expand Up @@ -54,6 +73,7 @@ target_link_libraries(${CMAKE_PROJECT_NAME}
zlib
zstd
snappy
brotli
)

## enable usb output, disable uart output
Expand Down
18 changes: 18 additions & 0 deletions src/algo-brotli.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "algo-zlib.h"

#include "../lib/brotli/c/include/brotli/encode.h"
#include "../lib/brotli/c/include/brotli/decode.h"

void brotli_init()
{
}

void brotli_compress(uint8_t *input, size_t insize, uint8_t *output, size_t *outsize)
{
BrotliEncoderCompress(1, 10, BROTLI_DEFAULT_MODE, insize, input, outsize, output);
}

void brotli_uncompress(uint8_t *input, size_t insize, uint8_t *output, size_t *outsize)
{
BrotliDecoderDecompress(insize, input, outsize, output);
}
10 changes: 10 additions & 0 deletions src/algo-brotli.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef __ALGO_BROTLI_H__
#define __ALGO_BROTLI_H__

#include "pico/stdlib.h"

void brotli_init();
void brotli_compress(uint8_t* input, size_t insize, uint8_t* output, size_t* outsize);
void brotli_uncompress(uint8_t* input, size_t insize, uint8_t* output, size_t* outsize);

#endif // __ALGO_BROTLI_H__
24 changes: 20 additions & 4 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@

#include "algo-heatshrink.h"
#include "algo-zlib.h"
#if HAVE_ALGO_ZSTD
#include "algo-zstd.h"
#endif
#include "algo-snappy.h"
#if HAVE_ALGO_BROTLI
#include "algo-brotli.h"
#endif


// TEST VECTORS
Expand Down Expand Up @@ -54,8 +59,13 @@ struct algo {
enum {
ALGO_HEATSHRINK,
ALGO_ZLIB,
#if HAVE_ALGO_ZSTD
ALGO_ZSTD,
#endif
ALGO_SNAPPY,
#if HAVE_ALGO_BROTLI
ALGO_BROTLI,
#endif
// Add more HERE
ALGO_NUM_TOTAL
};
Expand Down Expand Up @@ -179,34 +189,40 @@ int main() {
printf("Initializing compression algorithms ... ");
start_time = time_us_32();

// Algo 0: heatshrink
sprintf(algos[ALGO_HEATSHRINK].name, "HEATSHRINK");
algos[ALGO_HEATSHRINK].init = heatshrink_init;
algos[ALGO_HEATSHRINK].compress = heatshrink_compress;
algos[ALGO_HEATSHRINK].uncompress = heatshrink_uncompress;
algos[ALGO_HEATSHRINK].init();

// Algo 1: zlib
sprintf(algos[ALGO_ZLIB].name, "ZLIB");
algos[ALGO_ZLIB].init = zlib_init;
algos[ALGO_ZLIB].compress = zlib_compress;
algos[ALGO_ZLIB].uncompress = zlib_uncompress;
algos[ALGO_ZLIB].init();

// Algo 2: zstd
#if HAVE_ALGO_ZSTD
sprintf(algos[ALGO_ZSTD].name, "ZSTD");
algos[ALGO_ZSTD].init = zstd_init;
algos[ALGO_ZSTD].compress = zstd_compress;
algos[ALGO_ZSTD].uncompress = zstd_uncompress;
algos[ALGO_ZSTD].init();
#endif

// Algo 2: snappy
sprintf(algos[ALGO_SNAPPY].name, "SNAPPY");
algos[ALGO_SNAPPY].init = mysnappy_init;
algos[ALGO_SNAPPY].compress = mysnappy_compress;
algos[ALGO_SNAPPY].uncompress = mysnappy_uncompress;
algos[ALGO_SNAPPY].init();

#if HAVE_ALGO_BROTLI
sprintf(algos[ALGO_BROTLI].name, "BROTLI");
algos[ALGO_BROTLI].init = brotli_init;
algos[ALGO_BROTLI].compress = brotli_compress;
algos[ALGO_BROTLI].uncompress = brotli_uncompress;
algos[ALGO_BROTLI].init();
#endif

finish_time = time_us_32();
elapsed_time = finish_time - start_time;
printf("%luµs\n", elapsed_time);
Expand Down

0 comments on commit b12855e

Please sign in to comment.