Skip to content

Commit f1d43c4

Browse files
authored
Speed up buffer resetting (#10)
The combination of kmalloc and memset can be replaced with kzalloc, resulting in faster execution. Reference results: - kzalloc: 120 ns - kmalloc: 350 ns, memset: 500 ns
1 parent 0cfbd71 commit f1d43c4

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

http_server.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ static int http_server_worker(void *arg)
159159
allow_signal(SIGKILL);
160160
allow_signal(SIGTERM);
161161

162-
buf = kmalloc(RECV_BUFFER_SIZE, GFP_KERNEL);
162+
buf = kzalloc(RECV_BUFFER_SIZE, GFP_KERNEL);
163163
if (!buf) {
164164
pr_err("can't allocate memory!\n");
165165
return -1;
@@ -169,9 +169,7 @@ static int http_server_worker(void *arg)
169169
http_parser_init(&parser, HTTP_REQUEST);
170170
parser.data = &request;
171171
while (!kthread_should_stop()) {
172-
int ret;
173-
memset(buf, 0, RECV_BUFFER_SIZE);
174-
ret = http_server_recv(socket, buf, RECV_BUFFER_SIZE - 1);
172+
int ret = http_server_recv(socket, buf, RECV_BUFFER_SIZE - 1);
175173
if (ret <= 0) {
176174
if (ret)
177175
pr_err("recv error: %d\n", ret);
@@ -180,6 +178,7 @@ static int http_server_worker(void *arg)
180178
http_parser_execute(&parser, &setting, buf, ret);
181179
if (request.complete && !http_should_keep_alive(&parser))
182180
break;
181+
memset(buf, 0, RECV_BUFFER_SIZE);
183182
}
184183
kernel_sock_shutdown(socket, SHUT_RDWR);
185184
sock_release(socket);

0 commit comments

Comments
 (0)