mojira.dev
MC-234323

Performance issue with text parsing

The bug

Since Minecraft 1.12 (or a bit before), some of the font rendering was remade, and a minor performance issue was introduced.

In the ChatFormatting class:

public static ChatFormatting getByCode(char c) {
    char lower = Character.toString(c).toLowerCase(Locale.ROOT).charAt(0);

Here, there is a useless conversion from a character to a string, a lowercase string, then a character again. A simple Character.toLowerCase(c) would have been enough.

This issue was even worse before 1.12, where the whole rendered text was being changed to lowercase on each color character, and was causing a ~1% performance drop when rendering a lot of text. Here the impact is minor, but would still be a good improvement to clean it up.

Comments 16

You also have a very important security leak on this Jira. Anyone can set values that they shouldn't be able to, like Mojang Priority or Category, by just enabling the HTML elements in the edit form and sending it.

Still an issue in 21w37a.

Still an issue in 21w38a.

Still an issue in 21w39a.

Still an issue in 21w40a.

6 more comments

Is it okay to make this issue public or does it contain any sensitive information?

Hey, I originally made this private because it was showing Minecraft original codebase. As long as it's ok for you, you can make it public

Can you show a profile the data that supports this assessment? Thanks.

This issue is still present in the game as of 23w05a.

 

It's a very small optimisation, just to get a few nanoseconds, but that's still an improvement when a lot of colored text is being rendered. It's not much, but for the time it takes to fix it (less than 5 minutes), that's definitely worth it. Also would make the code more readable and clear for the future.

> Can you show a profile the data that supports this assessment? Thanks.
 
I have made a small benchmark with 100 million iterations and 5 cycles of iterations, the result I got was:

Current implementation: 1799595435ns (1799ms)
Current implementation: 1176125877ns (1176ms)
Current implementation: 1143411915ns (1143ms)
Current implementation: 1124996127ns (1124ms)
Current implementation: 1160581816ns (1160ms)
Simplified implementation: 421642052ns (421ms)
Simplified implementation: 137799694ns (137ms)
Simplified implementation: 178195836ns (178ms)
Simplified implementation: 167903541ns (167ms)
Simplified implementation: 182431767ns (182ms)

code:

java
public class Test {
    private static final int ITERATIONS = 100000000;
    private static final int ITERATIONS_CYCLES = 5;

    public static void main(String[] args) {
        for (int j = 0; j < ITERATIONS_CYCLES; j++) {
            long start = System.nanoTime();
            for (int i = 0; i < ITERATIONS; i++) {
                char c = (char) (i % 128);
                Character.toString(c).toLowerCase(Locale.ROOT).charAt(0);
            }
            long end = System.nanoTime();
            long currentTime = end - start;
            System.out.println("Current implementation: " + currentTime + "ns (" + TimeUnit.NANOSECONDS.toMillis(currentTime) + "ms)");
        }

        for (int j = 0; j < ITERATIONS_CYCLES; j++) {
            long start = System.nanoTime();
            for (int i = 0; i < ITERATIONS; i++) {
                char c = (char) (i % 128);
                Character.toLowerCase(c);
            }
            long end = System.nanoTime();
            long simplifiedTime = end - start;
            System.out.println("Simplified implementation: " + simplifiedTime + "ns (" + TimeUnit.NANOSECONDS.toMillis(simplifiedTime) + "ms)");
        }
    }
}

Rigner

etanaratsastaja

Plausible

Platform

Low

Performance

Optimization, Rendering, Text

1.17.1, 21w37a, 21w38a, 21w39a, 21w40a, ..., 1.20 Release Candidate 1, 1.20, 1.20.1 Release Candidate 1, 1.20.1, 1.20.2

23w41a

Retrieved