nlohmann::basic_json_document::shrink_to_fit¶
void shrink_to_fit();
-Releases capacity that is no longer needed, both of the node index and of the buffer for decoded strings (strings that contained escape sequences), e.g. after read() replaced a large document with a much smaller one. Like std::vector::shrink_to_fit(), this is a non-binding request: the library may keep more capacity than strictly necessary.
Exception safety¶
Strong guarantee: if an exception is thrown, there are no changes to the document.
Exceptions¶
May throw std::bad_alloc if the reallocation fails; on exception, the document is unchanged.
Complexity¶
Linear in node_count() plus the length of the decoded strings.
Notes¶
Invalidates views
Unlike moving the document, shrink_to_fit() invalidates every view taken from this document before the call, including a previously obtained root(): the node index is moved into a new, smaller allocation, and the old one is freed. Take a fresh view from root() after calling this function.
This is unlike std::vector::shrink_to_fit(), which promises nothing about validity but in practice often leaves iterators alone when it did not need to reallocate; here, an implementation that avoids a reallocation when possible would be an internal optimization only, not a guarantee to rely on.
Examples¶
Example
#include <iostream>
+Releases capacity that is no longer needed, both of the node index and of the buffer for decoded strings (strings that contained escape sequences), e.g. after read() replaced a large document with a much smaller one. Like std::vector::shrink_to_fit(), this is a non-binding request: the library may keep more capacity than strictly necessary.
Exception safety¶
Strong guarantee: if an exception is thrown, there are no changes to the document.
Exceptions¶
May throw std::bad_alloc if the reallocation fails; on exception, the document is unchanged.
Complexity¶
Linear in node_count() plus the length of the decoded strings.
Notes¶
The node index of a small document (an estimate of up to 1024 nodes, which is up to about 4 KiB of minified text) is allocated together with the document's header, so that parsing needs one allocation. That part cannot be released separately: shrink_to_fit() only trims a node index that has its own allocation, and the decoded strings.
Invalidates views
Unlike moving the document, shrink_to_fit() invalidates every view taken from this document before the call, including a previously obtained root(): the node index is moved into a new, smaller allocation, and the old one is freed. Take a fresh view from root() after calling this function.
This is unlike std::vector::shrink_to_fit(), which promises nothing about validity but in practice often leaves iterators alone when it did not need to reallocate; here, an implementation that avoids a reallocation when possible would be an internal optimization only, not a guarantee to rely on.
Examples¶
Example
#include <iostream>
#include <sstream>
#include <string>
#include <nlohmann/json_view.hpp>
@@ -46,4 +46,4 @@
Output:
true
true
true 1
-See also¶
- node_count - the number of index entries
- memory_usage - the number of bytes held by the document
- root - the view of the root value
Version history¶
- Added in version 3.13.0 unreleased.