Godot Engine – Multi-platform 2D and 3D game engine
[Editor] Use single ZSTD blob for editor translations and other compressed data.
Pāvels Nadtočajevsc35c03d092d5
diff --git a/.github/actions/godot-deps/action.yml b/.github/actions/godot-deps/action.ymlindex 2088797c9d1..712ca011879 100644--- a/.github/actions/godot-deps/action.yml+++ b/.github/actions/godot-deps/action.yml@@ -27,7 +27,7 @@ runs:shell: bashrun: |python -c "import sys; print(sys.version)"- python -m pip install scons==${{ inputs.scons-version }}+ python -m pip install scons==${{ inputs.scons-version }} zstandardscons --version- name: Setup problem matchersdiff --git a/core/core_builders.py b/core/core_builders.pyindex c9d76eb0687..b596aa3deb8 100644--- a/core/core_builders.py+++ b/core/core_builders.py@@ -74,7 +74,7 @@ uint8_t script_encryption_key[32] = {{def make_certs_header(target, source, env):buffer = methods.get_buffer(str(source[0]))decomp_size = len(buffer)- buffer = methods.compress_buffer(buffer)+ buffer = methods.compress_buffer_zstd(buffer, env)with methods.generated_wrapper(str(target[0])) as file:# System certs path. Editor will use them if defined. (for package maintainers)diff --git a/core/extension/make_interface_dumper.py b/core/extension/make_interface_dumper.pyindex 024ac712ef1..82f55f3efdf 100644--- a/core/extension/make_interface_dumper.py+++ b/core/extension/make_interface_dumper.py@@ -4,7 +4,7 @@ import methodsdef run(target, source, env):buffer = methods.get_buffer(str(source[0]))decomp_size = len(buffer)- buffer = methods.compress_buffer(buffer)+ buffer = methods.compress_buffer_zstd(buffer, env)with methods.generated_wrapper(str(target[0])) as file:file.write(f"""\@@ -25,7 +25,7 @@ class GDExtensionInterfaceDump {{static Vector<uint8_t> load_gdextension_interface_file() {{Vector<uint8_t> data;data.resize(_gdextension_interface_data_uncompressed_size);- int ret = Compression::decompress(data.ptrw(), _gdextension_interface_data_uncompressed_size, _gdextension_interface_data_compressed, _gdextension_interface_data_compressed_size, Compression::MODE_DEFLATE);+ int ret = Compression::decompress(data.ptrw(), _gdextension_interface_data_uncompressed_size, _gdextension_interface_data_compressed, _gdextension_interface_data_compressed_size, Compression::MODE_ZSTD);ERR_FAIL_COND_V_MSG(ret == -1, Vector<uint8_t>(), "Compressed file is corrupt.");return data;}}diff --git a/editor/doc/doc_tools.cpp b/editor/doc/doc_tools.cppindex 00719c5a5c5..03838382a2b 100644--- a/editor/doc/doc_tools.cpp+++ b/editor/doc/doc_tools.cpp@@ -1873,7 +1873,7 @@ Error DocTools::save_classes(const String &p_default_path, const HashMap<String,Error DocTools::load_compressed(const uint8_t *p_data, int64_t p_compressed_size, int64_t p_uncompressed_size) {Vector<uint8_t> data;data.resize(p_uncompressed_size);- const int64_t ret = Compression::decompress(data.ptrw(), p_uncompressed_size, p_data, p_compressed_size, Compression::MODE_DEFLATE);+ const int64_t ret = Compression::decompress(data.ptrw(), p_uncompressed_size, p_data, p_compressed_size, Compression::MODE_ZSTD);ERR_FAIL_COND_V_MSG(ret == -1, ERR_FILE_CORRUPT, "Compressed file is corrupt.");class_list.clear();diff --git a/editor/editor_builders.py b/editor/editor_builders.pyindex 0c2d741130d..e4b79e0501d 100644--- a/editor/editor_builders.py+++ b/editor/editor_builders.py@@ -1,10 +1,6 @@"""Functions used to generate source files during build time"""-import osimport os.path-import subprocess-import tempfile-import uuidimport methods@@ -55,7 +51,7 @@ void register_exporter_types() {{def make_doc_header(target, source, env):buffer = b"".join([methods.get_buffer(src) for src in map(str, source)])decomp_size = len(buffer)- buffer = methods.compress_buffer(buffer)+ buffer = methods.compress_buffer_zstd(buffer, env)with methods.generated_wrapper(str(target[0])) as file:file.write(f"""\@@ -75,62 +71,41 @@ def make_translations(target, source, env):sorted_paths = sorted([src.abspath for src in source], key=lambda path: os.path.splitext(os.path.basename(path))[0])xl_names = []- msgfmt = env.Detect("msgfmt")- if not msgfmt:- methods.print_warning("msgfmt not found, using .po files instead of .mo")with methods.generated_wrapper(target_cpp) as file:+ offset = 0+ full_buffer = b""for path in sorted_paths:name = os.path.splitext(os.path.basename(path))[0]- # msgfmt erases non-translated messages, so avoid using it if exporting the POT.- if msgfmt and name != category:- mo_path = os.path.join(tempfile.gettempdir(), uuid.uuid4().hex + ".mo")- cmd = f'{msgfmt} "{path}" --no-hash -o "{mo_path}"'- try:- subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE).communicate()- buffer = methods.get_buffer(mo_path)- except OSError as e:- methods.print_warning(- f"msgfmt execution failed, using .po file instead of .mo: path={path!r}; [{e.__class__.__name__}] {e}"- )- buffer = methods.get_buffer(path)- finally:- try:- if os.path.exists(mo_path):- os.remove(mo_path)- except OSError as e:- # Do not fail the entire build if it cannot delete a temporary file.- methods.print_warning(- f"Could not delete temporary .mo file: path={mo_path!r}; [{e.__class__.__name__}] {e}"- )- else:- buffer = methods.get_buffer(path)- if name == category:- name = "source"-- decomp_size = len(buffer)- buffer = methods.compress_buffer(buffer)-- file.write(f"""\-inline constexpr const unsigned char _{category}_translation_{name}_compressed[] = {{- {methods.format_buffer(buffer, 1)}-}};+ buffer = methods.get_buffer(path)+ if name == category:+ name = "source"-""")+ full_buffer += buffer- xl_names.append([name, len(buffer), decomp_size])+ xl_names.append([name, len(buffer), offset])+ offset += len(buffer)++ decomp_size = len(full_buffer)+ full_buffer = methods.compress_buffer_zstd(full_buffer, env)file.write(f"""\#include "{target_h}"+const unsigned int _{category}_decomp_size = {decomp_size};+const unsigned int _{category}_comp_size = {len(full_buffer)};+const unsigned char _{category}_translation_compressed[] = {{+ {methods.format_buffer(full_buffer, 1)}+}};+const EditorTranslationList _{category}_translations[] = {{""")for x in xl_names:- file.write(f'\t{{ "{x[0]}", {x[1]}, {x[2]}, _{category}_translation_{x[0]}_compressed }},\n')+ file.write(f'\t{{ "{x[0]}", {x[1]}, {x[2]} }},\n')file.write("""\- { nullptr, 0, 0, nullptr },+ { nullptr, 0, 0 },};""")@@ -142,12 +117,14 @@ const EditorTranslationList _{category}_translations[] = {{struct EditorTranslationList {{const char* lang;- int comp_size;- int uncomp_size;- const unsigned char* data;+ unsigned int size;+ unsigned int offset;}};#endif // EDITOR_TRANSLATION_LIST+extern const unsigned int _{category}_decomp_size;+extern const unsigned int _{category}_comp_size;+extern const unsigned char _{category}_translation_compressed[];extern const EditorTranslationList _{category}_translations[];""")diff --git a/editor/translations/editor_translation.cpp b/editor/translations/editor_translation.cppindex 0e268a05645..6231f792e3a 100644--- a/editor/translations/editor_translation.cpp+++ b/editor/translations/editor_translation.cpp@@ -42,7 +42,7 @@Vector<String> get_editor_locales() {Vector<String> locales;- for (const EditorTranslationList *etl = _editor_translations; etl->data; etl++) {+ for (const EditorTranslationList *etl = _editor_translations; etl->lang; etl++) {const String &locale = etl->lang;locales.push_back(locale);}@@ -50,19 +50,21 @@ Vector<String> get_editor_locales() {return locales;}-static void _load(const Ref<TranslationDomain> p_domain, const String &p_locale, const EditorTranslationList *p_etl) {+static void _load(const Ref<TranslationDomain> p_domain, const String &p_locale, const EditorTranslationList *p_etl, const uint8_t *p_data, unsigned int p_decomp_size, unsigned int p_comp_size) {+ LocalVector<uint8_t> data;+ data.resize_uninitialized(p_decomp_size);+ const int64_t ret = Compression::decompress(data.ptr(), p_decomp_size, p_data, p_comp_size, Compression::MODE_ZSTD);+ ERR_FAIL_COND_MSG(ret == -1, "Compressed file is corrupt.");+String full_locale = p_locale;String base_locale = p_locale.get_slicec('@', 0).split("_")[0];- for (const EditorTranslationList *etl = p_etl; etl->data; etl++) {+ for (const EditorTranslationList *etl = p_etl; etl->lang; etl++) {if (etl->lang == full_locale || etl->lang == base_locale) {- LocalVector<uint8_t> data;- data.resize_uninitialized(etl->uncomp_size);- const int64_t ret = Compression::decompress(data.ptr(), etl->uncomp_size, etl->data, etl->comp_size, Compression::MODE_DEFLATE);- ERR_FAIL_COND_MSG(ret == -1, "Compressed file is corrupt.");+ ERR_CONTINUE(etl->offset + etl->size > data.size());Ref<FileAccessMemory> fa;fa.instantiate();- fa->open_custom(data.ptr(), data.size());+ fa->open_custom(data.ptr() + etl->offset, etl->size);Ref<Translation> tr = TranslationLoaderPO::load_translation(fa);if (tr.is_valid()) {@@ -78,36 +80,37 @@ void load_editor_translations(const String &p_locale) {domain = TranslationServer::get_singleton()->get_editor_domain();domain->clear();- _load(domain, p_locale, _editor_translations);- _load(domain, p_locale, _extractable_translations);+ _load(domain, p_locale, _editor_translations, (const uint8_t *)_editor_translation_compressed, _editor_decomp_size, _editor_comp_size);+ _load(domain, p_locale, _extractable_translations, (const uint8_t *)_extractable_translation_compressed, _extractable_decomp_size, _extractable_comp_size);domain = TranslationServer::get_singleton()->get_property_domain();domain->clear();- _load(domain, p_locale, _property_translations);+ _load(domain, p_locale, _property_translations, (const uint8_t *)_property_translation_compressed, _property_decomp_size, _property_comp_size);}void load_doc_translations(const String &p_locale) {const Ref<TranslationDomain> domain = TranslationServer::get_singleton()->get_doc_domain();domain->clear();- _load(domain, p_locale, _doc_translations);+ _load(domain, p_locale, _doc_translations, (const uint8_t *)_doc_translation_compressed, _doc_decomp_size, _doc_comp_size);}Vector<Vector<String>> get_extractable_message_list() {Vector<Vector<String>> list;- for (const EditorTranslationList *etl = _extractable_translations; etl->data; etl++) {+ LocalVector<uint8_t> data;+ data.resize_uninitialized(_extractable_decomp_size);+ const int64_t ret = Compression::decompress(data.ptr(), _extractable_decomp_size, _extractable_translation_compressed, _extractable_comp_size, Compression::MODE_ZSTD);+ ERR_FAIL_COND_V_MSG(ret == -1, list, "Compressed file is corrupt.");++ for (const EditorTranslationList *etl = _extractable_translations; etl->lang; etl++) {if (strcmp(etl->lang, "source")) {continue;}-- LocalVector<uint8_t> data;- data.resize_uninitialized(etl->uncomp_size);- const int64_t ret = Compression::decompress(data.ptr(), etl->uncomp_size, etl->data, etl->comp_size, Compression::MODE_DEFLATE);- ERR_FAIL_COND_V_MSG(ret == -1, list, "Compressed file is corrupt.");+ ERR_CONTINUE(etl->offset + etl->size > data.size());Ref<FileAccessMemory> fa;fa.instantiate();- fa->open_custom(data.ptr(), data.size());+ fa->open_custom(data.ptr() + etl->offset, etl->size);// Taken from TranslationLoaderPO, modified to work specifically with POTs.{diff --git a/methods.py b/methods.pyindex 6201253c71c..5e48f4beabf 100644--- a/methods.py+++ b/methods.py@@ -1581,6 +1581,29 @@ def get_buffer(path: str) -> bytes:return file.read()+def compress_buffer_zstd(buffer: bytes, env):+ # Levels selected for a good tradeoff between compression speed and size reduction,+ # being more aggressive on production builds.+ # Long-distance matching helps a lot with the editor translations on lower levels,+ # but costs some extra decompression time (+30%). It's not helping at high levels.+ level = 20 if env["production"] else 7+ ldm = False if env["production"] or env.dev_build else True+ if sys.version_info >= (3, 14):+ import compression.zstd++ CP = compression.zstd.CompressionParameter+ options = {CP.compression_level: level, CP.enable_long_distance_matching: ldm}+ return compression.zstd.compress(buffer, options=options)+ else:+ try:+ import zstandard+ except ImportError:+ print_error("Python versions earlier than 3.14 require the `zstandard` module")+ sys.exit(255)+ params = zstandard.ZstdCompressionParameters.from_level(level, enable_ldm=ldm)+ return zstandard.ZstdCompressor(compression_params=params).compress(buffer)++def compress_buffer(buffer: bytes) -> bytes:# Use maximum zlib compression level to further reduce file size# (at the cost of initial build times).diff --git a/modules/mbedtls/crypto_mbedtls.cpp b/modules/mbedtls/crypto_mbedtls.cppindex ec36b9b4b04..99b11ac5313 100644--- a/modules/mbedtls/crypto_mbedtls.cpp+++ b/modules/mbedtls/crypto_mbedtls.cpp@@ -464,7 +464,7 @@ void CryptoMbedTLS::load_default_certificates(const String &p_path) {// Use builtin certs if there are no system certs.PackedByteArray certs;certs.resize(_certs_uncompressed_size + 1);- const int64_t decompressed_size = Compression::decompress(certs.ptrw(), _certs_uncompressed_size, _certs_compressed, _certs_compressed_size, Compression::MODE_DEFLATE);+ const int64_t decompressed_size = Compression::decompress(certs.ptrw(), _certs_uncompressed_size, _certs_compressed, _certs_compressed_size, Compression::MODE_ZSTD);ERR_FAIL_COND_MSG(decompressed_size != _certs_uncompressed_size, "Error decompressing builtin CA certificates. Decompressed size did not match expected size.");certs.write[_certs_uncompressed_size] = 0; // Make sure it ends with string terminatordefault_certs->load_from_memory(certs.ptr(), certs.size());