Version 4.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 3.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.
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
15 = Components
16
Thomas Mortagne 1.18 17 == [[Wiki components>>extensions:Extension.WikiComponent Module]] are slow
Thomas Mortagne 1.1 18
Thomas Mortagne 3.1 19 Wiki components are discouraged because they are much slower than the alternatives (beside various other problems).
Thomas Mortagne 1.1 20
Thomas Mortagne 1.2 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.
Thomas Mortagne 1.1 22
23 = Rendering
24
Thomas Mortagne 1.18 25 == When [[implementing a macro>>xwiki:Documentation.DevGuide.Tutorials.WritingMacros]]
Thomas Mortagne 1.1 26
27 When you implement your own macro, there are various performance related things that you should be aware of.
28
Thomas Mortagne 1.18 29 === [[Wiki macros>>xwiki:Documentation.DevGuide.Tutorials.WritingMacros.WikiMacroTutorial]] are much slower than [[Java macros>>rendering:Main.ExtendingMacro]]
Thomas Mortagne 1.1 30
Thomas Mortagne 3.1 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.
Thomas Mortagne 1.1 32
Thomas Mortagne 3.1 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.
Thomas Mortagne 1.1 34
Thomas Mortagne 1.7 35 === Macro preparation
Thomas Mortagne 1.1 36
37 {{info}}
Thomas Mortagne 3.1 38 Currently, not supported in Wiki macros.
Thomas Mortagne 1.1 39 {{/info}}
40
41 There are two steps in the execution of a macro:
42 * the preparation
43 * the actual execution
44
Thomas Mortagne 3.1 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.
Thomas Mortagne 1.1 46
Thomas Mortagne 3.1 47
48
Thomas Mortagne 1.3 49 === Isolated execution
50
51 {{version since="17.3.0"}}
Thomas Mortagne 3.1 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:
Thomas Mortagne 1.3 53
Thomas Mortagne 3.1 54 * For Java macros: see [[rendering:Main.ExtendingMacro]]
55 * For wiki macros: see [[xwiki:Documentation.DevGuide.Tutorials.WritingMacros.WikiMacroTutorial]]
Thomas Mortagne 1.3 56 {{/version}}
57
Thomas Mortagne 1.1 58 == HTML Macro
59
Thomas Mortagne 1.6 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.
Thomas Mortagne 1.7 61
62 == Preparation
63
64 === Preparing ##Block##s
65
Thomas Mortagne 1.10 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.
Thomas Mortagne 1.7 67
Thomas Mortagne 3.1 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:
Thomas Mortagne 1.7 69
70 {{code language="java"}}
71 @Inject
72 @Named("macro")
73 private Translation macroTransformation;
74
Thomas Mortagne 1.10 75 private XDOM cachedXDOM;
Thomas Mortagne 1.7 76
Thomas Mortagne 1.10 77 void cacheContent(String wikiContent)
Thomas Mortagne 1.7 78 {
Thomas Mortagne 1.10 79 this.cachedXDOM = parse(wikiContent);
80 this.macroTransformation.prepare(cachedBlock);
Thomas Mortagne 1.7 81 }
82
83 ...
84 {{/code}}
85
Thomas Mortagne 1.13 86 === Compiling Velocity
Thomas Mortagne 1.7 87
Thomas Mortagne 1.13 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.
Thomas Mortagne 1.7 89
Thomas Mortagne 1.10 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
Thomas Mortagne 1.12 101 void evaludate(Writer writer)
Thomas Mortagne 1.10 102 {
103 ...
104 this.velocityManager.getVelocityEngine().evaluate(velocityContext, writer, namespace, this.cachedVelocity);
105 }
Thomas Mortagne 1.11 106
Thomas Mortagne 1.10 107 ...
108 {{/code}}
109
Thomas Mortagne 1.16 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"]].
Thomas Mortagne 1.13 111
Thomas Mortagne 1.20 112 = [[UI extensions>>extensions:Extension.UIExtension Module]]
Thomas Mortagne 1.7 113
Thomas Mortagne 3.1 114 == Wiki-based UI extensions are slower than Java-based ones
Thomas Mortagne 1.7 115
Thomas Mortagne 3.1 116 What is true for generic wiki components and wiki macros is also true for UI extensions.

Get Connected