Is my program effectively single-threaded?

https://github.com/acnologia000/fast_rust_http_server/blob/master/src/main.rs

i would like to know if above program is effectively single threaded or its able to make use of all 3 extra threads

all criticism for above whole repo is welcome (remember , its unfinished and will take years/months to get to final )

Yes, it uses total of 4 threads including the main thread. And here's some bonus criticisms:

  1. #[inline(always)] almost always makes your code slower.

Compiler can inline functions even without inline attribute if it seems to make code faster. But inlining not always has positive effect, for example inlining large but cold code may flush CPU code cache. But #[inline(always)] blindly enforces inlining and may give false huristics to optimizer.

  1. Cloning HashMap will also clones all its contents.

It seems the file_hash is a large hashmap that contains non-trivial length of values. When you .clone() on it, it clones the hashtable itself and also all the entries it has, effectively doubles the memory consumption as a result, also costs extra memcpy and allocation time. If this hashmap will not be modified after initialization, try wrapping it in Arc and distribute the shallow copies of it.

  1. format!("{}", &String::from_utf8_lossy(..)) doesn't make sense at all.

It creates new String from likely-utf8 data using lossy conversion method, and create another String with the same content, and drop the original one.

Anyway, I'm on mobile and my fingers are screaming now. All the _criticism_s I wrote are mostly ergonomic issues and you'll learn it by write more rusty code. Good luck and have nice code!

3 Likes

i am still bit unclear , that its able to leverage all 4 threads or its just using one thread at a time(effectively), for memory part , i tried passing references but value is being moved so i had to clone , and for format! part i agree that it was sheer stupidity from my side

Main suggestions is add error handling sooner rather than later. .unwrap(); isn't a good way to leave any code unless you explicitly want it to panic (or know it wont.)

Write integration test before adding more. Typically will be only the single machine simulating multiple connections but still better than piling on features without any tests for existing code.

If the main thread gets a slow connection then everything else stops receiving. (Less important other threads individually could queue up if it gets a slow connection.)

1 Like

so i should avoid processing request in main thread and use a bigger threadpool for avoiding queue , or make it async (i have no clue , i will have to read more)?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.