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 generally pre-compiled or cached after compilation.
8
9You should define your QML modules using \l{qt_add_qml_module} that makes sure
10that the \l{Qt Quick Compiler} processes your QML and JavaScript files ahead of
11time. Also, it guarantees optimum performance at run time. The
12\l{Qt Quick Compiler} generates byte code for
13each function and binding. This byte code can be used by the QML interpreter,
14and the Just-in-time (JIT) compiler in the QML engine. In addition, the
15\l{Qt Quick Compiler} generates native code for suitable functions and
16bindings. The native code can be executed directly, which results in better
17performance than interpreting or just-in-time compiling the byte code. Both,
18byte code and native code are then compiled into your binary.
19
20When using \l{qmake} you can specify \c{CONFIG += qtquickcompiler} to
21give similar treatment to QML and JavaScript files added as resources to your
22project. \l{\QC Documentation}{\QC} has a setting that allows passing
23\c{CONFIG += qtquickcompiler} to the qmake command line. By default, it is
24enabled for release and profile builds. \l{qmake} cannot pass as much
25information to the \l{Qt Quick Compiler} as CMake. Therefore, the compilation
26will contain less native code.
27
28You should make sure to load your QML documents from the resource file system
29where possible. Otherwise the QML engine won't be able to find the code compiled
30ahead of time.
31
32If no byte code or native code can be found for a QML document at run time, or
33if the code is found but cannot be used, the QML engine compiles the document
34into a byte code representation on the fly. The compiling process can be time
35consuming, and the result will contain only byte code. Subsequent loads of the
36same document will yield the same byte code. The QML engine can optimize this
37step by caching the result of the compilation. It stores the byte code in a
38cache file and later loads the cache file instead of re-compiling when the same
39QML document is requested again. Usually, the cache files are stored in a
40subdirectory \c{qmlcache} of the system's cache directory, as denoted by
41QStandardPaths::CacheLocation.
42
43Checks are in place to make sure that any cache files and any code compiled
44ahead of time are only loaded if all of the following conditions are met:
45\list
46 \li The Qt version has not changed
47 \li The source code in the original file has not changed
48 \li The QML debugger is not running
49 \li The \l{Validation of ahead of time generated native code}{validation of AOT code} succeeds
50\endlist
51
52Only the \c{QML_FORCE_DISK_CACHE} variable (see below) overrides only the
53condition regarding the QML debugger. The other environment variables do not
54influence these conditions.
55
56The primary way of fine tuning the behavior regarding ahead of time compiled
57code and caching is via the environment variable \c{QML_DISK_CACHE}. This
58variable takes a comma-separated list of options, for example:
59
60\badcode
61QML_DISK_CACHE=aot,qmlc-read
62\endcode
63
64The available options are as follows:
65
66\table
67 \header
68 \li Option
69 \li Description
70 \row
71 \li aot-native
72 \li Load the compilation units compiled ahead of time and allow
73 execution of any native code found in them.
74 \row
75 \li aot-bytecode
76 \li Load the compilation units compiled ahead of time and allow
77 interpretation and just-in-time compilation of byte code found
78 in them.
79 \row
80 \li aot
81 \li Shorthand for \c{aot-native,aot-bytecode}.
82 \row
83 \li qmlc-read
84 \li Load any cached compilation units for QML and JavaScript files from
85 the host file system and allow interpretation and just-in-time
86 compilation of byte code found in them.
87 \row
88 \li qmlc-write
89 \li When compiling a QML or JavaScript file on the fly, create a cache
90 file afterward. The cache file can be loaded when the same
91 document is requested again.
92 \row
93 \li qmlc
94 \li Shorthand for \c{qmlc-read,qmlc-write}.
95\endtable
96
97Furthermore, you can use the following environment variables:
98
99\table
100 \header
101 \li Environment Variable
102 \li Description
103 \row
104 \li \c{QML_DISABLE_DISK_CACHE}
105 \li Disables the disk cache and forces re-compilation from source for
106 all QML and JavaScript files. \c{QML_DISABLE_DISK_CACHE} overrides
107 \c{QML_DISK_CACHE}.
108 \row
109 \li \c{QML_FORCE_DISK_CACHE}
110 \li Enables the disk cache even when debugging QML. You cannot use the
111 JavaScript debugger this way. It may fail to stop at breakpoints,
112 for example. You can still use the QML inspector to explore the
113 object hierarchy, though. \c{QML_FORCE_DISK_CACHE} overrides
114 \c{QML_DISABLE_DISK_CACHE} and \c{QML_DISK_CACHE}.
115 \row
116 \li \c{QML_DISK_CACHE_PATH}
117 \li Specifies a custom location where the cache files shall be stored
118 instead of using the default location.
119\endtable
120
121\section1 Validation of ahead of time generated native code
122
123The native code generated by the \l{Qt Quick Compiler} has some assumptions
124built in. If the conditions in which the code is executed differ from those in
125which it was compiled, the code may be unsafe to use. Therefore, the native code
126is validated at runtime using metadata stored alongside it. If this validation
127passes, the code is executed as normal. If it fails, the execution silently
128falls back to interpreting the bytecode instead. This validation happens once
129per file, the first time the code is loaded, and either approves or rejects all
130functions and bindings in that QML file as a whole.
131
132The validation of AOT code can be customized. By default, the validation is
133enabled but it can be skipped at runtime by setting the
134\c{QV4_SKIP_AOT_VALIDATION} environment variable to avoid paying the small
135overhead of performing the validation. By contrast, if you wish to ensure that
136validation succeeds at runtime (in order to make sure compiled functions are
137indeed executed as native code, for example), you can set the
138\c{QV4_FAIL_ON_INVALID_AOT} environment variable that will terminate the program
139in case of a failed validation. Finally, passing \c{NO_GENERATE_AOT_VALIDATION}
140to \l{qt_add_qml_module} disables the feature completely and prevents the
141\l{Qt Quick Compiler} from generating the metadata and validation logic.
142
143*/