jart/json.cpp

Using std::string_view

Closed this issue · 1 comments

Have you considered using std::string_view instead of std::string for the public facing API? most of the time the JSON string would be on a buffer somewhere (read from disk, read from a socket, etc) and creating a std::string would be a waste of performance. I also wonder if using std::string_view internally will also improve performance due to the lack of allocation and copying of strings.

jart commented

I was using it, but ended up removing it, since I want this project to be simple and C++11 friendly. I found std::string_view somewhat cumbersome, due to the lack of implicit conversions between it and std::string. I think std::string should be good enough for this use case. Implementations of std::string usually reserve at least 16 bytes of memory in order to avoid memory allocation for small strings. Also, since json.cpp is simpler, it's relatively trivial for anyone who copies it into their codebase to change the interfaces to use std::string_view instead.