Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do some profiling #59

Open
dom96 opened this issue Oct 10, 2021 · 5 comments
Open

Do some profiling #59

dom96 opened this issue Oct 10, 2021 · 5 comments

Comments

@dom96
Copy link
Owner

dom96 commented Oct 10, 2021

It might come as a surprise to some but I haven't actually profiled httpbeast thus far. I am getting more curious whether there are some low hanging fruit there to optimise.

I'll probably do it eventually, but if someone is interested in having a crack at it, please share what you find :)

@kubo39
Copy link
Contributor

kubo39 commented Oct 11, 2021

I've found httpbeast calls fcntl(2) every time to set nonblocking, even if it uses accept4(2).

@ajusa
Copy link
Contributor

ajusa commented Apr 21, 2022

I did some profiling, leaving some notes here:

httpbeast/src/httpbeast.nim

Lines 252 to 253 in 5202b4c

data.data.setLen(origLen + ret)
for i in 0 ..< ret: data.data[origLen+i] = buf[i]

These lines are a bit expensive - we could probably forgo the buffer and recv directly into the data.data string

httpbeast/src/httpbeast.nim

Lines 432 to 443 in 5202b4c

let respCode = $code
let bodyLen = $body.len
appendAll(
"HTTP/1.1 ", respCode,
"\c\LContent-Length: ", bodyLen,
"\c\LServer: ", serverInfo,
"\c\LDate: ", serverDate,
otherHeaders,
"\c\L\c\L",
body
)

The int to string conversions here are a little costly - using something like https://github.com/brentp/mosdepth/blob/master/int2str.nim and appending directly to the buffer would be faster.

@dom96
Copy link
Owner Author

dom96 commented Apr 22, 2022

Thanks @ajusa!

The int to string conversions here are a little costly - using something like https://github.com/brentp/mosdepth/blob/master/int2str.nim and appending directly to the buffer would be faster.

Regarding this, would it makes sense for Nim proper to adopt it for all int to str conversions? That way we can optimise everyone's code :D

@ajusa
Copy link
Contributor

ajusa commented Apr 23, 2022

Potentially, though I seem to recall Araq being against stdlib optimizations since they seem to inevitably break someone's code.

Additionally, I'm suggesting a different proc signature - rather than the proc allocating the string (as $ does), you pass in a var string for it to append onto. I'll look into it further if I have time.

@dom96
Copy link
Owner Author

dom96 commented Apr 23, 2022

though I seem to recall Araq being against stdlib optimizations since they seem to inevitably break someone's code.

Hm, for what it's worth: I think if that happens we can revert, I don't think we should be this defensive.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants