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

Unlimited (100x) undo buffer with compression #201

Merged
merged 8 commits into from
Mar 24, 2024

Conversation

pascal-niklaus
Copy link
Contributor

This (small) PR implements LZ4 compression of the undo buffers.

LZ4 compression/decompression is very fast, and with the drawings I tested, achieves a compression down to 1.5-2.5% of the original buffer. Hence, this allows for a very deep undo buffer without significant memory penalty.
I have fixed it at 100 which for practical purposes is unlimited.

The entire logic of the undo system remains unaltered.

The only difference is that swap_surface includes a compression/decompression step.

The undo buffers are only allocated once they are needed, and they are successively grown when this is necessary.

@pascal-niklaus
Copy link
Contributor Author

Sorry, I have parts in the PR that were not meant to be in there. Not sure how this happens. I will tidy this up now...

Copy link
Owner

@bk138 bk138 left a comment

Choose a reason for hiding this comment

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

Very quick first review while commuting, probably more to come 😉

CMakeLists.txt Outdated Show resolved Hide resolved
src/main.c Outdated
cairo_set_operator(context->paint_ctx, CAIRO_OPERATOR_ATOP);
else /* GROMIT_PEN */
cairo_set_operator(context->paint_ctx, CAIRO_OPERATOR_OVER);
else if (type == GROMIT_RECOLOR)
Copy link
Owner

Choose a reason for hiding this comment

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

Seems unrelated.

void snap_undo_state (GromitData *data)
{
if(data->debug)
g_printerr ("DEBUG: Snapping undo buffer %d.\n", data->undo_head);

copy_surface(data->undobuffer[data->undo_head], data->backbuffer);
undo_compress(data, data->backbuffer);
Copy link
Owner

Choose a reason for hiding this comment

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

Why not have compression and saving in one function?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

...because it is a swap operation which requires three steps, and these steps are combined differently in snap_.... and in undo/redo...

In snap_undo_state we have

undo_compress(data, data->backbuffer);            // data -> temp[LZ4]
undo_temp_buffer_to_slot(data, data->undo_head);  // temp[LZ] -> undo_slot[LZ4]

In undo/redo we have the 3-step swapping:

undo_compress(data, data->backbuffer);                     // data -> temp[LZ4]
undo_decompress(data, data->undo_head, data->backbuffer);  // undo_slot[i,LZ4]  -> data
undo_temp_buffer_to_slot(data, data->undo_head);           // temp[LZ4] -> undo_slot[i,LZ4]

The other aspect is that temp_buffer (step 1) is generously grown when it is too small (it is doubled), whereas the undo_buffers are only every grown to the size needed. The doubling is not critical because there is only one instance of temp_buffer, whereas there are 100 instances of the undo_buffer

src/main.h Outdated
@@ -193,4 +201,5 @@ void paint_context_free (GromitPaintContext *context);

void indicate_active(GromitData *data, gboolean YESNO);

Copy link
Owner

Choose a reason for hiding this comment

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

Seems unused.

@bk138 bk138 merged commit 4a19157 into bk138:master Mar 24, 2024
1 check passed
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.

2 participants