mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-01 14:07:43 +01:00
Avoid list copy when unnecessary
This commit is contained in:
parent
2d6368f6bd
commit
9ee74845e1
@ -129,10 +129,15 @@ public class Tag<T> {
|
||||
}, null, path,
|
||||
copy != null ? ts -> {
|
||||
T[] array = (T[]) new Object[ts.size()];
|
||||
boolean shallowCopy = true;
|
||||
for (int i = 0; i < ts.size(); i++) {
|
||||
array[i] = copy.apply(ts.get(i));
|
||||
T t = ts.get(i);
|
||||
array[i] = copy.apply(t);
|
||||
if (shallowCopy && array[i] != t) {
|
||||
shallowCopy = false;
|
||||
}
|
||||
}
|
||||
return List.of(array);
|
||||
return shallowCopy ? List.copyOf(ts) : List.of(array);
|
||||
} : List::copyOf);
|
||||
}
|
||||
|
||||
|
@ -26,6 +26,41 @@ public class TagListTest {
|
||||
assertNull(handler.getTag(tag));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cache() {
|
||||
var handler = TagHandler.newHandler();
|
||||
var tag = Tag.Integer("number").list();
|
||||
var val = List.of(1, 2, 3);
|
||||
|
||||
handler.setTag(tag, val);
|
||||
assertSame(val, handler.getTag(tag));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void recursiveCache() {
|
||||
var handler = TagHandler.newHandler();
|
||||
var tag = Tag.Integer("number").list().list();
|
||||
var val = List.of(List.of(1, 2, 3), List.of(4, 5, 6));
|
||||
|
||||
handler.setTag(tag, val);
|
||||
assertSame(val.get(0), handler.getTag(tag).get(0));
|
||||
assertSame(val.get(1), handler.getTag(tag).get(1));
|
||||
assertSame(val, handler.getTag(tag));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void recursiveCacheIncorrect() {
|
||||
var handler = TagHandler.newHandler();
|
||||
var tag = Tag.Integer("number").list().list();
|
||||
var val = List.of(List.of(1, 2, 3), new ArrayList<>(Arrays.asList(4, 5, 6)));
|
||||
|
||||
handler.setTag(tag, val);
|
||||
assertSame(val.get(0), handler.getTag(tag).get(0));
|
||||
assertNotSame(val.get(1), handler.getTag(tag).get(1));
|
||||
assertNotSame(val, handler.getTag(tag));
|
||||
assertEquals(val, handler.getTag(tag));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void snbt() {
|
||||
var handler = TagHandler.newHandler();
|
||||
|
Loading…
Reference in New Issue
Block a user