optimizing_for_size.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. You need to have at least
  49. 8 GB of RAM available for successful linking with LTO enabled. Since the operating
  50. system and programs will take up some RAM, in practice, you need 12 GB of RAM
  51. installed in your system (preferably 16 GB) to compile Godot with LTO enabled.
  52. Optimizing for size instead of speed
  53. ------------------------------------
  54. - **Space savings:** High
  55. - **Difficulty:** Easy
  56. - **Performed in official builds:** Yes, but only for web builds
  57. Godot 3.1 onwards allows compiling using size optimizations (instead of speed).
  58. To enable this, set the ``optimize`` flag to ``size``:
  59. ::
  60. scons target=template_release optimize=size
  61. Some platforms such as WebAssembly already use this mode by default.
  62. Godot 4.5 introduced the ``size_extra`` option, which can further reduce size.
  63. ::
  64. scons target=template_release optimize=size_extra
  65. Disabling advanced text server
  66. ------------------------------
  67. - **Space savings:** High
  68. - **Difficulty:** Easy
  69. - **Performed in official builds:** No
  70. By default, Godot uses an advanced text server with the support for the
  71. following features:
  72. - Right-to-left typesetting and complex scripts, required to write languages
  73. such as Arabic and Hebrew.
  74. - Font ligatures and OpenType features (such as small capitals, fractions and
  75. slashed zero).
  76. Godot provides a fallback text server that isn't compiled by default. This text
  77. server can be used as a lightweight alternative to the default advanced text
  78. server:
  79. ::
  80. scons target=template_release module_text_server_adv_enabled=no module_text_server_fb_enabled=yes
  81. If you only intend on supporting Latin, Greek and Cyrillic-based languages in
  82. your project, the fallback text server should suffice.
  83. This fallback text server can also process large amounts of text more quickly
  84. than the advanced text server. This makes the fallback text server a good fit
  85. for mobile/web projects.
  86. .. note::
  87. Remember to always pass ``module_text_server_fb_enabled=yes`` when using
  88. ``module_text_server_adv_enabled=no``. Otherwise, the compiled binary won't
  89. contain any text server, which means no text will be displayed at all when
  90. running the project.
  91. Disabling 3D
  92. ------------
  93. - **Space savings:** Moderate
  94. - **Difficulty:** Easy
  95. - **Performed in official builds:** No
  96. For 2D games, having the whole 3D engine available usually makes no sense.
  97. Because of this, there is a build flag to disable it:
  98. ::
  99. scons target=template_release disable_3d=yes
  100. Tools must be disabled in order to use this flag, as the editor is not designed
  101. to operate without 3D support. Without it, the binary size can be reduced
  102. by about 15%.
  103. Disabling advanced GUI objects
  104. ------------------------------
  105. - **Space savings:** Moderate
  106. - **Difficulty:** Easy
  107. - **Performed in official builds:** No
  108. Most small games don't require complex GUI controls such as Tree, ItemList,
  109. TextEdit or GraphEdit. They can be disabled using a build flag:
  110. ::
  111. scons target=template_release disable_advanced_gui=yes
  112. This is everything that will be disabled:
  113. - :ref:`class_AcceptDialog`
  114. - :ref:`class_CharFXTransform`
  115. - :ref:`class_CodeEdit`
  116. - :ref:`class_CodeHighlighter`
  117. - :ref:`class_ColorPicker`
  118. - :ref:`class_ColorPickerButton`
  119. - :ref:`class_ConfirmationDialog`
  120. - :ref:`class_FileDialog`
  121. - :ref:`class_FoldableContainer`
  122. - :ref:`class_FoldableGroup`
  123. - :ref:`class_GraphEdit`
  124. - :ref:`class_GraphElement`
  125. - :ref:`class_GraphFrame`
  126. - :ref:`class_GraphNode`
  127. - :ref:`class_HSplitContainer`
  128. - :ref:`class_MenuBar`
  129. - :ref:`class_MenuButton`
  130. - :ref:`class_OptionButton`
  131. - :ref:`class_PopupMenu` (will make all popup menus unavailable in code for classes that use them,
  132. like :ref:`class_LineEdit`, even though those classes are still available)
  133. - :ref:`class_RichTextEffect`
  134. - :ref:`class_RichTextLabel`
  135. - :ref:`class_SpinBox`
  136. - :ref:`class_SplitContainer`
  137. - :ref:`class_SubViewportContainer`
  138. - :ref:`class_SyntaxHighlighter`
  139. - :ref:`class_TextEdit`
  140. - :ref:`class_Tree`
  141. - :ref:`class_TreeItem`
  142. - :ref:`class_VSplitContainer`
  143. Disabling physics engines
  144. -------------------------
  145. - **Space savings:** Low to moderate
  146. - **Difficulty:** Easy
  147. - **Performed in official builds:** No
  148. If your 3D project uses Jolt Physics, you can disable GodotPhysics3D at compile-time as
  149. it will never be used:
  150. ::
  151. scons target=template_release module_godot_physics_3d_enabled=no
  152. Inversely, if your 3D project uses GodotPhysics3D, you can disable Jolt Physics at compile-time:
  153. ::
  154. scons target=template_release module_jolt_enabled=no
  155. If your project uses 3D rendering but not physics (or 2D rendering but not physics),
  156. you can also disable 2D or 3D physics entirely. Most 3D projects can take advantage
  157. of this, as they don't make use of 2D physics:
  158. ::
  159. scons target=template_release disable_physics_2d=yes
  160. ::
  161. scons target=template_release disable_physics_3d=yes
  162. Disabling unwanted modules
  163. --------------------------
  164. - **Space savings:** Very low to moderate depending on modules
  165. - **Difficulty:** Medium to hard depending on modules
  166. - **Performed in official builds:** No
  167. A lot of Godot's functions are offered as modules.
  168. You can see a list of modules with the following command:
  169. ::
  170. scons --help
  171. The list of modules that can be disabled will appear, together with all
  172. build options. If you are working on a simple 2D game, you could disable
  173. a lot of them:
  174. ::
  175. scons target=template_release module_astcenc_enabled=no module_basis_universal_enabled=no module_bcdec_enabled=no module_bmp_enabled=no module_camera_enabled=no module_csg_enabled=no module_dds_enabled=no module_enet_enabled=no module_etcpak_enabled=no module_fbx_enabled=no module_gltf_enabled=no module_gridmap_enabled=no module_hdr_enabled=no module_interactive_music_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_2d_enabled=no module_navigation_3d_enabled=no module_ogg_enabled=no module_openxr_enabled=no module_raycast_enabled=no module_regex_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
  176. If this proves not to work for your use case, you should review the list of
  177. modules and see which ones you actually still need for your game (e.g. you might
  178. want to keep networking-related modules, regex support,
  179. ``minimp3``/``ogg``/``vorbis`` to play music, or ``theora`` to play videos).
  180. Alternatively, you can supply a list of disabled modules by creating
  181. ``custom.py`` at the root of the source, with the contents similar to the
  182. following:
  183. .. code-block:: python
  184. :caption: custom.py
  185. module_astcenc_enabled = "no"
  186. module_basis_universal_enabled = "no"
  187. module_bcdec_enabled = "no"
  188. module_bmp_enabled = "no"
  189. module_camera_enabled = "no"
  190. module_csg_enabled = "no"
  191. module_dds_enabled = "no"
  192. module_enet_enabled = "no"
  193. module_etcpak_enabled = "no"
  194. module_fbx_enabled = "no"
  195. module_gltf_enabled = "no"
  196. module_gridmap_enabled = "no"
  197. module_hdr_enabled = "no"
  198. module_interactive_music_enabled = "no"
  199. module_jsonrpc_enabled = "no"
  200. module_ktx_enabled = "no"
  201. module_mbedtls_enabled = "no"
  202. module_meshoptimizer_enabled = "no"
  203. module_minimp3_enabled = "no"
  204. module_mobile_vr_enabled = "no"
  205. module_msdfgen_enabled = "no"
  206. module_multiplayer_enabled = "no"
  207. module_noise_enabled = "no"
  208. module_navigation_2d_enabled = "no"
  209. module_navigation_3d_enabled = "no"
  210. module_ogg_enabled = "no"
  211. module_openxr_enabled = "no"
  212. module_raycast_enabled = "no"
  213. module_regex_enabled = "no"
  214. module_svg_enabled = "no"
  215. module_tga_enabled = "no"
  216. module_theora_enabled = "no"
  217. module_tinyexr_enabled = "no"
  218. module_upnp_enabled = "no"
  219. module_vhacd_enabled = "no"
  220. module_vorbis_enabled = "no"
  221. module_webrtc_enabled = "no"
  222. module_websocket_enabled = "no"
  223. module_webxr_enabled = "no"
  224. module_zip_enabled = "no"
  225. .. seealso::
  226. :ref:`doc_overriding_build_options`.
  227. Optimizing the distribution of your project
  228. -------------------------------------------
  229. Desktop
  230. ~~~~~~~
  231. .. note::
  232. This section is only relevant when distributing the files on a desktop
  233. platform that doesn't perform its own compression or packing. As such, this
  234. advice is relevant when you distribute ZIP archives on itch.io or GitHub
  235. Releases.
  236. Platforms like Steam already apply their own compression scheme, so you
  237. don't need to create a ZIP archive to distribute files in the first place.
  238. As an aside, you can look into optimizing the distribution of your project itself.
  239. This can be done even without recompiling the export template.
  240. `7-Zip <https://7-zip.org/>`__ can be used to create ZIP archives that are more
  241. efficient than usual, while remaining compatible with every ZIP extractor
  242. (including Windows' own built-in extractor). ZIP size reduction in a large
  243. project can reach dozens of megabytes compared to a typical ZIP compressor,
  244. although average savings are in the 1-5 MB range. Creating this ZIP archive will
  245. take longer than usual, but it will extract just as fast as any other ZIP
  246. archive.
  247. When using the 7-Zip GUI, this is done by creating a ZIP archive with the Ultra
  248. compression mode. When using the command line, this is done using the following
  249. command:
  250. ::
  251. 7z a -mx9 my_project.zip folder_containing_executable_and_pck
  252. Web
  253. ~~~
  254. Enabling gzip or Brotli compression for all file types from the web export
  255. (especially the ``.wasm`` and ``.pck``) can reduce the download size
  256. significantly, leading to faster loading times, especially on slow connections.
  257. Creating precompressed gzip or Brotli files with a high compression level can be
  258. even more efficient, as long as the web server is configured to serve those
  259. files when they exist. When supported, Brotli should be preferred over gzip as
  260. it has a greater potential for file size reduction.
  261. See :ref:`doc_exporting_for_web_serving_the_files` for instructions.