Skip to content

Custom Cloning

Gustavo Malvestiti edited this page Aug 16, 2026 · 1 revision

Cloning defaults to a JSON round-trip through the config model, which is correct for any model but adds overhead proportional to the number of fields. Replace it with a hand-written copy when profiling shows copy() is on a hot path:

public final class MyModConfigCloner implements StateCloner<MyModConfig> {

    @Override
    public MyModConfig copy(MyModConfig source) {
        MyModConfig copy = new MyModConfig();
        copy.hudScale = source.hudScale;
        copy.showHints = source.showHints;
        copy.hiddenHints = new ArrayList<>(source.hiddenHints);   // deep-copy the list
        return copy;
    }
}
EasyConfig.holder(MyModConfig.class)
    .modId("mymod")
    .stateCloner(new MyModConfigCloner())
    .create();

A custom StateCloner must return a fully independent object. Any field left as a shared reference will silently alias between the published state and every copy derived from it, which produces hard-to-diagnose corruption bugs when either side mutates the shared value.

Clone this wiki locally