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

Minify projects #148

Open
GarboMuffin opened this issue Sep 7, 2021 · 6 comments
Open

Minify projects #148

GarboMuffin opened this issue Sep 7, 2021 · 6 comments
Labels
enhancement New feature or request

Comments

@GarboMuffin
Copy link
Member

Scratch projects tend to be very large, we should try to address that with lossy compression that doesn't affect project behavior

  • project.json: We should be able to very aggressively compress by pretending the editor doesn't exist
  • *.png: we could throw the files into a lossless compressor eg. oxipng
  • *.svg: we could throw the files into svgo with a few things turned off
@CST1229
Copy link

CST1229 commented Sep 7, 2021

Something that might be worth adding is something like the project.json size reducer, especially compressing identifiers.

@GarboMuffin
Copy link
Member Author

That's vaguely what I'm referring to for project.json. if we pretend that the editor doesn't exist and decide to effectively become a project obfuscator, we can do some much, much more aggressive things than that project

@GarboMuffin GarboMuffin added the enhancement New feature or request label Sep 8, 2021
@apple502j
Copy link

Compressions that do not affect the editor (thus can be applied to all projects):

  • ID shortening
  • Trimming meta.agent and meta.vm (they are not required)
  • Using literal true/false in block.mutation
  • Using numbers in num_primitive and text_primitive if it allows
  • Using default values

Compressions that affect the editor in some way (but acceptable in immutable projects):

  • Removing comments
  • Removing broadcasts and local variables so that the fallback code will create it in the runtime
  • Removing invisible monitors
  • Removing extensions. This is used in WWW but not in VM, loading extension relies on opcode.
  • Removing block.x/block.y
  • Removing shadow block
  • Removing unused variables or lists
  • Removing dead code/orphaned blocks

Compression that violates the Scratch 3.0 file format and VM implementation but is acceptable:

  • Shortening keys
  • Removing target.isStage; the first element of the list must be the stage so this is redundant
  • Removing asset.md5ext which is just ${assetId}.${dataFormat}
  • Using sequential ID instead of hash for filenames like Scratch 2.0
  • Storing color_primitive as CSS color, decimal value, RGB or RRGGBB, whichever is shorter
  • Flattening block.inputs
  • Changing block.mutation, currently this is JSON representation of XMLDOM
  • Using 1/0 instead of boolean
  • Removing name and layerOrder from the stage

GarboMuffin added a commit that referenced this issue Sep 8, 2021
This uses some disabled-by-default code from TW/scratch-vm to optimize projects
It's not optimal, but it's safe outside of the editor while still saving significant space
Part of #148
@Samq64
Copy link

Samq64 commented Sep 9, 2021

Another easy way to add lossy compression is to add an option to initialize all variables to '' and lists to []. That would save a lot of space, but it has to be optional because a lot of projects rely on those values and don't regenerate them.

@GarboMuffin
Copy link
Member Author

GarboMuffin commented Sep 9, 2021

sb3 project.json compression has landed

There's probably a couple hundred more KB that could be saved without breaking projects. Beyond that point, we would probably be better off making a binary file format instead of using JSON.

Note that many of these projects contain many large assets that make any project.json optimizations almost meaningless. Some of those will be investigated next.

@lisa-wolfgang
Copy link

Note that many of these projects contain many large assets that make any project.json optimizations almost meaningless.

Yes, especially audio files. Scratch projects can play .mp3 files as long as they have the same filenames as the .wav assets they were converted from, so automatically converting all audio to .mp3 at a constant (reduced) bitrate would be very beneficial. (I've already done this manually for a project and haven't had issues with editing the files either.)

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

No branches or pull requests

5 participants