fuwaneko / node-protobuf

Google Protocol Buffers wrapper for Node.js [UNMAINTAINED]
181 stars 42 forks source link

Improves perf of serialize & parse #71

Closed InfinitiesLoop closed 8 years ago

InfinitiesLoop commented 8 years ago
  1. Adds a general perf test with a small but representative message.
  2. Reuses DynamicMessageFactory instead of recreating it on each call. Perf results are small but significant:

before:

    $ node test/perf/perfTest.js
    serializing and parsing a message 1,000,000 times...
    serialize: 13535ms
    parse: 9818ms

after:

    $ node test/perf/perfTest.js
    serializing and parsing a message 1,000,000 times...
    serialize: 12623ms
    parse: 9354ms

Google docs on DynamicMessageFactory encourage reusing them, because the class is essentially a cache mechanism: https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.dynamic_message

As it turns out, a DynamicMessage needs to construct extra information about its type in order to operate. Most of this information can be shared between all DynamicMessages of the same type. But, caching this information in some sort of global map would be a bad idea, since the cached information for a particular descriptor could outlive the descriptor itself. To avoid this problem, DynamicMessageFactory encapsulates this "cache". All DynamicMessages of the same type created from the same factory will share the same support data. Any Descriptors used with a particular factory must outlive the factory.