Fix the slowdown of _object_to_tensor since 1.9 (#65721)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/65721
#Closes: https://github.com/pytorch/pytorch/issues/65696
The bug is introduced in https://github.com/pytorch/pytorch/pull/55861, and it causes 100X slowdown since 1.9.
ghstack-source-id: 139128267
Test Plan:
Performance test:
```
import time
from torch.distributed.distributed_c10d import _object_to_tensor
start = time.time()
_object_to_tensor("x" * 50_000_000)
print("Time:", time.time() - start)
```
Reviewed By: rohan-varma
Differential Revision: D31219794
fbshipit-source-id: 1abec38f9d51361c1eab6ad5efd87b589322e208