turms-im / turms

🕊️ The world's most advanced open source instant messaging engine for 100K~10M concurrent users https://turms-im.github.io/docs
Apache License 2.0
1.73k stars 266 forks source link

Use primitive objects once JDK supports #604

Open JamesChenX opened 3 years ago

JamesChenX commented 3 years ago

Motivation

The primitive object supported by the Valhalla project will be introduced in JDK17 as a preview feature, and will significantly improve the memory layout of value-based classes, reduce 50% heap memory at least (we will try to reduce 80% heap memory after optimization), and make fewer Objects (the new name IdentityObject) with faster GC.

We will add "--enable-preview" to support primitive objects by default if we have enough unit/integration tests to ensure our projects can work as expected.

btw: The boxing and unboxing in Java should be considered as a design flaw, which will be refactored to the implementation primitive reference and value conversion in JEP 401.

Notes:

Checklist:

JamesChenX commented 3 years ago

If we are lucky, we can take 1\~2 days to adapt our codes (we are very familiar with the usage of primitive class) to primitive classes, specifically Primitive Objects + Unify the Basic Primitives with Objects (btw, I have waited for the primitive object for years, if there is no this project, I may not write Turms in Java). And takes 2~3 days to test it to ensure it won't break the code of our dependencies. And we will just test the system by mainly sending 1 million client requests to check if any bug caused by primitive class occurs.

If it really breaks our deps, we will just try to not use the deps with primitive objects. But the good news is: A lot of important components (like requests/responses of RPC, entities serialization for MongoDB, etc) are written by us, meaning we can control and adapt them to primitive objects.

Note that we don't need to expect Universal Generics(https://openjdk.java.net/jeps/8261529) because I guess it will be published as a preview in JDK 19\~21. When it really comes, we will just adapt our code for it again, which is just a piece of cake.


We will adapt our code for Valhalla once it publishes EAP builds so that we can develop and test based on the EAP version for months before it is merged into the baseline of JDK (just like what we had done to JDK 17 EAP). We will only commit our changes when the JDK supports primitive objects publishes.


After playing with Valhalla EAP, I find that they even haven't delivered anything about JEP 402 currently (9/25) though they have taken 8 years for Valhalla project since 2014, so we hold on. btw, we test whether JEP 402 is available by:

Set<Integer> set = new HashSet<>();
set.add(new Integer(999999999));
set.add(new Integer(999999999));
System.out.println(set.size()); // should be 1 if JEP 402 is available