C++ network sockets, SCTP and packet size -
i'm developing server using connection-oriented sctp serve small number of clients. after finishing first prototype naive implementation, i'm profiling application optimize. turns out, 1 of 2 main consumers of cpu time networking part.
there 2 questions efficiency of application-level protocol have implemented:
1) packet size
currently, use maximum packet size of 64 bytes. can find many posts discussing packet sizes big, can small? sctp allows me read 1 packet @ time - upd - while guaranteeing in-order delivery - tcp - simplified implementation significantly. however, if understand correctly, cost 1 syscall each , every time send packet. amount of syscalls have significant impact on performance? able shave off lot of cpu cycles sending messages in bunches in bigger packets, i.e. 1024 - 8192 bytes?
2) reading , writing buffers
i'm using memcpy move data , out of application-level network buffers. found many conflicting posts more efficient, memcpy or normal assignment. i'm wondering if 1 approach faster other in scenario:
option 1
void network::receivepacket(char* packet) { uint8_t param1; uint16_t param2 uint32_t param3; memcpy(¶m1, packet, 1); memcpy(¶m2, packet+1, 2); memcpy(¶m3, packet+3, 4); // handle packet here } void network::sendpacket(uint8_t param1, uint16_t param2, uint32_t param3) { char packet[7] memcpy(&packet, ¶m1, 1); memcpy(&packet+1, ¶m2, 2); memcpy(&packet+3, ¶m3, 4); // send packet here }
option 2
void network::receivepacket(char* packet) { uint8_t param1; uint16_t param2 uint32_t param3; param1 = *((uint8_t*)packet); param2 = *((uint16_t*)packet+1); param3 = *((uint32_t*)packet+3); // handle packet here } void network::sendpacket(uint8_t param1, uint16_t param2, uint32_t param3) { char packet[7] *((uint8_t*)packet) = param1; *((uint16_t*)packet+1) = param2; *((uint32_t*)packet+3) = param3; // send packet here }
the first 1 seems lot cleaner me, i've found many posts indicating maybe second 1 quite bit faster.
any kind of feedback of course welcome.
as far know compilers optimize memcpy calls in particular should use it.
about first question:
- summary: make packet size big can , avoid possibility of having enlowered cpu performance.
a syscall
, system call, os replying or processing request , every time request being executed in kernel, moderate amount of work. honest not familiar sctp
concept, matter of fact haven't dealt socket programming since last time worked on stuff , created server via tcp. remember mtu relevant physical layer element 1500
, recall implementing packet size 1450-1460
, trying maximum packet size underneath 1500
cap.
so im saying if want os less active wont trouble cpu performance.
Comments
Post a Comment