optimizing_for_size.rst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. .. _doc_optimizing_for_size:
  2. Optimizing a build for size
  3. ===========================
  4. .. highlight:: shell
  5. Rationale
  6. ---------
  7. Sometimes, it is desired to optimize a build for size rather than speed.
  8. This means not compiling unused functions from the engine, as well as using
  9. specific compiler flags to aid on decreasing build size.
  10. Common situations include creating builds for mobile and Web platforms.
  11. This tutorial aims to give an overview on different methods to create
  12. a smaller binary. Before continuing, it is recommended to read the previous tutorials
  13. on compiling Godot for each platform.
  14. The options below are listed from the most important (greatest size savings)
  15. to the least important (lowest size savings).
  16. Stripping binaries
  17. ------------------
  18. - **Space savings:** Very high
  19. - **Difficulty:** Easy
  20. - **Performed in official builds:** Yes
  21. If you build Windows (MinGW), Linux or macOS binaries from source, remember to
  22. strip debug symbols from binaries by installing the ``strip`` package from your
  23. distribution then running:
  24. ::
  25. strip path/to/godot.binary
  26. On Windows, ``strip.exe`` is included in most MinGW toolchain setups.
  27. This will reduce the size of compiled binaries by a factor between 5× and 10×.
  28. The downside is that crash backtraces will no longer provide accurate information
  29. (which is useful for troubleshooting the cause of a crash).
  30. :ref:`C++ profilers <doc_using_cpp_profilers>` will also no longer be able to display
  31. function names (this does not affect the built-in GDScript profiler).
  32. .. note::
  33. The above command will not work on Windows binaries compiled with MSVC
  34. and platforms such as Android and Web. Instead, pass ``debug_symbols=no``
  35. on the SCons command line when compiling.
  36. Compiling with link-time optimization
  37. -------------------------------------
  38. - **Space savings:** High
  39. - **Difficulty:** Easy
  40. - **Performed in official builds:** Yes
  41. Enabling link-time optimization produces more efficient binaries, both in
  42. terms of performance and file size. It works by eliminating duplicate
  43. template functions and unused code. It can currently be used with the GCC
  44. and MSVC compilers:
  45. ::
  46. scons target=template_release lto=full
  47. Linking becomes much slower and more RAM-consuming with this option,
  48. so it should be used only for release builds:
  49. - When compiling the ``master`` branch, you need to have at least 8 GB of RAM
  50. available for successful linking with LTO enabled.
  51. - When compiling the ``3.x`` branch, you need to have at least 6 GB of RAM
  52. available for successful linking with LTO enabled.
  53. Optimizing for size instead of speed
  54. ------------------------------------
  55. - **Space savings:** High
  56. - **Difficulty:** Easy
  57. - **Performed in official builds:** Yes, but only for web builds
  58. Godot 3.1 onwards allows compiling using size optimizations (instead of speed).
  59. To enable this, set the ``optimize`` flag to ``size``:
  60. ::
  61. scons target=template_release optimize=size
  62. Some platforms such as WebAssembly already use this mode by default.
  63. Disabling advanced text server
  64. ------------------------------
  65. - **Space savings:** High
  66. - **Difficulty:** Easy
  67. - **Performed in official builds:** No
  68. By default, Godot uses an advanced text server with the support for the
  69. following features:
  70. - Right-to-left typesetting and complex scripts, required to write languages
  71. such as Arabic and Hebrew.
  72. - Font ligatures and OpenType features (such as small capitals, fractions and
  73. slashed zero).
  74. Godot provides a fallback text server that isn't compiled by default. This text
  75. server can be used as a lightweight alternative to the default advanced text
  76. server:
  77. ::
  78. scons target=template_release module_text_server_adv_enabled=no module_text_server_fb_enabled=yes
  79. If you only intend on supporting Latin, Greek and Cyrillic-based languages in
  80. your project, the fallback text server should suffice.
  81. This fallback text server can also process large amounts of text more quickly
  82. than the advanced text server. This makes the fallback text server a good fit
  83. for mobile/web projects.
  84. .. note::
  85. Remember to always pass ``module_text_server_fb_enabled=yes`` when using
  86. ``module_text_server_adv_enabled=no``. Otherwise, the compiled binary won't
  87. contain any text server, which means no text will be displayed at all when
  88. running the project.
  89. Disabling 3D
  90. ------------
  91. - **Space savings:** Moderate
  92. - **Difficulty:** Easy
  93. - **Performed in official builds:** No
  94. For 2D games, having the whole 3D engine available usually makes no sense.
  95. Because of this, there is a build flag to disable it:
  96. ::
  97. scons target=template_release disable_3d=yes
  98. Tools must be disabled in order to use this flag, as the editor is not designed
  99. to operate without 3D support. Without it, the binary size can be reduced
  100. by about 15%.
  101. Disabling advanced GUI objects
  102. ------------------------------
  103. - **Space savings:** Moderate
  104. - **Difficulty:** Easy
  105. - **Performed in official builds:** No
  106. Most small games don't require complex GUI controls such as Tree, ItemList,
  107. TextEdit or GraphEdit. They can be disabled using a build flag:
  108. ::
  109. scons target=template_release disable_advanced_gui=yes
  110. This is everything that will be disabled:
  111. - FileDialog
  112. - PopupMenu
  113. - Tree
  114. - TextEdit
  115. - CodeEdit
  116. - SyntaxHighlighter
  117. - CodeHighlighter
  118. - TreeItem
  119. - OptionButton
  120. - SpinBox
  121. - ColorPicker
  122. - ColorPickerButton
  123. - RichTextlabel
  124. - RichTextEffect
  125. - CharFXTransform
  126. - AcceptDialog
  127. - ConfirmationDialog
  128. - MarginContainer
  129. - SubViewportContainer
  130. - SplitContainer
  131. - HSplitContainer
  132. - VSplitContainer
  133. - GraphNode
  134. - GraphEdit
  135. Disabling unwanted modules
  136. --------------------------
  137. - **Space savings:** Very low to moderate depending on modules
  138. - **Difficulty:** Medium to hard depending on modules
  139. - **Performed in official builds:** No
  140. A lot of Godot's functions are offered as modules.
  141. You can see a list of modules with the following command:
  142. ::
  143. scons --help
  144. The list of modules that can be disabled will appear, together with all
  145. build options. If you are working on a simple 2D game, you could disable
  146. a lot of them:
  147. ::
  148. scons target=template_release module_basis_universal_enabled=no module_bmp_enabled=no module_camera_enabled=no module_csg_enabled=no module_dds_enabled=no module_enet_enabled=no module_gridmap_enabled=no module_hdr_enabled=no module_jsonrpc_enabled=no module_ktx_enabled=no module_mbedtls_enabled=no module_meshoptimizer_enabled=no module_minimp3_enabled=no module_mobile_vr_enabled=no module_msdfgen_enabled=no module_multiplayer_enabled=no module_noise_enabled=no module_navigation_enabled=no module_ogg_enabled=no module_openxr_enabled=no module_raycast_enabled=no module_regex_enabled=no module_squish_enabled=no module_svg_enabled=no module_tga_enabled=no module_theora_enabled=no module_tinyexr_enabled=no module_upnp_enabled=no module_vhacd_enabled=no module_vorbis_enabled=no module_webrtc_enabled=no module_websocket_enabled=no module_webxr_enabled=no module_zip_enabled=no
  149. If this proves not to work for your use case, you should review the list of
  150. modules and see which ones you actually still need for your game (e.g. you might
  151. want to keep networking-related modules, regex support,
  152. ``minimp3``/``ogg``/``vorbis`` to play music, or ``theora`` to play videos).
  153. Alternatively, you can supply a list of disabled modules by creating
  154. ``custom.py`` at the root of the source, with the contents similar to the
  155. following:
  156. .. code-block:: python
  157. # custom.py
  158. module_basis_universal_enabled = "no"
  159. module_bmp_enabled = "no"
  160. module_camera_enabled = "no"
  161. module_csg_enabled = "no"
  162. module_dds_enabled = "no"
  163. module_enet_enabled = "no"
  164. module_gridmap_enabled = "no"
  165. module_hdr_enabled = "no"
  166. module_jsonrpc_enabled = "no"
  167. module_ktx_enabled = "no"
  168. module_mbedtls_enabled = "no"
  169. module_meshoptimizer_enabled = "no"
  170. module_minimp3_enabled = "no"
  171. module_mobile_vr_enabled = "no"
  172. module_msdfgen_enabled= "no"
  173. module_multiplayer_enabled = "no"
  174. module_noise_enabled = "no"
  175. module_navigation_enabled = "no"
  176. module_ogg_enabled = "no"
  177. module_openxr_enabled = "no"
  178. module_raycast_enabled = "no"
  179. module_regex_enabled = "no"
  180. module_squish_enabled = "no"
  181. module_svg_enabled = "no"
  182. module_tga_enabled = "no"
  183. module_theora_enabled = "no"
  184. module_tinyexr_enabled = "no"
  185. module_upnp_enabled = "no"
  186. module_vhacd_enabled = "no"
  187. module_vorbis_enabled = "no"
  188. module_webrtc_enabled = "no"
  189. module_websocket_enabled = "no"
  190. module_webxr_enabled = "no"
  191. module_zip_enabled = "no"
  192. .. seealso::
  193. :ref:`doc_overriding_build_options`.
  194. Optimizing the distribution of your project
  195. -------------------------------------------
  196. Desktop
  197. ^^^^^^^
  198. .. note::
  199. This section is only relevant when distributing the files on a desktop
  200. platform that doesn't perform its own compression or packing. As such, this
  201. advice is relevant when you distribute ZIP archives on itch.io or GitHub
  202. Releases.
  203. Platforms like Steam already apply their own compression scheme, so you
  204. don't need to create a ZIP archive to distribute files in the first place.
  205. As an aside, you can look into optimizing the distribution of your project itself.
  206. This can be done even without recompiling the export template.
  207. `7-Zip <https://7-zip.org/>`__ can be used to create ZIP archives that are more
  208. efficient than usual, while remaining compatible with every ZIP extractor
  209. (including Windows' own built-in extractor). ZIP size reduction in a large
  210. project can reach dozens of megabytes compared to a typical ZIP compressor,
  211. although average savings are in the 1-5 MB range. Creating this ZIP archive will
  212. take longer than usual, but it will extract just as fast as any other ZIP
  213. archive.
  214. When using the 7-Zip GUI, this is done by creating a ZIP archive with the Ultra
  215. compression mode. When using the command line, this is done using the following
  216. command:
  217. ::
  218. 7z a -mx9 my_project.zip folder_containing_executable_and_pck
  219. Web
  220. ^^^
  221. Enabling gzip or Brotli compression for all file types from the web export
  222. (especially the ``.wasm`` and ``.pck``) can reduce the download size
  223. significantly, leading to faster loading times, especially on slow connections.
  224. Creating precompressed gzip or Brotli files with a high compression level can be
  225. even more efficient, as long as the web server is configured to serve those
  226. files when they exist. When supported, Brotli should be preferred over gzip as
  227. it has a greater potential for file size reduction.
  228. See :ref:`doc_exporting_for_web_serving_the_files` for instructions.