Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qmldiskcache.qdoc
Go to the documentation of this file.
1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5\page qmldiskcache.html
6\title The QML Disk Cache
7\brief QML documents are pre-compiled ahead of time or cached after compilation
8 at runtime.
9
10To achieve optimal performance, QML documents are either pre-compiled ahead of
11time during the build process or cached after compilation at runtime. This page
12describes both strategies and how to configure the caching behavior.
13
14\section1 Ahead-of-Time Compilation
15
16You should define your QML modules using \l{qt_add_qml_module} that makes sure
17the \l{Qt Quick Compiler} processes your QML and JavaScript files ahead of time.
18This guarantees optimum performance at run time.
19
20The \l{Qt Quick Compiler} generates byte code for each function and binding.
21This byte code can be used by the QML interpreter and the Just-in-Time (JIT)
22compiler in the QML engine. In addition, the \l{Qt Quick Compiler} generates
23native code for suitable functions and bindings. The native code is executed
24directly, which results in better performance than interpreting or just-in-time
25compiling byte code. Both byte code and native code are then compiled into your
26application binary.
27
28One benefit of ahead-of-time compilation is that syntax errors in your QML
29documents are detected at application compile-time instead of at run-time when
30the file is loaded.
31
32\section2 With CMake
33
34When using CMake with \l{qt_add_qml_module}, QML files are automatically
35compiled ahead of time. You should load your QML documents from the resource
36file system where possible. This ensures the QML engine can find the code
37compiled ahead of time.
38
39\section2 With qmake
40
41When using \l{qmake}, you can enable ahead-of-time compilation by specifying
42\c{CONFIG += qtquickcompiler} in your project file. \l{\QC Documentation}{\QC}
43has a setting that allows passing this directive to the qmake command line.
44By default, it is enabled for release and profile builds.
45
46When using qmake, you must organize your project in a specific way:
47
48\list
49 \li All QML documents (including JavaScript files) must be included as
50 resources via \l{The Qt Resource System}{Qt's Resource system}.
51 \li Your application must load the QML documents via the \c qrc:/// URL
52 scheme.
53\endlist
54
55Note that qmake cannot pass as much information to the \l{Qt Quick Compiler}
56as CMake. Therefore, the compilation will contain less native code.
57
58\section1 Runtime Disk Caching
59
60If no pre-compiled code is found at run time, or if the code cannot be used, the
61QML engine compiles QML documents into a byte code representation on the fly.
62Instead of re-compiling the same document each time it is loaded, the QML engine
63caches the compiled byte code. The caching process is automatic: each time you
64load a changed QML document, the cache is automatically re-created.
65
66\section2 Cache File Format and Location
67
68Cache files use the following extensions:
69
70\list
71 \li \c .qmlc for compiled QML documents
72 \li \c .jsc for imported JavaScript files
73 \li \c .mjsc for ECMAScript modules
74\endlist
75
76Cache files are located in a subdirectory named \c{qmlcache} of the system's
77cache directory, as denoted by QStandardPaths::CacheLocation.
78
79\section2 Memory Efficiency
80
81Cache files are loaded via the \c mmap() system call on POSIX-compliant
82operating systems or \c CreateFileMapping() on Windows. This memory-mapping
83approach results in significant memory savings. In addition, when multiple
84applications use the same QML document, the memory needed for the code is shared
85between application processes, further reducing memory overhead.
86
87\section1 Cache Validation
88
89Cache files and ahead-of-time compiled code are only loaded if all of the
90following conditions are met:
91
92\list
93 \li The Qt version has not changed
94 \li The source code in the original file has not changed
95 \li The QML debugger is not running
96 \li The \l{Validation of ahead of time generated native code}{validation of AOT code}
97 succeeds
98\endlist
99
100Note that \c{QML_FORCE_DISK_CACHE} (see below) can override the QML debugger
101condition. The other environment variables do not influence these validation
102conditions.
103
104\section2 Validation of ahead of time generated native code
105
106The native code generated by the \l{Qt Quick Compiler} has some assumptions
107built in. If the conditions in which the code is executed differ from those in
108which it was compiled, the code may be unsafe to use. Therefore, the native code
109is validated at runtime using metadata stored alongside it. If this validation
110passes, the code is executed as normal. If it fails, the execution silently
111falls back to interpreting the bytecode instead. This validation happens once
112per file, the first time the code is loaded, and either approves or rejects all
113functions and bindings in that QML file as a whole.
114
115You can customize the validation of AOT code:
116
117\list
118 \li To disable the validation at runtime, set the
119 \c{QV4_SKIP_AOT_VALIDATION} environment variable. This allows you to
120 avoid paying the small overhead of performing the validation.
121
122 \code
123 QV4_SKIP_AOT_VALIDATION=1 ./myQmlApp
124 \endcode
125 \li To ensure that validation succeeds at runtime, set the
126 \c{QV4_FAIL_ON_INVALID_AOT} environment variable. If validation fails,
127 the program is terminated. This allows you to make sure compiled
128 functions are indeed executed as native code, for example.
129
130 \code
131 QV4_FAIL_ON_INVALID_AOT=1 ./myQmlApp
132 \endcode
133 \li To disable the feature completely and prevent the \l{Qt Quick Compiler}
134 from generating the metadata and validation logic, pass
135 \c{NO_GENERATE_AOT_VALIDATION} to \l{qt_add_qml_module}.
136
137 \code
138 qt_add_qml_module(... NO_GENERATE_AOT_VALIDATION)
139 \endcode
140\endlist
141
142
143\section1 Configuration
144
145You can fine-tune caching behavior using the environment variable
146\c{QML_DISK_CACHE}, which takes a comma-separated list of options. For example:
147
148\badcode
149QML_DISK_CACHE=aot,qmlc-read
150\endcode
151
152The available options are as follows:
153
154\table
155 \header
156 \li Option
157 \li Description
158 \row
159 \li aot-native
160 \li Load the compilation units compiled ahead of time and allow
161 execution of any native code found in them.
162 \row
163 \li aot-bytecode
164 \li Load the compilation units compiled ahead of time and allow
165 interpretation and just-in-time compilation of byte code found
166 in them.
167 \row
168 \li aot
169 \li Shorthand for \c{aot-native,aot-bytecode}.
170 \row
171 \li qmlc-read
172 \li Load any cached compilation units for QML and JavaScript files from
173 the host file system and allow interpretation and just-in-time
174 compilation of byte code found in them.
175 \row
176 \li qmlc-write
177 \li When compiling a QML or JavaScript file on the fly, create a cache
178 file afterward. The cache file can be loaded when the same
179 document is requested again.
180 \row
181 \li qmlc
182 \li Shorthand for \c{qmlc-read,qmlc-write}.
183\endtable
184
185Furthermore, you can use the following environment variables:
186
187\table
188 \header
189 \li Environment Variable
190 \li Description
191 \row
192 \li \c{QML_DISABLE_DISK_CACHE}
193 \li Disables the disk cache and forces re-compilation from source for
194 all QML and JavaScript files. \c{QML_DISABLE_DISK_CACHE} overrides
195 \c{QML_DISK_CACHE}.
196 \row
197 \li \c{QML_FORCE_DISK_CACHE}
198 \li Enables the disk cache even when debugging QML. You cannot use the
199 JavaScript debugger this way. It may fail to stop at breakpoints,
200 for example. You can still use the QML inspector to explore the
201 object hierarchy, though. \c{QML_FORCE_DISK_CACHE} overrides
202 \c{QML_DISABLE_DISK_CACHE} and \c{QML_DISK_CACHE}.
203 \row
204 \li \c{QML_DISK_CACHE_PATH}
205 \li Specifies a custom location where the cache files shall be stored
206 instead of using the default location.
207\endtable
208
209*/