Version 6.1 by Thomas Mortagne on 2025/05/16

Hide last authors
Thomas Mortagne 1.1 1 {{box cssClass="floatinginfobox" title="**Contents**"}}{{toc/}}{{/box}}
2
3 = Database
4
5 == Index
6
Thomas Mortagne 5.1 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.
Thomas Mortagne 1.1 8
Thomas Mortagne 3.1 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.
Thomas Mortagne 1.1 10
Thomas Mortagne 3.1 11 The possible alternatives are generally:
12 * Use the Solr ##search## core instead.
Thomas Mortagne 4.1 13 * Do post filtering/ordering on your side instead of the database (but this can cause problems when you need pagination)
Thomas Mortagne 1.1 14
Thomas Mortagne 6.1 15 = Solr
16
17 == Commit is slow
18
19 Each commit to Solr can be expensive and reducing the number of commits you are doing when you send a lot of data to Solr can have a very big speed impact.
20
Thomas Mortagne 1.1 21 = Components
22
Thomas Mortagne 1.18 23 == [[Wiki components>>extensions:Extension.WikiComponent Module]] are slow
Thomas Mortagne 1.1 24
Thomas Mortagne 3.1 25 Wiki components are discouraged because they are much slower than the alternatives (beside various other problems).
Thomas Mortagne 1.1 26
Thomas Mortagne 1.2 27 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.
Thomas Mortagne 1.1 28
29 = Rendering
30
Thomas Mortagne 1.18 31 == When [[implementing a macro>>xwiki:Documentation.DevGuide.Tutorials.WritingMacros]]
Thomas Mortagne 1.1 32
33 When you implement your own macro, there are various performance related things that you should be aware of.
34
Thomas Mortagne 1.18 35 === [[Wiki macros>>xwiki:Documentation.DevGuide.Tutorials.WritingMacros.WikiMacroTutorial]] are much slower than [[Java macros>>rendering:Main.ExtendingMacro]]
Thomas Mortagne 1.1 36
Thomas Mortagne 3.1 37 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.
Thomas Mortagne 1.1 38
Thomas Mortagne 3.1 39 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.
Thomas Mortagne 1.1 40
Thomas Mortagne 1.7 41 === Macro preparation
Thomas Mortagne 1.1 42
43 {{info}}
Thomas Mortagne 3.1 44 Currently, not supported in Wiki macros.
Thomas Mortagne 1.1 45 {{/info}}
46
47 There are two steps in the execution of a macro:
48 * the preparation
49 * the actual execution
50
Thomas Mortagne 3.1 51 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.
Thomas Mortagne 1.1 52
Thomas Mortagne 3.1 53
54
Thomas Mortagne 1.3 55 === Isolated execution
56
57 {{version since="17.3.0"}}
Thomas Mortagne 3.1 58 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:
Thomas Mortagne 1.3 59
Thomas Mortagne 3.1 60 * For Java macros: see [[rendering:Main.ExtendingMacro]]
61 * For wiki macros: see [[xwiki:Documentation.DevGuide.Tutorials.WritingMacros.WikiMacroTutorial]]
Thomas Mortagne 1.3 62 {{/version}}
63
Thomas Mortagne 1.1 64 == HTML Macro
65
Thomas Mortagne 1.6 66 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.
Thomas Mortagne 1.7 67
68 == Preparation
69
70 === Preparing ##Block##s
71
Thomas Mortagne 1.10 72 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.
Thomas Mortagne 1.7 73
Thomas Mortagne 3.1 74 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:
Thomas Mortagne 1.7 75
76 {{code language="java"}}
77 @Inject
78 @Named("macro")
79 private Translation macroTransformation;
80
Thomas Mortagne 1.10 81 private XDOM cachedXDOM;
Thomas Mortagne 1.7 82
Thomas Mortagne 1.10 83 void cacheContent(String wikiContent)
Thomas Mortagne 1.7 84 {
Thomas Mortagne 1.10 85 this.cachedXDOM = parse(wikiContent);
86 this.macroTransformation.prepare(cachedBlock);
Thomas Mortagne 1.7 87 }
88
89 ...
90 {{/code}}
91
Thomas Mortagne 1.13 92 === Compiling Velocity
Thomas Mortagne 1.7 93
Thomas Mortagne 1.13 94 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.
Thomas Mortagne 1.7 95
Thomas Mortagne 1.10 96 {{code language="java"}}
97 @Inject
98 private VelocityManager velocityManager;
99
100 private VelocityTemplate cachedVelocity;
101
102 void cacheContent(String velocityScript)
103 {
104 this.cachedVelocity = this.velocityManager.compile(nameAssociatedtoTheScript, velocityScript)
105 }
106
Thomas Mortagne 1.12 107 void evaludate(Writer writer)
Thomas Mortagne 1.10 108 {
109 ...
110 this.velocityManager.getVelocityEngine().evaluate(velocityContext, writer, namespace, this.cachedVelocity);
111 }
Thomas Mortagne 1.11 112
Thomas Mortagne 1.10 113 ...
114 {{/code}}
115
Thomas Mortagne 1.16 116 This is, for example, what the [[##~{{velocity}}## macro>>extensions:Extension.Velocity Macro]] is doing as part of it's [[macro preparation step>>||anchor="#HMacropreparation"]].
Thomas Mortagne 1.13 117
Thomas Mortagne 1.20 118 = [[UI extensions>>extensions:Extension.UIExtension Module]]
Thomas Mortagne 1.7 119
Thomas Mortagne 3.1 120 == Wiki-based UI extensions are slower than Java-based ones
Thomas Mortagne 1.7 121
Thomas Mortagne 3.1 122 What is true for generic wiki components and wiki macros is also true for UI extensions.

Get Connected