It was mainly done to reduce GC. Trustin did some testing here and found this more efficient.
Why Netty uses reflection to replace members in sun.nio.ch.SelectorImpl class with array based set?
-
18-07-2023 - |
Question
While browsing through the Netty code base I came across below code block in NioEventLoop.java.
SelectedSelectionKeySet selectedKeySet = new SelectedSelectionKeySet();
Class<?> selectorImplClass =
Class.forName("sun.nio.ch.SelectorImpl", false, PlatformDependent.getSystemClassLoader());
// Ensure the current selector implementation is what we can instrument.
if (!selectorImplClass.isAssignableFrom(selector.getClass())) {
return selector;
}
Field selectedKeysField = selectorImplClass.getDeclaredField("selectedKeys");
Field publicSelectedKeysField = selectorImplClass.getDeclaredField("publicSelectedKeys");
selectedKeysField.setAccessible(true);
publicSelectedKeysField.setAccessible(true);
selectedKeysField.set(selector, selectedKeySet);
publicSelectedKeysField.set(selector, selectedKeySet);
selectedKeys = selectedKeySet;
Why Netty uses reflection to change a member of java library class sun.nio.ch.SelectorImpl?
I see one advantage instead of using Set from java collections it uses array based Set, which I believe will be faster. Is there any other specific reason for that ?
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow