Performances development best practices
Database
Index
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.
You can also have a similar problems when combining several indexed fields 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 make such an index go beyond that limit.
The possible alternatives are generally:
- Use the Solr search core instead.
- Do post filtering/ordering on your side (Java, script) instead of the database (but this can cause prove challenging when you need pagination)
Solr
Commit is slow
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 generally have a very big speed impact.
Use sensible limits in queries
Even when you want to have all results for a query (without pagination), never put a limit larger than 10k (and ideally, not larger than 1k). Instead, use cursor-based pagination to paginate through the results to obtain all matches. The reason for this is that Solr allocates arrays that are large enough to store all results that could match. While Solr automatically limits this to the number of indexed documents, when the Solr core has 30 million documents, these are around 700MB – just to answer a single query and even when this query doesn't match any documents. This makes queries slow and leads to out of memory situations.
Components
Wiki components are slow
Wiki components are discouraged because they are much slower than the alternatives (beside various other problems).
If you really need to implement your component in a wiki page, you should take a look at 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.
Rendering
When implementing a macro
When you implement your own macro, there are various performance related things that you should be aware of.
Wiki macros are much slower than Java macros
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 from your Java code.
Like with any other component, you can implement a Java macro through a 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 (so it always have the same features).
Macro preparation
There are two steps in the execution of a macro:
- the preparation
- the actual execution
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). For example message macro, like the info macro, parse and prepare the passed content. 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.
Isolated execution
XWiki 17.3.0+
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:
- For Java macros: see ExtendingMacro
- For wiki macros: see WikiMacroTutorial
This will help speed up the macro transformation ordering of macros by reducing a lot the navigation it needs to do in the blocks.
HTML Macro
The cleaning part of the HTML macro can be expensive, so using clean="false"
could have a significant impact on the performance if a lot of them are used.
Preparation
Preparing Blocks
Similarly to what is explained for macros, 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.
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:
@Inject
@Named("macro")
private Translation macroTransformation;
private XDOM cachedXDOM;
void cacheContent(String wikiContent)
{
this.cachedXDOM = parse(wikiContent);
this.macroTransformation.prepare(cachedBlock);
}
...
Compiling Velocity
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.
@Inject
private VelocityManager velocityManager;
private VelocityTemplate cachedVelocity;
void cacheContent(String velocityScript)
{
this.cachedVelocity = this.velocityManager.compile(nameAssociatedtoTheScript, velocityScript)
}
void evaluate(Writer writer)
{
...
this.velocityManager.getVelocityEngine().evaluate(velocityContext, writer, namespace, this.cachedVelocity);
}
...
This is, for example, what the {{velocity}} macro is doing as part of its macro preparation step.
UI extensions
Wiki-based UI extensions are slower than Java-based ones
What is true for generic wiki components and wiki macros is also true for UI extensions. Since UI extension are components, you can perfectly implement them in Java:
@Component
@Named(MyUserProfileUIExtension.ID)
@Singleton
public class MyUserProfileUIExtension implements UIExtension, Initializable
{
/**
* The id of the UI extension.
*/
public static final String ID = "my.user.profile.uiextension";
@Inject
private TemplateManager templates;
private Map<String, String> parameters;
@Override
public void initialize() throws InitializationException
{
this.parameters = new HashMap<>();
this.parameters.put("id", "myuserprofileuiextension");
this.parameters.put("icon", "key");
}
@Override
public String getId()
{
return ID;
}
@Override
public String getExtensionPointId()
{
return "org.xwiki.plaftorm.user.profile.menu";
}
@Override
public Map<String, String> getParameters()
{
return this.parameters;
}
@Override
public Block execute()
{
// The actual UI is written in a template file located in the JAR
return this.templates.executeNoException("my/userprofile/uiextension.vm");
}
}
Of course, this kind of UI extension is also less fragile (since it's easy to break a wiki page by mistake).