Working on a lil pet project in rust rn and discovered that using Result in the hot path is significantly affecting performance, how do people deal with this?
@cas some things i might try:
- reduce the size of the
Errorpayload, ideally to a single word (e.g. by Box'ing the error) - if this isn't enough, return nothing in the Err case (
()or a unit-structstruct ErrorType;) and store the error data out-of-band ie in a mutable variable somewhere. this makes the overhead of the result type minimal and makes the destructor a no-op. you could also try a hybrid approach where the error type is a plain enum with error codes, and then extra error metadata like strings are stored out of band - make the Result-manipulation easier for the compiler to eliminate, so that it can remove the byte-shuffling and only preserve the control flow. if you have a long function using Results, move the "meat" of the logic that doesn't use Result to a separate function, so that part doesn't get inlined. inlining typically stops at a certain function size so breaking code up like this makes it easier for compiler to make more granular inlining decisions. and inlining is super profitable because it allows the compiler to eliminate more code after inlining