Version 5.1 by Thomas Mortagne on 2025/05/16

Show last authors
1 {{box cssClass="floatinginfobox" title="**Contents**"}}{{toc/}}{{/box}}
2
3 = Database
4
5 == Index
6
7 Filtering and ordering on a non indexed field (for example, searching for something in the content of a document) will cause very important performance problems as soon as that table starts to contain a lot of entries. Not only for the query you are trying to execute but also for other queries running in the database server.
8
9 You can also have a similar problem when combining several indexed field in the same ##WHERE## clause, as, in theory, for this to be fast, you would need to have an index combining those fields. Unfortunately, very often, it's not possible in XWiki to do that (at least with MySQL/MariaDB) because most String columns already have the maximum size accepted for an index, and combining them would go beyond that limit.
10
11 The possible alternatives are generally:
12 * Use the Solr ##search## core instead.
13 * Do post filtering/ordering on your side instead of the database (but this can cause problems when you need pagination)
14
15 = Components
16
17 == [[Wiki components>>extensions:Extension.WikiComponent Module]] are slow
18
19 Wiki components are discouraged because they are much slower than the alternatives (beside various other problems).
20
21 If you really need to implement your component in a wiki page, you should take a look at [[extensions:Extension.Script Component]] (which also makes implementing, and especially registering, components in a wiki page a lot easier). What will be produced in the end is technically exactly the same thing as what you get with a component implemented in Java.
22
23 = Rendering
24
25 == When [[implementing a macro>>xwiki:Documentation.DevGuide.Tutorials.WritingMacros]]
26
27 When you implement your own macro, there are various performance related things that you should be aware of.
28
29 === [[Wiki macros>>xwiki:Documentation.DevGuide.Tutorials.WritingMacros.WikiMacroTutorial]] are much slower than [[Java macros>>rendering:Main.ExtendingMacro]]
30
31 So you should try as much as possible to implement Java macros (it's also much easier to write automated tests for Java macros). It's not always very practical to write complex UI in Java, but you can easily execute a [[template>>extensions:Extension.Template Module]] from your Java code.
32
33 Like with any other component, you can implement a Java macro through [[extensions:Extension.Script Component]] if you absolutely need your macro to be implemented in a wiki page. What will be produced in the end is technically exactly the same thing as what you get with a macro implemented in Java.
34
35 === Macro preparation
36
37 {{info}}
38 Currently, not supported in Wiki macros.
39 {{/info}}
40
41 There are two steps in the execution of a macro:
42 * the preparation
43 * the actual execution
44
45 During the preparation step, you get a chance to do everything that does not rely on any contextual information and store the result in the ##MacroBlock## (as attribute). It can improve execution time greatly because the ##MacroBlock## is generally part of cached data, so the preparation will be executed only once for potentially many executions.
46
47
48
49 === Isolated execution
50
51 {{version since="17.3.0"}}
52 If your macro does not modify the XDOM around it (which is the case most of the time), it's highly recommended to indicate it:
53
54 * For Java macros: see [[rendering:Main.ExtendingMacro]]
55 * For wiki macros: see [[xwiki:Documentation.DevGuide.Tutorials.WritingMacros.WikiMacroTutorial]]
56 {{/version}}
57
58 == HTML Macro
59
60 The cleaning part of the HTML macro can be expensive, so using {{code}}clean="false"{{/code}} could have a significant impact on the performance if a lot of them are used.
61
62 == Preparation
63
64 === Preparing ##Block##s
65
66 Similarly to [[what is explained for macros>>||anchor="HMacropreparation"]], it's a good idea to parse and "prepare" any wiki content you are planning to execute several times. It will generally considerably reduce the execution time.
67
68 You can do that by calling ##Transformation#prepare(Block block)## in Java. If you don't know which ##Transformation## to apply, apply at least the ##macro## one:
69
70 {{code language="java"}}
71 @Inject
72 @Named("macro")
73 private Translation macroTransformation;
74
75 private XDOM cachedXDOM;
76
77 void cacheContent(String wikiContent)
78 {
79 this.cachedXDOM = parse(wikiContent);
80 this.macroTransformation.prepare(cachedBlock);
81 }
82
83 ...
84 {{/code}}
85
86 === Compiling Velocity
87
88 If you have the possibility to cache a Velocity script, it's generally a good idea to pre-compile it. In the case of Velocity it means parsing the String into an AST, which can be an expensive step.
89
90 {{code language="java"}}
91 @Inject
92 private VelocityManager velocityManager;
93
94 private VelocityTemplate cachedVelocity;
95
96 void cacheContent(String velocityScript)
97 {
98 this.cachedVelocity = this.velocityManager.compile(nameAssociatedtoTheScript, velocityScript)
99 }
100
101 void evaludate(Writer writer)
102 {
103 ...
104 this.velocityManager.getVelocityEngine().evaluate(velocityContext, writer, namespace, this.cachedVelocity);
105 }
106
107 ...
108 {{/code}}
109
110 This is, for example, what the [[##~{{velocity}}## macro>>extensions:Extension.Velocity Macro]] is doing as part of it's [[macro preparation step>>||anchor="#HMacropreparation"]].
111
112 = [[UI extensions>>extensions:Extension.UIExtension Module]]
113
114 == Wiki-based UI extensions are slower than Java-based ones
115
116 What is true for generic wiki components and wiki macros is also true for UI extensions.

Get Connected