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

std::string not allocated in Arena memory #4202

Closed
mrpi opened this issue Jan 20, 2018 · 5 comments
Closed

std::string not allocated in Arena memory #4202

mrpi opened this issue Jan 20, 2018 · 5 comments

Comments

@mrpi
Copy link
Contributor

mrpi commented Jan 20, 2018

Hi!

I have noticed that std::string objects are not constructed in the arena memory but only marked as owned by the arena.

I understand why the current design does not allow to put long strings completely into the arena, but why is the std::string object itself not in the arena?

This way, string values that fit into the short string optimization buffer would not cause any heap allocation outside the arena and long strings would only require one heap allocation instead of two.

The method ArenaStringPtr::CreateInstance currently looks this way:

void CreateInstance(::google::protobuf::Arena* arena,
                      const ::std::string* initial_value) {
    GOOGLE_DCHECK(initial_value != NULL);
    ptr_ = new ::std::string(*initial_value);
    if (arena != NULL) {
      arena->Own(ptr_);
    }
}

but I would have expected an implementation that looks something like this:

void CreateInstance(::google::protobuf::Arena* arena,
                      const ::std::string* initial_value) {
    GOOGLE_DCHECK(initial_value != NULL);
    // uses "new ::std::string" when arena is nullptr
    ptr_ = Arena::Create<::std::string>(arena, *initial_value);
}

Thank you!

P.S.: Support for std::string_view ([ctype = STRING_VIEW]) as a replacement of your internal StringPiece would be even better :-)

@arthur-tacca
Copy link

Related (but different): #1896 Zero copy API

@acozzette
Copy link
Member

@mrpi That is a good point; we should be able to allocate the std::string object on the arena. Do you want to send us a pull request with your fix?

@mrpi
Copy link
Contributor Author

mrpi commented Jan 24, 2018

@acozzette Yes, until now I have not tried this change by my self. I have just noticed that my application was doing more small allocations than expected.
I will have a closer look at it the next days and create a patch.

@mrpi
Copy link
Contributor Author

mrpi commented Jan 27, 2018

Patched with #4239. Benchmarks look fine.

@mrpi
Copy link
Contributor Author

mrpi commented Feb 1, 2018

The pull request #4239 is merged. Closing this issue.

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

4 participants