1 The size of special effects packages for TikTok1.1 What is package size in one sentence?Package size mainly refers to the size of the application installation package, such as the installation size displayed by the installation package in the App Store. 1.2 Why should we optimize the package size?As the capabilities of applications are updated and iterated, the size of application installation packages will gradually increase, the traffic consumption and charges for users downloading applications will further increase, and the user's willingness to download will decrease relatively. On the other hand, as the package size increases, the time to install the application will become relatively longer, affecting the user experience. For low-end mobile phones with smaller ROMs, the application will occupy more memory after decompression, and some mobile phone managers will prompt insufficient memory and prompt uninstallation, which directly affects user usage. 1.3 Contribution of special effects to package size in TikTokDouyin currently consists of multiple business lines, each of which plays a role similar to the middle platform. The special effects middle platform is one of the links of Douyin. Currently, special effects are aggregated from effect and lab into EffectSDK, which accounts for a proportion of the settlement package volume in Douyin as an independent business line. 1.4 Package volume composition on the special effects sideThe package size of EffectSDK consists of two parts: binary files (i.e. executable files) and other resource files (images, configuration files, etc.). Binary files are mainly executable files generated by code, and resource files refer to built-in model files, material files, configuration files, etc. As a middle platform, the binary code in the special effects EffectSDK occupies the vast majority of the volume. Different from the package size optimization ideas of applications such as Douyin and Toutiao, special effects can do relatively little in terms of resource compression. Since special effects are used as a middle platform to support Douyin's business, and provide special effects capabilities in the form of a library, there is a lot of room for deleting useless resources, removing useless codes, and optimizing code. Therefore, the performance optimization on the special effects side mainly focuses on minimizing the package size while supporting multiple functions, improving code quality, and achieving a balance between code efficiency and code volume. 2 Background knowledge of package size optimizationThe special effects side in Douyin is supported by C++ code writing, which generates a static library after compilation and finally links to the executable file. In the process from code to binary file, the compiler does the preprocessing, compilation, assembly, linking and other processes for us. Finally, the Android side generates ELF format files and the iOS side generates Mach-O files. There are four types of ELF format files, including relocatable files (Relocatable File), executable files (Executable File), shared object files (Shared Object File), and core dump files (Core Dump File). Among them, the shared object file, that is, the xxx.so file, contains code and data that can be linked in two contexts. The link editor can process it together with other relocatable files and shared object files to generate another object file; in addition, the dynamic linker (Dynamic Linker) may combine it with an executable file and other shared objects to create a process image. The special effects side supports Douyin's special effects shooting capabilities in the form of a shared object file (libeffect.so). Since ELF files are involved in program linking and execution, there are usually two views: one is the link view and the other is the execution view (the left figure below); the compiler and linker will form a collection of sections according to the link view, with sections as units, and the section header table (section header table); the loader will follow the execution view, with the file as a collection of segments according to the program header table (program header table) in units of segments. Usually, a relocatable file (xxx.o) will contain a section header table, an executable file (xxx.exe) will contain a program header table, and a shared object file (xxx.so) will contain both. The following is the section information in effect_sdk.so viewed using the binutils tool: $ greatelf -h libeffect_sdk.so Usually each section is responsible for different functions and is stored in different locations. The size of the section is a reflection of the size of the compiled code. In the final analysis, the final package size of the special effects side is determined by the size of the section and headers. Optimizing the package size means optimizing the code writing efficiency and compilation method, and reducing the size of each section. int gInitVar = 24; //-- .data section 3 Package size optimization tipsAfter understanding the basic composition of package size, we can make targeted adjustments to the compilation options and code to optimize the package size. Both iOS and Android can optimize the code size by optimizing the compilation options. Here are some commonly used ones. 3.1 Compilation Optimization3.1.1 Use Oz instead of OsCompile options
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -Oz") 3.1.2 Reduce the size of unused code
Link Options
Example:
3.1.3 Enable link optimizationCompile options
Link Options
Example:
3.1.4 Turn off exceptions and rtti
3.1.5 Automatically remove symbols from imported static libraries
(If iOS wants to import symbols into the library, you need to manually enable the -reexport-l$(UR_LIB) option) if ("${CMAKE_BUILD_TYPE}" STREQUAL "Release" AND ANDROID) Currently, special effects on Android use this option. 3.1.6 Reducing the Symbol Table
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffunction-sections -fdata-sections -fvisibility=hidden -g") Currently, the special effects side uses -fvisibility=hidden 3.1.7 Dynamic Linking C++Dynamically link the libstdc++ library to avoid increasing the size of the library file. 3.2 Code OptimizationIn a nutshell: the less code there is, the smaller the package size will be. From experience, 100 lines of code will take up about 1~5K of space. If the ratio exceeds this, there must be something wrong with the code. 3.2.1 Do not have invalid judgment logic (if...else...)You can use a table-driven approach to implement if else and reduce unnecessary code references. 3.2.2 Reduce template expansion and macro expansionTemplate expansion takes up a lot of space, especially for the same form of code, the template will expand into multiple different classes. In this case, it is best to extract the common part and declare it as a static method. The following is the method of binding variables: template <typename T> Can be modified to: // bindArgs extracted 3.2.3 Avoid unnecessary use of stl/stdFor example, some callbacks can use function pointers: std::function<> as a class, which is bound to have a higher size cost than a function pointer such as void * fun; // using FunInstantiate = std::function<FunInterface*()>; // no longer used For example, when referencing a constant string, you can use the const char* type to avoid the compiler calling the implicit copy constructor; // void DemoClass::fun(const std::string &name, const DemoPtr &demoPtr) // no longer used 3.2.4 Do not define const or static variables in header filesThe const / static variables in the header file will be introduced into the corresponding cpp file, which is equivalent to introducing a long string of constant strings in each .o file. 3.2.5 Avoid large arraysLarge arrays take up space equal to the size of the array. 3.2.6 Reduce unnecessary virtual base classes/virtual functions// class Child : virtual public Parent // no longer used 4 Packet Volume Monitoring Tool4.1 Why do we need a package volume monitoring tool?Each version of Douyin will have a lot of new capabilities updated, and each update of each requirement will lead to changes in package size. In order to better monitor the changes in package size, confirm the reasons for package size growth, and improve ROI, a package size monitoring tool is introduced to more intuitively confirm the reasons for package size growth, intercept abnormal growth, output the package size growth size and reasons for package size growth brought by each requirement, give package size alarms in time, locate abnormal incremental cases, slow down package size growth, and promote business optimization. 4.2 How to monitor package volumeThe package size monitoring tool currently used by the special effects side comes from Google's open source binary file size analysis tool Bloaty, which is used to analyze binary files (xxx.exe, xxx.bin), shared target files (xxx.so), object files (xxx.o) and static libraries (xxx.a), and supports ELF\Mach-O\WebAssembly formats. It can sort out the volume composition of each part of the file, split the size of each section, and combine the symbol information to infer the package size of each method and source file. Taking the special effects side libeffect_sdk.so as an example, analyze the component units and source files of the .so file and intercept some output results: FILE SIZE Using the above tools, you can more clearly locate the package size increase caused by each file. 4.2.1 Package volume monitoring tool workflowThe package size monitoring tool is a must-have before the current special effects requirements are put on the car. All requirements will be checked for package size after MR (merge request) is proposed and CI packaging is completed. Only package size increments that meet the expected requirements are allowed to be merged. All package size increments correspond to requirements one by one and are recorded. 4.2.2 Analysis capabilities of package volume monitoring toolsThe package size analysis tool supports single file analysis and version iteration comparison analysis. For single file analysis, since the special effects side is mainly delivered through .so files, after each MR is packaged, the tool will automatically obtain the corresponding .so files and .so.symbol files, analyze the package volume composition and package volume source of the library file, and output the package volume size brought by all method functions, sections, and compilation units (xxx.cpp). After confirming the size, the incremental source module of the package volume is confirmed through keyword matching, and the final package volume profile of each module unit and compilation unit is given. On the other hand, since the special effects capabilities are always updated and iterated based on the requirements, every time a substantial requirement is submitted, the package size difference between the previous version and the current version will be compared, and the incremental source of each version requirement will be recorded. When the incremental value brought by the version comparison result exceeds the expected value, the communication API will be called to send out an alarm indicating that the package size exceeds the limit. 4.2.3 Package volume data record bookAll required package size increments will be recorded in the package size log book: when the service receives a demand event, it will call the bits/meego interface to request the demand information and the package size preset exp_pack_size increment to be written into the mr_pkg_size table; after the local package is completed, the actual package size increment real_pack_size will be recorded in the mr_pkg_size table, and the expected value will be compared with the actual increment. Finally, all package size increments and historical sources of demand increments are recorded, and through the table query interface, the source of package size growth can be identified on the web page based on conditions such as demand name/time period/branch name/commit id. 5 ConclusionThrough the above-mentioned trinity of code volume optimization accumulation, real-time volume monitoring, and the implementation of incremental demand to people, the volume of special effects side packages can be controlled to grow in an orderly manner and code efficiency can be improved. |
<<: Let you know the development history of speech recognition technology
>>: Spring Festival Event- Technical Solution for Peak Reward Distribution
There are so many seasonal fruits in autumn, but ...
This year, due to the increase in business volume...
WeChat mini-program applications have penetrated ...
As a veteran who has been in the APP promotion in...
Source code introduction: FDSlideBar is a top sli...
A friend said to me at a gathering in the circle....
As a brand, if you want to enter the hearts of us...
This article comprehensively explains the framewo...
Churn analysis is not about analyzing the problem...
On October 5, 964 dead migratory birds were found...
In the field of copyrighted videos, Netflix, the ...
It's winter vacation Students have a long-awa...
[Must-have course for moms] 20 lessons on childre...
Based on my observations and thoughts in recent t...