protobuf icon indicating copy to clipboard operation
protobuf copied to clipboard

Customize allocate functions in arena

Open arslan-urtashev opened this issue 10 years ago • 5 comments

Hi!

Can I create pull request with changing block_alloc and block_dealloc to custom allocator class?

It's need for parse heterogeneous protobufs in loop. For example:

(hereinafter, the pseudo-code)

message Test {
  repeated A a = 1;
  repeated B b = 2;
  repeated C c = 3;
}

Test messages lies in a row in file. I mean something like this:

while (true) {
  int32_t x = 0;
  read(fd, &x, sizeof(x));

  buffer.resize(x);
  read(fd, buffer.data(), buffer.size());

  Test test;
  test.ParseFromArray(buffer.data(), buffer.size());

  // Process test.
}

It will be so slow. Of course I can do something like this:

Test test;

while (true) {
  int32_t x = 0;
  read(fd, &x, sizeof(x));

  buffer.resize(x);
  read(fd, buffer.data(), buffer.size());

  test.ParseFromArray(buffer.data(), buffer.size());

  // Process test.
}

It will be much faster, but memory using will be awful. I can use Arena:

Arena arena;

while (true) {
  int32_t x = 0;
  read(fd, &x, sizeof(x));

  buffer.resize(x);
  read(fd, buffer.data(), buffer.size());

  Test* test = Arena::CreateMessage<Test>(&arena);
  test.ParseFromArray(buffer.data(), buffer.size());

  // Process test.

  arena.Reset();
}

Getting better, but still relatively slowly.

I can override block_alloc and block_dealloc, but it's not provide sufficient flexibility.

But if I will use custom pool allocator, I can get much better performance.

PoolAllocator allocator;
allocator.PreallocateBuffer(8_gb)

ArenaOptions options.
options.allocator = &allocator.

Arena arena(options);

while (true) {
  int32_t x = 0;
  read(fd, &x, sizeof(x));

  buffer.resize(x);
  read(fd, buffer.data(), buffer.size());

  Test* test = Arena::CreateMessage<Test>(&arena);
  test.ParseFromArray(buffer.data(), buffer.size());

  // Process test.

  arena.Reset();
}

P.S. It's real use of protocol buffers in openstreetmap pbf data parsing.

arslan-urtashev avatar Jan 19 '16 09:01 arslan-urtashev

Hi Avitella,

The arena code in protobuf library is very performance sensitive and used in some of Google's most CPU intensive services. Every change will need to be backed by benchmarks showing it doesn't affect existing binaries negatively. As the benchmarks we need to run are not opensourced and we also need to measure the performance of internal services, it's unlikely that we will accept changes from external users directly. Sorry for that. Feel free to suggest how the API should look like though. We will take that into consideration when we implement the feature in our internal code base.

xfxyjwf avatar Jan 19 '16 22:01 xfxyjwf

Interface doesn't matter. I just want customize block_alloc and block_dealloc by allocator with state. I understand that patch must have great performance.

Sorry, I didn't fully understand the answer. Do you mean, that any pull request will be rejected, because it's hard to check changes in google internal infrastructure?

arslan-urtashev avatar Jan 20 '16 14:01 arslan-urtashev

For every change that may have a significant impact, we require more proving data to support the change. If it's performance related, we ask for micro-benchmark results and load test results from some of the crucial Google server binaries. If it's a public API change, we ask for an analysis of existing usage of the old API, a migration path to the new API and an estimation of the migration cost. We will reject a change if the author can not provide sufficient supporting data for his change. We hold this same standard for both internal and external contributors, but as external contributors don't have access to Google internal resources, we will not accept a pull request from them if we suspect the pull request may have a significant impact on our internal code base and services. Such changes need to be driven by a Google engineer.

xfxyjwf avatar Jan 20 '16 19:01 xfxyjwf

Thanks for the detailed answer!

arslan-urtashev avatar Jan 20 '16 19:01 arslan-urtashev

I'm closing this bug based on the previous answer.

The one thing I would mention is: did you notice the "initial_block" parameter in ArenaOptions? https://github.com/google/protobuf/blob/master/src/google/protobuf/arena.h#L110

If your goal is to pre-allocate 8GB, the initial_block seems like it should give you exactly what you need with no overhead.

haberman avatar Mar 09 '17 21:03 haberman