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

Avoid allocating memory for entire swf file in format.swf.Writer #107

Open
tobil4sk opened this issue Apr 7, 2023 · 0 comments
Open

Avoid allocating memory for entire swf file in format.swf.Writer #107

tobil4sk opened this issue Apr 7, 2023 · 0 comments

Comments

@tobil4sk
Copy link
Member

tobil4sk commented Apr 7, 2023

Currently, when running format.swf.Writer, at the end, a memory block the size of the entire swf file is allocated:

format/format/swf/Writer.hx

Lines 1450 to 1452 in 318e49a

var bytes = o.getBytes();
var size = bytes.length;
if( compressed ) bytes = format.tools.Deflate.run(bytes);

This can cause issues with large swfs, for example: openfl/lime#1307.

A possible better solution is to:

  1. Work out the size of the swf in advance, so we don't need to wait until the large byte buffer has been filled to figure out the length for the header. Here is some example code from the haxe repo that does this: https://github.com/HaxeFoundation/haxe/blob/731dcd71f10c495a5a820449249fbb3d4b40a7c1/libs/swflib/swfParser.ml#L2062-L2066.
  2. When the byte buffer exceeds a certain size, send the current bytes to the compression stream (using Compress.execute). The compressed bytes can be written to the file, and we can continue with emptied buffers. This removes the need for a monolithic allocation and compression call at the end.
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

No branches or pull requests

1 participant