Closed bblaszkow06 closed 2 years ago
Will look into that.
I think that the problem is different - brotli:encode/1
can be used with binaries that are at most 1.25 MiB (that is 1310721 bytes). Question is how we should handle such case. Whether disallow passing such huge binaries at once and force user to use streaming parsing, or rather "internally" split the input into smaller chunks and parse them separately.
For brotli:encode/1
I would suggest using BrotliEncoderCompress
as it performs one-shot memory-to-memory compression, and is essentialy what brotli:encode/1
is now, but also add brotli:encode_stream
which would internally use BrotliEncoderCompressStream
and would allow to handle output in chunks. But to do that there would have to be a way to handleavailable_in
, next_in
, next_out
and total_out
which are used to resume compression
@marmor157 the problem is that BrotliEncoderCompress
supports only small set of the options that brotli:encode/2
supports. Technically I could do some runtime checks to pick proper implementation, however that was easier.
@marmor157 another reason why I prefer using Erlang implementation for compressing via brotli:encode/{1,2}
is that it create less stress on the schedulers (even dirty). Having NIF that will run few seconds can be problematic even with dirty schedulers.
Awesome, thanks @hauleth for the quick fix & update.
Using version v0.3.0 the library returns an
:error
atom when trying to encode larger binary (in my case somewhere between 1M and 2M). This breaks https://github.com/hauleth/phoenix_bakery when a large JS bundle is compressedSteps to reproduce (in Elixir)
First create a file
then
Suspected reason
The responsible atom comes from here: https://github.com/yjh0502/erl-brotli/blob/9582135327ca81d420b5ccb5ba9eff871a49076c/src/brotli.erl#L65 My guess is that the encoder still has some data left and
:brotli_nif.encoder_take_output
has to be called more than once. From Brotli docs: