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

Reduce UDP service per-packet allocation size #6263

Merged
merged 1 commit into from
Apr 8, 2016
Merged

Conversation

sparrc
Copy link
Contributor

@sparrc sparrc commented Apr 7, 2016

Required for all non-trivial PRs
  • Rebased/mergable
  • Tests pass
  • CHANGELOG.md updated

@sparrc
Copy link
Contributor Author

sparrc commented Apr 7, 2016

This change makes the UDP service only create a buffer that is the exact size of the incoming packet.

This will reduce memory pressure and number of GC cycles, my results sending 100,000 UDP points were:

  • udp-payload-size=0: 242 GC cycles
  • udp-payload-size=1500: 142 GC cycles
  • with change (no more config option): 114 GC cycles

@sparrc sparrc changed the title Create smaller UDP buffers Reduce UDP service per-packet allocation size Apr 7, 2016
@sparrc sparrc force-pushed the udp-smaller-buffer branch 3 times, most recently from b98319e to da975c1 Compare April 7, 2016 20:30
@sparrc
Copy link
Contributor Author

sparrc commented Apr 7, 2016

cc @joelegasse because you've been working on the influxdb relay, might be relevant to the UDP service there

@sparrc sparrc force-pushed the udp-smaller-buffer branch 2 times, most recently from 0e5659c to 2298349 Compare April 7, 2016 21:14
@joelegasse
Copy link
Contributor

I actually did something similar in the relay that entirely eliminates the need for the config option, and instead just has a per-listener 64K buffer. :-)

https://github.com/influxdata/influxdb-relay/blob/master/relay/udp.go#L113

@sparrc
Copy link
Contributor Author

sparrc commented Apr 7, 2016

this essentially does that too, I just don't really understand the influxdb config enough to know how to deprecate a config option?

@sparrc sparrc force-pushed the udp-smaller-buffer branch 3 times, most recently from acaba81 to 253975d Compare April 7, 2016 22:31
@sparrc
Copy link
Contributor Author

sparrc commented Apr 7, 2016

Erp, nevermind, I've figured it out

@sparrc
Copy link
Contributor Author

sparrc commented Apr 7, 2016

cc @sebito91, I believe you made this change to have a configurable udp-payload-size in the first place. This change goes a bit further and only allocates a buffer on each loop iteration that is the exact size of the incoming packet.

@sebito91
Copy link
Contributor

sebito91 commented Apr 8, 2016

I'm away from the machine at the moment but the reason we put in the option
was to prevent alloc of the entire 64k buffer each time you called read on
the socket...if you do a very cheesy memalloc in a loop and time it you
will see about 10x speed up for a smaller buffer (e.g. 1600 vs 64k).

I will post the findings when I'm back at the terminal.

On Thursday, April 7, 2016, Cameron Sparr notifications@github.com wrote:

cc @sebito91 https://github.com/sebito91, I believe you made this
change to have a configurable udp-payload-size in the first place. This
change goes a bit further and only allocates a buffer on each loop
iteration that is the exact size of the incoming packet.


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#6263 (comment)

Sebastian Borza

PGP: EDC2 BF61 4B91 14F2 AAB4 06C9 3744 7F3F E411 0D3E

@sebito91
Copy link
Contributor

sebito91 commented Apr 8, 2016

And re-reading the thread I think this change would actually be awesome,
that was my intent before but my Golang skill set at the time was wonky :/

👍 from me!

On Thursday, April 7, 2016, Sebastian Borza sborza@alumni.princeton.edu
wrote:

I'm away from the machine at the moment but the reason we put in the
option was to prevent alloc of the entire 64k buffer each time you called
read on the socket...if you do a very cheesy memalloc in a loop and time it
you will see about 10x speed up for a smaller buffer (e.g. 1600 vs 64k).

I will post the findings when I'm back at the terminal.

On Thursday, April 7, 2016, Cameron Sparr <notifications@github.com
javascript:_e(%7B%7D,'cvml','notifications@github.com');> wrote:

cc @sebito91 https://github.com/sebito91, I believe you made this
change to have a configurable udp-payload-size in the first place. This
change goes a bit further and only allocates a buffer on each loop
iteration that is the exact size of the incoming packet.


You are receiving this because you were mentioned.
Reply to this email directly or view it on GitHub
#6263 (comment)

Sebastian Borza

PGP: EDC2 BF61 4B91 14F2 AAB4 06C9 3744 7F3F E411 0D3E

Sebastian Borza

PGP: EDC2 BF61 4B91 14F2 AAB4 06C9 3744 7F3F E411 0D3E


bufCopy := make([]byte, n)
copy(bufCopy, buf[:n])
s.parserChan <- bufCopy
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC we have to zero out buf for each subsequent ReadFromUDP otherwise we fill the buf?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

each call to ReadFromUDP will write to buf from the beginning, so it won't ever fill

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is correct the way it's written. ReadFromUDP can't modify our slice, so buf will always refer to the full 64K slice, and will get data copied into it starting at index zero, and return n, the number of bytes that were copied for a given read.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why I thought (or remembered) differently, but as long as this checks out and passes tests then I'm fine with it.

@joelegasse
Copy link
Contributor

LGTM 👍

This will reduce memory pressure and number of GC cycles, my results
sending 100,000 UDP points were:

- udp-payload-size=0: 242 GC cycles
- udp-payload-size=1500: 142 GC cycles
- udp-payload-size=0 (with change): 114 GC cycles
- udp-payload-size=1500 (with change): 112 GC cycles
@sparrc sparrc merged commit 23e2c40 into master Apr 8, 2016
@mark-rushakoff mark-rushakoff deleted the udp-smaller-buffer branch April 12, 2016 03:31
@timhallinflux timhallinflux added this to the 0.13.0 milestone Dec 20, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants