From 83b39b15f67846989249d1ce9cbdcccf6f67d174 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hugo=20M=C3=A5rdbrink?= Date: Sun, 9 Nov 2025 11:45:31 +0100 Subject: [PATCH] Optimise the read proc --- fjord_test.odin | 8 ++++---- server.odin | 12 ++++++------ 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/fjord_test.odin b/fjord_test.odin index bc89462..f066c2f 100644 --- a/fjord_test.odin +++ b/fjord_test.odin @@ -174,7 +174,7 @@ shutdown_concurrent_server :: proc( thread.destroy(t) } -assert_endpoint :: proc(url: string, expected_response: string) { +assert_endpoint_response :: proc(url: string, expected_response: string) { state, stdout, stderr, err := os2.process_exec( {command = []string{"curl", "-s", url}}, context.temp_allocator, @@ -232,15 +232,15 @@ test_server_general_ok :: proc(t: ^testing.T) { t := start_concurrent_server(&server) - assert_endpoint( + assert_endpoint_response( "http://127.0.0.1:8080/hello/world", "
Hello firstworld
", ) - assert_endpoint( + assert_endpoint_response( "http://127.0.0.1:8080/hello/lonely%20world/only", "
Hello secondlonely%20world
", ) - assert_endpoint( + assert_endpoint_response( "http://127.0.0.1:8080/hello/world/and/worlds%20friend", "
Hello thirdworldandworlds%20friend
", ) diff --git a/server.odin b/server.odin index 857f02a..baa5b0e 100644 --- a/server.odin +++ b/server.odin @@ -26,11 +26,6 @@ Server :: struct($Error_Type: typeid) { } @(private) -request_ended :: proc(buf: []byte) -> bool { - HTTP_END_SEQUENCE :: []byte{'\r', '\n', '\r', '\n'} - return bytes.index(buf, HTTP_END_SEQUENCE) != -1 -} - read_connection :: proc( client_socket: net.TCP_Socket, allocator := context.temp_allocator, @@ -39,6 +34,8 @@ read_connection :: proc( err: http.RequestError, ) { TCP_CHUNK_SIZE :: 1024 + HTTP_END_SEQUENCE :: []byte{'\r', '\n', '\r', '\n'} + buffer: bytes.Buffer bytes.buffer_init_allocator(&buffer, 0, 0, allocator) @@ -48,9 +45,12 @@ read_connection :: proc( if net_err != nil do return data, .NetworkError if bytes_read == 0 do break + + prev_len := len(buffer.buf) bytes.buffer_write(&buffer, chunk[:bytes_read]) - if request_ended(buffer.buf[:]) do break + search_start := max(0, prev_len - 3) + if bytes.index(buffer.buf[search_start:], HTTP_END_SEQUENCE) != -1 do break } return buffer.buf[:], nil