最佳化建置檔案大小¶
理由¶
有時候,我們會優先最佳化建置大小而不是速度。也就是不編譯引擎內沒用到的功能,以及使用特定的編譯旗標來降低建置的大小。常見的情況如為行動平台與 Web 平台。
本教學旨在提供各種能縮小二進位檔的方法概覽。繼續之前,建議先閱讀為各個平台編譯 Godot 的教學。
The options below are listed from the most important (greatest size savings) to the least important (lowest size savings).
在二進位檔中移除¶
Space savings: Very high
Difficulty: Easy
Performed in official builds: Yes
If you build Windows (MinGW), Linux or macOS binaries from source, remember to
strip debug symbols from binaries by installing the strip
package from your
distribution then running:
strip path/to/godot.binary
On Windows, strip.exe
is included in most MinGW toolchain setups.
This will reduce the size of compiled binaries by a factor between 5× and 10×. The downside is that crash backtraces will no longer provide accurate information (which is useful for troubleshooting the cause of a crash). C++ profilers will also no longer be able to display function names (this does not affect the built-in GDScript profiler).
備註
The above command will not work on Windows binaries compiled with MSVC
and platforms such as Android and HTML5. Instead, pass debug_symbols=no
on the SCons command line when compiling.
最佳化大小而非速度¶
Space savings: High
Difficulty: Easy
Performed in official builds: Yes, but only for HTML5
Godot 3.1 後可進行大小最佳化 (而非速度)。若要啟用大小最佳化,請將 optimize
旗標設為 size
:
scons p=windows target=release tools=no optimize=size
部分平台,如 WebAssembly,已預設使用此模式。
以連結時期最佳化進行編譯¶
Space savings: High
Difficulty: Easy
Performed in official builds: Yes
開啟連結時期最佳化可產生效能與檔案大小都更有效率的二進位檔。連結時期最佳化是通過清理重複的樣板功能與為使用的程式碼來達成的。目前可以在 GCC 與 MSVC 編譯器上使用:
scons p=windows target=release tools=no use_lto=yes
Linking becomes much slower and more RAM-consuming with this option, so it should be used only for release builds:
When compiling the
master
branch, you need to have at least 8 GB of RAM available for successful linking with LTO enabled.When compiling the
3.x
branch, you need to have at least 6 GB of RAM available for successful linking with LTO enabled.
禁用 3D¶
Space savings: Moderate
Difficulty: Easy
Performed in official builds: No
對於 2D 遊戲來說,包含完整的 3D 引擎通常沒什麼意義。因此,可以使用一個建置旗標來禁用:
scons p=windows target=release tools=no disable_3d=yes
必須要禁用 Tools 才能使用該旗標,因為編輯器並非設計給不支援 3D 的情景。關閉 3D 支援後,二進位檔的大小約可以減少 15%。
Disabling advanced GUI objects¶
Space savings: Moderate
Difficulty: Easy
Performed in official builds: No
大多數小型遊戲都不需要如 Tree, ItemList, TextEdit 或 GraphEdit 等的複雜 GUI 控制。可以通過一個建置旗標來禁用:
scons p=windows target=release tools=no disable_advanced_gui=yes
This is everything that will be disabled:
FileDialog
PopupMenu
Tree
TextEdit
TreeItem
OptionButton
SpinBox
ColorPicker
ColorPickerButton
RichTextLabel
RichTextEffect
CharFXTransform
PopupDialog
WindowDialog
AcceptDialog
ConfirmationDialog
MarginContainer
ViewportContainer
SplitContainer
HSplitContainer
GraphNode
GraphEdit
禁用不需要的模組¶
Space savings: Very low to moderate depending on modules
Difficulty: Medium to hard depending on modules
Performed in official builds: No
許多 Godot 的功能都以模組的形式提供,可以通過下列指令來檢視模組列表:
scons --help
會顯示能禁用的模組列表,以及所有建置選項。若正在開發一款簡易的 2D 遊戲,可以禁用其中許多的模組:
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
若確定這個設定與你的需求不合時,可以看看模組列表,找找其中是否有你的遊戲需要的功能 (如,與網路相關的模組、正規表示式支援以及用於播放影片的 Theora/WebM)。
此外,也可以通過在原始碼根目錄建立 custom.py
來提供要禁用的模組列表,其內容類似如下:
# custom.py
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"
也參考
複寫建置選項 。