Make shrinkToFit() documents more usable.
- Using a DynamicJsonDocument , I read in a document that may be very large.
- I then call .shrinkToFit() immediately afterward to free up as much heap memory as possible.
- After some processing, I try to replace the existing "updatedAt" field on my document with a new string of the same length.
- doc.overflowed() now returns true, and serializing the document includes { "updatedAt": null }
Based on the documentation, I think I understand that this is due to the monotonic allocator never reusing memory, and shrinkToFit() releasing all unused memory.
Ideal solution (for my somewhat specific case): Allow replacing a value to reuse the same memory, provided the new value is smaller or equal to the existing value. This would probably be very involved, and may be impossible without unrealistic changes.
(Hopefully) More practical and general alternative: Allow shrinkToFit() to accept a uint argument specifying some amount of unused memory to retain. What if the document can't shrink at all, or can't shrink enough to offer the requested unused memory? I think it's reasonable that the retained memory be best-effort only.