optimizing_for_size.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. .. note::
  102. Disabling 3D support also disables all navigation. This includes 2D navigation,
  103. as it uses the 3D navigation system internally.
  104. Disabling advanced GUI objects
  105. ------------------------------
  106. - **Space savings:** Moderate
  107. - **Difficulty:** Easy
  108. - **Performed in official builds:** No
  109. Most small games don't require complex GUI controls such as Tree, ItemList,
  110. TextEdit or GraphEdit. They can be disabled using a build flag:
  111. ::
  112. scons target=template_release disable_advanced_gui=yes
  113. This is everything that will be disabled:
  114. - :ref:`class_AcceptDialog`
  115. - :ref:`class_CharFXTransform`
  116. - :ref:`class_CodeEdit`
  117. - :ref:`class_CodeHighlighter`
  118. - :ref:`class_ColorPickerButton`
  119. - :ref:`class_ColorPicker`
  120. - :ref:`class_ConfirmationDialog`
  121. - :ref:`class_FileDialog`
  122. - :ref:`class_GraphEdit`
  123. - :ref:`class_GraphElement`
  124. - :ref:`class_GraphFrame`
  125. - :ref:`class_GraphNode`
  126. - :ref:`class_HSplitContainer`
  127. - :ref:`class_MenuBar`
  128. - :ref:`class_MenuButton`
  129. - :ref:`class_OptionButton`
  130. - :ref:`class_PopupMenu` (will make all popup menus unavailable in code for classes that use them,
  131. like :ref:`class_LineEdit`, even though those classes are still available)
  132. - :ref:`class_RichTextEffect`
  133. - :ref:`class_RichTextLabel`
  134. - :ref:`class_SpinBox`
  135. - :ref:`class_SplitContainer`
  136. - :ref:`class_SubViewportContainer`
  137. - :ref:`class_SyntaxHighlighter`
  138. - :ref:`class_TextEdit`
  139. - :ref:`class_TreeItem`
  140. - :ref:`class_Tree`
  141. - :ref:`class_VSplitContainer`
  142. Disabling unwanted modules
  143. --------------------------
  144. - **Space savings:** Very low to moderate depending on modules
  145. - **Difficulty:** Medium to hard depending on modules
  146. - **Performed in official builds:** No
  147. A lot of Godot's functions are offered as modules.
  148. You can see a list of modules with the following command:
  149. ::
  150. scons --help
  151. The list of modules that can be disabled will appear, together with all
  152. build options. If you are working on a simple 2D game, you could disable
  153. a lot of them:
  154. ::
  155. 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
  156. If this proves not to work for your use case, you should review the list of
  157. modules and see which ones you actually still need for your game (e.g. you might
  158. want to keep networking-related modules, regex support,
  159. ``minimp3``/``ogg``/``vorbis`` to play music, or ``theora`` to play videos).
  160. Alternatively, you can supply a list of disabled modules by creating
  161. ``custom.py`` at the root of the source, with the contents similar to the
  162. following:
  163. .. code-block:: python
  164. :caption: custom.py
  165. module_basis_universal_enabled = "no"
  166. module_bmp_enabled = "no"
  167. module_camera_enabled = "no"
  168. module_csg_enabled = "no"
  169. module_dds_enabled = "no"
  170. module_enet_enabled = "no"
  171. module_gridmap_enabled = "no"
  172. module_hdr_enabled = "no"
  173. module_jsonrpc_enabled = "no"
  174. module_ktx_enabled = "no"
  175. module_mbedtls_enabled = "no"
  176. module_meshoptimizer_enabled = "no"
  177. module_minimp3_enabled = "no"
  178. module_mobile_vr_enabled = "no"
  179. module_msdfgen_enabled= "no"
  180. module_multiplayer_enabled = "no"
  181. module_noise_enabled = "no"
  182. module_navigation_enabled = "no"
  183. module_ogg_enabled = "no"
  184. module_openxr_enabled = "no"
  185. module_raycast_enabled = "no"
  186. module_regex_enabled = "no"
  187. module_squish_enabled = "no"
  188. module_svg_enabled = "no"
  189. module_tga_enabled = "no"
  190. module_theora_enabled = "no"
  191. module_tinyexr_enabled = "no"
  192. module_upnp_enabled = "no"
  193. module_vhacd_enabled = "no"
  194. module_vorbis_enabled = "no"
  195. module_webrtc_enabled = "no"
  196. module_websocket_enabled = "no"
  197. module_webxr_enabled = "no"
  198. module_zip_enabled = "no"
  199. .. seealso::
  200. :ref:`doc_overriding_build_options`.
  201. Optimizing the distribution of your project
  202. -------------------------------------------
  203. Desktop
  204. ^^^^^^^
  205. .. note::
  206. This section is only relevant when distributing the files on a desktop
  207. platform that doesn't perform its own compression or packing. As such, this
  208. advice is relevant when you distribute ZIP archives on itch.io or GitHub
  209. Releases.
  210. Platforms like Steam already apply their own compression scheme, so you
  211. don't need to create a ZIP archive to distribute files in the first place.
  212. As an aside, you can look into optimizing the distribution of your project itself.
  213. This can be done even without recompiling the export template.
  214. `7-Zip <https://7-zip.org/>`__ can be used to create ZIP archives that are more
  215. efficient than usual, while remaining compatible with every ZIP extractor
  216. (including Windows' own built-in extractor). ZIP size reduction in a large
  217. project can reach dozens of megabytes compared to a typical ZIP compressor,
  218. although average savings are in the 1-5 MB range. Creating this ZIP archive will
  219. take longer than usual, but it will extract just as fast as any other ZIP
  220. archive.
  221. When using the 7-Zip GUI, this is done by creating a ZIP archive with the Ultra
  222. compression mode. When using the command line, this is done using the following
  223. command:
  224. ::
  225. 7z a -mx9 my_project.zip folder_containing_executable_and_pck
  226. Web
  227. ^^^
  228. Enabling gzip or Brotli compression for all file types from the web export
  229. (especially the ``.wasm`` and ``.pck``) can reduce the download size
  230. significantly, leading to faster loading times, especially on slow connections.
  231. Creating precompressed gzip or Brotli files with a high compression level can be
  232. even more efficient, as long as the web server is configured to serve those
  233. files when they exist. When supported, Brotli should be preferred over gzip as
  234. it has a greater potential for file size reduction.
  235. See :ref:`doc_exporting_for_web_serving_the_files` for instructions.