Dùng ValueTask<T> khi method thường xuyên hoàn thành synchronously — tránh được allocation overhead của heap-allocated Task object và giảm GC pressure.
- Ít hữu ích hơn cho I/O-bound work luôn async.
- Hãy benchmark để xác nhận cải thiện thực sự trước khi áp dụng.
ValueTaskcần xử lý cẩn thận — không được await nhiều lần và không được dùng trong các context phức tạp.
Use ValueTask<T> when methods frequently complete synchronously — it eliminates the allocation overhead of heap-allocated Task objects, reducing GC pressure.
- It is less beneficial for I/O-bound work that is always async.
- Benchmark to confirm real gains before adopting.
ValueTaskrequires careful handling: never await it more than once or use it in complex shared contexts.