optimizing_for_size.rst 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 HTML5. Instead, pass ``debug_symbols=no``
  35. on the SCons command line when compiling.
  36. Optimizing for size instead of speed
  37. ------------------------------------
  38. - **Space savings:** High
  39. - **Difficulty:** Easy
  40. - **Performed in official builds:** Yes, but only for HTML5
  41. Godot 3.1 onwards allows compiling using size optimizations (instead of speed).
  42. To enable this, set the ``optimize`` flag to ``size``:
  43. ::
  44. scons p=windows target=release tools=no optimize=size
  45. Some platforms such as WebAssembly already use this mode by default.
  46. Compiling with link-time optimization
  47. -------------------------------------
  48. - **Space savings:** High
  49. - **Difficulty:** Easy
  50. - **Performed in official builds:** Yes
  51. Enabling link-time optimization produces more efficient binaries, both in
  52. terms of performance and file size. It works by eliminating duplicate
  53. template functions and unused code. It can currently be used with the GCC
  54. and MSVC compilers:
  55. ::
  56. scons p=windows target=release tools=no use_lto=yes
  57. Linking becomes much slower and more RAM-consuming with this option,
  58. so it should be used only for release builds:
  59. - When compiling the ``master`` branch, you need to have at least 8 GB of RAM
  60. available for successful linking with LTO enabled.
  61. - When compiling the ``3.x`` branch, you need to have at least 6 GB of RAM
  62. available for successful linking with LTO enabled.
  63. Disabling 3D
  64. ------------
  65. - **Space savings:** Moderate
  66. - **Difficulty:** Easy
  67. - **Performed in official builds:** No
  68. For 2D games, having the whole 3D engine available usually makes no sense. Because of this, there is a build flag to disable it:
  69. ::
  70. scons p=windows target=release tools=no disable_3d=yes
  71. Tools must be disabled in order to use this flag, as the editor is not designed
  72. to operate without 3D support. Without it, the binary size can be reduced
  73. by about 15%.
  74. Disabling advanced GUI objects
  75. ------------------------------
  76. - **Space savings:** Moderate
  77. - **Difficulty:** Easy
  78. - **Performed in official builds:** No
  79. Most small games don't require complex GUI controls such as Tree, ItemList,
  80. TextEdit or GraphEdit. They can be disabled using a build flag:
  81. ::
  82. scons p=windows target=release tools=no disable_advanced_gui=yes
  83. This is everything that will be disabled:
  84. - FileDialog
  85. - PopupMenu
  86. - Tree
  87. - TextEdit
  88. - TreeItem
  89. - OptionButton
  90. - SpinBox
  91. - ColorPicker
  92. - ColorPickerButton
  93. - RichTextLabel
  94. - RichTextEffect
  95. - CharFXTransform
  96. - PopupDialog
  97. - WindowDialog
  98. - AcceptDialog
  99. - ConfirmationDialog
  100. - MarginContainer
  101. - ViewportContainer
  102. - SplitContainer
  103. - HSplitContainer
  104. - GraphNode
  105. - GraphEdit
  106. Disabling unwanted modules
  107. --------------------------
  108. - **Space savings:** Very low to moderate depending on modules
  109. - **Difficulty:** Medium to hard depending on modules
  110. - **Performed in official builds:** No
  111. A lot of Godot's functions are offered as modules.
  112. You can see a list of modules with the following command:
  113. ::
  114. scons --help
  115. The list of modules that can be disabled will appear, together with all
  116. build options. If you are working on a simple 2D game, you could disable
  117. a lot of them:
  118. ::
  119. scons p=windows target=release tools=no module_arkit_enabled=no module_assimp_enabled=no module_bmp_enabled=no module_bullet_enabled=no module_camera_enabled=no module_csg_enabled=no module_dds_enabled=no module_enet_enabled=no module_etc_enabled=no module_gdnative_enabled=no module_gridmap_enabled=no module_hdr_enabled=no module_jsonrpc_enabled=no module_mbedtls_enabled=no module_mobile_vr_enabled=no module_opensimplex_enabled=no module_opus_enabled=no module_pvr_enabled=no module_recast_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_webm_enabled=no module_webrtc_enabled=no module_websocket_enabled=no module_xatlas_unwrap_enabled=no
  120. If this proves not to work for your use case, you should review the list of
  121. modules and see which ones you actually still need for your game (e.g. you
  122. might want to keep networking-related modules, regex support, or theora/webm
  123. to play videos).
  124. Alternatively, you can supply a list of disabled modules by creating
  125. ``custom.py`` at the root of the source, with the contents similar to the
  126. following:
  127. .. code-block:: python
  128. # custom.py
  129. module_arkit_enabled = "no"
  130. module_assimp_enabled = "no"
  131. module_bmp_enabled = "no"
  132. module_bullet_enabled = "no"
  133. module_camera_enabled = "no"
  134. module_csg_enabled = "no"
  135. module_dds_enabled = "no"
  136. module_enet_enabled = "no"
  137. module_etc_enabled = "no"
  138. module_gdnative_enabled = "no"
  139. module_gridmap_enabled = "no"
  140. module_hdr_enabled = "no"
  141. module_jsonrpc_enabled = "no"
  142. module_mbedtls_enabled = "no"
  143. module_mobile_vr_enabled = "no"
  144. module_opensimplex_enabled = "no"
  145. module_opus_enabled = "no"
  146. module_pvr_enabled = "no"
  147. module_recast_enabled = "no"
  148. module_regex_enabled = "no"
  149. module_squish_enabled = "no"
  150. module_svg_enabled = "no"
  151. module_tga_enabled = "no"
  152. module_theora_enabled = "no"
  153. module_tinyexr_enabled = "no"
  154. module_upnp_enabled = "no"
  155. module_vhacd_enabled = "no"
  156. module_vorbis_enabled = "no"
  157. module_webm_enabled = "no"
  158. module_webrtc_enabled = "no"
  159. module_websocket_enabled = "no"
  160. module_xatlas_unwrap_enabled = "no"
  161. .. seealso::
  162. :ref:`doc_overriding_build_options`.