[caffe2] use AddNAlreadyReserved() when serializing blobs (#53400)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/53400
This is a reland of D26617038 (https://github.com/pytorch/pytorch/commit/b4a8d98247c4eac60ae594ae626886408dc4e2c0) after rebasing onto D26802576 (https://github.com/pytorch/pytorch/commit/f595ba1bae9a75c6403e4624e7294b5635acd40c).
Optimize the blob serialization code by using `AddNAlreadyReserved()` when
serializing tensor data, rather than making N separate `Add()` calls.
`AddNAlreadyReserved()` is a simple addition operation, while each `Add()`
call checks to see if it needs to reserve new space, and then updates the
element data, which is unnecessary in this case.
ghstack-source-id: 123567030
Test Plan:
This appears to improve raw serialization performance by 30 to 35% for float,
double, and int64_t types which use this function. This improvement appears
relatively consistent across large and small tensor sizes.
Reviewed By: mraway
Differential Revision: D26853941
fbshipit-source-id: 4ccaa5bc1dd7f7864068d71a0cde210c699cbdba