optimizing_for_size.rst 13 KB

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