demikernel
demikernel copied to clipboard
[inetstack] Unnecessary Buffer Copy in UDP Receive Path During Header Parsing
I happened to notice today that the UDP receive path makes an extraneous buffer copy during header parsing. The problem lies with how the header parsing is structured: there is a high-level "parse" function that minimally wraps a "parse_from_slice" function that does the real work. Unfortunately, "parse_from_slice" takes a slice and returns a slice (itself a subset of the passed-in slice), which loses the higher-level Buffer's metadata regarding what was parsed out. Therefore, "parse" needs to create a new heap-allocated Buffer to contain the subset slice which was passed out of "parse_from_slice", and this requires copying the data.
There doesn't appear to be a reason for this code structure, "parse_from_slice" is only called from "parse" (and a test routine). It would be far more efficient to have UDP's parse routine operate on the Buffer directly, like TCP's parse routine does. Fixing this would only require changing the implementation of the "parse" function (to include the functionality of "parse_from_slice"), its signature is fine (so changes would only be required to its internal operation).