Optimization of mosquito--other optimizations (ix)

Source: Internet
Author: User

This article is written by the carefree son, forwarded please mark the original site:

http://blog.csdn.net/houjixin/article/details/46413941

Or

http://houjixin.blog.163.com/blog/static/356284102015584617535/

9.1, free space management mechanism optimization

Mosquito in the original version of the program, when a new connection comes in, you need to scan the entire context to find an idle location to hold the context generated by the new connection, and if no idle location is found, use realloc to expand the location of a context. This mode of operation has a very inefficient two points:

1) Scan the context and scan the context array each time a new connection comes in to find an idle context position;

2) When there is no idle contxt in the current context array, only realloc is used to expand a position;

For these two causes of inefficiency, the following two ways of optimization are used:

1) for a full scan of the context array, a dynamic array is added to hold the index of all currently idle context, which is obtained directly from the dynamic array when a new connection comes in, without having to scan the entire context array each time;

2) for ReAlloc only to expand a context location, modified to each time the context space is not enough, will call ReAlloc request a batch (for example, 1000) the location of the context, and then put the index of these idle locations into the dynamic array.


9.2. Optimization of message sending mechanism

9.2.1 Optimization Reasons

Mosquitto receive and forward messages are processed separately in the original version of the message sending mechanism in the order of:

1) in the Mosquitto_main_loop function, based on the results returned by poll, if a ready socket is to send a message to a topic, search all the subscription trees to find all the subscription lists subscribed to the topic. and hangs the message in the message queue for each context in the subscription list, at which point the message is not sent to the subscriber.

2) Centrally scan all the context in function Mosquitto_main_loop and send a message: If its message queue is not empty, the message in the message queue is sent out.

The advantage of this messaging mechanism is that it is logically clear, but its efficiency is very low, because every time the message is sent, all the context needs to be scanned to determine which context has the message sent and which does not.

9.2. 2 Optimization method

For the Mosquitto message sending mechanism in need of full scan all the context caused by the inefficient problem, this optimization will add a hash table, the hash table will save all the context with the message, when the message is sent only to scan the context of the hash, Instead of scanning all of the context, and thus improve the efficiency of the system, the specific operation is:

1) Define a hash table in which each structure consists of a socket and a corresponding context for that socket, which is used to hold all message queue non-empty context.

2) Put the message queue not empty context into the hash table, this process is completed in the processing of epoll return results. This process, like poll, requires searching the subscription tree, querying all subscription lists that subscribe to the topic, and hooking the message to each context in the subscription list, and adding it to the hash table if the context is no longer in the hash table.

3) Send the message, traverse the hash table, and send out the message of the context in the hash table.

In the original Mosquitto program, when there is a message sent, you need to query the subscription tree, find the corresponding topic of the subscription list, and then hang the message to each context in the subscription list, when the number of clients is very large, each send a message query the subscription tree will take a certain amount of time, resulting in a serious waste of resources.

In view of the problem of the above message sending mechanism, the hash table is used to store topic to the list of subscriptions, and it is only necessary to find the hash table according to topic when the message is sent, and not to find the subscription tree, thus reducing the consumption of system resources.


Optimization of mosquito--other optimizations (ix)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.