Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How Netty optimizes the underlying Selector

Shulou Source: shulou.com Published: 2022-06-01 07:15:04 09月24日 Update

This article mainly shows you "Netty on the underlying Selector how to optimize", the content is easy to understand, clear, hope to help you solve doubts, the following let the editor lead you to study and learn "Netty on the underlying Selector how to optimize" this article.

An underlying Selector property of JDK is encapsulated when the NioEventLoop is created

Private Selector selector

So let's take a brief look at the implementation of this Selector at the JDK level.

Public abstract class SelectorImpl extends AbstractSelector {protected Set selectedKeys = new HashSet (); protected HashSet keys = new HashSet (); private Set publicKeys; private Set publicSelectedKeys

Protected SelectorImpl (SelectorProvider var1) {super (var1); if (Util.atBugLevel) {this.publicKeys = this.keys; this.publicSelectedKeys = this.selectedKeys;} else {this.publicKeys = Collections.unmodifiableSet (this.keys); this.publicSelectedKeys = Util.ungrowableSet (this.selectedKeys);}

From the source code, we can find that when the server listens to the event, it will be encapsulated into SelectionKey and put into HashSet, and then the program can take out the event from the HashSet to handle.

The time complexity of HashSet's add method is O (n), so Netty replaces the underlying HashSet with an array through reflection mechanism. After all, the time complexity of adding data to the array is O (1), so let's find the answer from the code.

NioEventLoop (NioEventLoopGroup parent, Executor executor, SelectorProvider selectorProvider, SelectStrategy strategy, RejectedExecutionHandler rejectedExecutionHandler) {selector = openSelector ();}

Follow up the openSelector () method

Private SelectorTuple openSelector () {final SelectedSelectionKeySet selectedKeySet = new SelectedSelectionKeySet ();}

It creates a SelectedSelectionKeySet object, and let's take a look at this class.

Final class SelectedSelectionKeySet extends AbstractSet {

SelectionKey [] keys; int size

SelectedSelectionKeySet () {keys = new SelectionKey [1024];}

@ Override public boolean add (SelectionKey o) {if (o = = null) {return false;}

Keys [size++] = o; if (size = = keys.length) {increaseCapacity ();}

Return true;}}

From here, we can find that the bottom layer of this Set set uses an array, and you can add elements to the array directly when you call the add method, time complexity O (1).

Next, take a look at Netty replacing that HashSet with reflection.

Field selectedKeysField = selectorImplClass.getDeclaredField ("selectedKeys"); Field publicSelectedKeysField = selectorImplClass.getDeclaredField ("publicSelectedKeys")

SelectedKeysField.set (unwrappedSelector, selectedKeySet); publicSelectedKeysField.set (unwrappedSelector, selectedKeySet)

Netty replaces HashSet in the Selector class with this SelectedSelectionKeySet class through reflection.

The reason why this place has been made like this by Netty is actually for the sake of performance. Because this place is the source of data reading and writing, if the performance of this place is not high, it will seriously affect the performance of the program. In the final analysis, it comes back to the knowledge of data structure.

The above is all the content of the article "how Netty optimizes the underlying Selector". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Underlying array complex content place complexity performance data method time article reflection event knowledge program learning encapsulation help in the final analysis Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei MySQL Docker macOS MariaDB