Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add reserve function for array to reserve memory before adding json values into it #436

Closed
fnc12 opened this issue Jan 24, 2017 · 5 comments

Comments

@fnc12
Copy link

fnc12 commented Jan 24, 2017

std::vector has a very useful reserve function that is used to preallocate memory for future push_backs. It reduces push_back time in case if a number of items is high. So it would be useful to add this function to basic_json for array reserve

@gregmarr
Copy link
Contributor

gregmarr commented Jan 24, 2017

You can currently do this manually using this function:

/// get a pointer to the value (array)
    array_t* get_impl_ptr(array_t* /*unused*/) noexcept
    {
        return is_array() ? m_value.array : nullptr;
    }

Once the array exists in the json object, this function will give you a pointer to it, which you can use to call reserve yourself. (That's not to say that this shouldn't be added.)

@fnc12
Copy link
Author

fnc12 commented Jan 24, 2017

no, get_impl_ptr is private

@gregmarr
Copy link
Contributor

Oh, sorry, I thought these had been added as public. Maybe I was thinking of a request that never went through.

@nlohmann
Copy link
Owner

There is get_ptr.

@fnc12
Copy link
Author

fnc12 commented Jan 25, 2017

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants