updates.
[silc.git] / silc_optimize
1 SILC Optimizations:
2 ===================
3
4 o Library
5
6         o There is currently three (3) allocations per packet in the
7           silc_packet_receive_process, which is used to process and
8           dispatch all packets in the packet queue to the parser callback
9           function.  First allocation is for parse_ctx, second for the
10           SilcPacketContext, and third for packet->buffer where the actual
11           data is saved.
12
13           The parse_ctx allocation can be removed by adding it as a
14           structure to the SilcPacketContext.  When the SilcPacketContext
15           is allocated there is space for the parse context already.
16
17           The silc_packet_context_alloc should have a free list of
18           packet contexts.  If free packet context is found from the list
19           it is returned instead of allocating a new one.  The library
20           could at first allocate them and save them to the free list
21           until enough contexts for smooth processing exists in the list.
22           This would remove a big allocation since the structure is
23           quite big, and even bigger if it would include the parse_ctx.
24
25           The packet->buffer can be optimized too if the SilcBuffer
26           interface would support free lists as well.  Maybe such could
27           be done in the same way as for SilcPacketContext.  The
28           silc_buffer_alloc would check free list before actually 
29           allocating new memory.  Since the packets in the SILC protocol
30           usually are about the same size (due to padding) it would be
31           easy to find suitable size buffer from the free list very
32           quickly.
33
34           These naturally cause the overal memory consumption to grow
35           but would take away many allocations that can be done several
36           times in a second.
37
38         o Move the actual file descriptor task callback (the callback that
39           handles the incoming data, outgoing data etc, that is implemnted
40           in server and client separately (silc_server_packet_process and
41           silc_client_packet_proces)) to the low level socket connection
42           handling routines, and create an interface where the application
43           can register a callbacks for incoming data, outoing data and EOF
44           receiving and maybe sending too, which the library will call
45           when necessary.  This way we can move the data handling in one 
46           place.
47
48         o Add silc_id_str2id to accept the destination buffer as argument
49           and thus not require any memory allocation.  Same will happen
50           with silc_id_payload_* functions.
51
52         o Rewrite the lib/silcutil/silcprotocol.[ch] not to have 
53           [un]register functions, but to make it context based all
54           the way.  The alloc should take as argument the protocol      
55           type and its callback (not only final callback).  It is not
56           good that we have now global list of registered protocols.
57
58
59 o Server
60
61         o When processing the decrypted and parsed packet we call the
62           silc_server_packet_parse_type function.  This function has a 
63           huge switch statement.  Replace this switch statment with pre-
64           defined table of function pointers where each of the slot
65           in the table represents the packet type (1 for packet type
66           value 1, 2 for value 2 and so on), and call the callback
67           found in the slot.  In this case we can do one-to-one mapping
68           of packet types to correct function.
69
70         o Same optimizations could be done with notify packets which
71           has huge switch statement too.  Same kind of table of notify
72           callbacks would be very easy to do, and achieve one-to-one
73           mapping of notify types.
74
75         o The parser callback in the server will add a timeout task for
76           all packets.  It will require registering and allocating a
77           new task to the SilcSchedule.  Maybe, at least, for server
78           and router packets the parser would be called immediately
79           instead of adding it to the scheduler with 0 timeout.  It
80           should be analyzed too how slow the task registering process
81           actually is, and find out ways to optimize it.