Skip to content

Commit e294a6a

Browse files
committed
Read into a temporary buffer instead of using prediction-based growth
Removed the 'presize' logic which attempted to predict packet size by dynamic resizing. There is no evidence that such prediction improves performance over using a fixed-size temporary buffer followed by memcpy. The new approach reduces memory operations, and can offer better real-time responsiveness. TCP_READ_BUF_SIZE is introduced as a configurable macro (default: 4096 bytes).
1 parent c86de86 commit e294a6a

File tree

2 files changed

+8
-16
lines changed

2 files changed

+8
-16
lines changed

silly-src/silly_conf.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040
#define SOCKET_MAX_EXP (16)
4141
#endif
4242

43+
#ifndef TCP_READ_BUF_SIZE
44+
#define TCP_READ_BUF_SIZE (4096)
45+
#endif
46+
4347
#ifndef TIMER_RESOLUTION
4448
#define TIMER_RESOLUTION (10) //ms
4549
#endif

silly-src/silly_socket.c

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ struct socket {
9898
fd_t fd;
9999
unsigned char protocol;
100100
unsigned char reading;
101-
int presize;
102101
enum stype type;
103102
size_t wloffset;
104103
struct wlist *wlhead;
@@ -214,7 +213,6 @@ static void socketpool_init(struct silly_socket *ss)
214213
pool->sid = -1;
215214
pool->fd = -1;
216215
pool->type = STYPE_RESERVE;
217-
pool->presize = MIN_READBUF_LEN;
218216
pool->wloffset = 0;
219217
pool->wlhead = NULL;
220218
pool->wltail = &pool->wlhead;
@@ -303,7 +301,6 @@ static struct socket *allocsocket(struct silly_socket *ss, fd_t fd,
303301
assert(s->wlhead == NULL);
304302
assert(s->wltail == &s->wlhead);
305303
s->protocol = protocol;
306-
s->presize = MIN_READBUF_LEN;
307304
s->sid = id;
308305
s->fd = fd;
309306
s->wloffset = 0;
@@ -688,24 +685,15 @@ static ssize_t sendudp(fd_t fd, uint8_t *data, size_t sz,
688685
static int forward_msg_tcp(struct silly_socket *ss, struct socket *s)
689686
{
690687
ssize_t sz;
691-
ssize_t presize = s->presize;
692-
uint8_t *buf = (uint8_t *)silly_malloc(presize);
693-
sz = readn(s->fd, buf, presize);
688+
uint8_t tmpbuf[TCP_READ_BUF_SIZE];
689+
sz = readn(s->fd, tmpbuf, sizeof(tmpbuf));
694690
//half close socket need no data
695691
if (sz > 0 && s->type != STYPE_SHUTDOWN) {
692+
uint8_t *buf = (uint8_t *)silly_malloc(sz);
693+
memcpy(buf, tmpbuf, sz);
696694
report_data(ss, s, SILLY_SDATA, buf, sz);
697-
//to predict the pakcet size
698-
if (sz == presize) {
699-
s->presize *= 2;
700-
} else if (presize > MIN_READBUF_LEN) {
701-
//s->presize at leatest is 2 * MIN_READBUF_LEN
702-
int half = presize / 2;
703-
if (sz < half)
704-
s->presize = half;
705-
}
706695
ss->netstat.recvsize += sz;
707696
} else {
708-
silly_free(buf);
709697
if (sz < 0) {
710698
report_close(ss, s, errno);
711699
freesocket(ss, s);

0 commit comments

Comments
 (0)