In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.