Skip to content

Add zlib:compress/1 #1570

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jakub-gonet
Copy link
Contributor

https://www.erlang.org/doc/apps/erts/zlib.html#compress/1

These changes are made under both the "Apache 2.0" and the "GNU Lesser General
Public License 2.1 or later" license terms (dual license).

SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later

Copy link
Collaborator

@bettio bettio left a comment

Choose a reason for hiding this comment

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

I left some comments, in addition I think adding a module for typespecs might be a good addition.

@@ -2411,59 +2421,69 @@ static term nif_erlang_float_to_list(Context *ctx, int argc, term argv[])
return make_list_from_ascii_buf((uint8_t *) float_buf, len, ctx);
}

static term nif_erlang_list_to_binary_1(Context *ctx, int argc, term argv[])
static term list_to_binary(term list, term *ret, Context *ctx)
Copy link
Collaborator

Choose a reason for hiding this comment

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

If we want to keep this approach I really suggest renaming this function to something like: list_to_binary_maybe_gc so the caller will be aware about the fact that after a call to this function, any non trivial term will be invalid.

if (result != OK_ATOM) {
RAISE_ERROR(result);
}
to_compress = to_compress_binary;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't really like this approach: we are basically creating a binary (with the same content of the iolist that has been given as argument) on the caller heap that will turn into garbage very soon.
Simple approach: factor out a function that allocates and populates a C buffer.
Cool approach: using iolist folding fun for compressing input piece by piece, without copying data to a temporary buffer.

ok = test_zlib_compress_iolist(),
0.

test_zlib_compress_binary() ->
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's also test errors, such as: zlib:compress([900]) and zlib:compress([{}]).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants