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

Make node bindings optionally return pre-rendered JSON buffers #5189

Merged
merged 1 commit into from
Sep 5, 2018

Conversation

danpat
Copy link
Member

@danpat danpat commented Sep 3, 2018

If you're using the NodeJS bindings with OSRM, and you're generating large responses, there can be significant overhead simply translating the OSRM JSON structure into a NodeJS object tree.

There are several use-cases where the next step you take is to simply render the NodeJS object into a JSON string, and send it over the network - using NodeJS as your OSRM HTTP server is one example of this.

This PR adds a new optional parameter to all the NodeJS API methods that allows you to configure the format of the response that OSRM returns. It defaults to the current object tree, but if you supply {format: 'json_buffer'}, it will instead cause OSRM to pre-render the JSON tree on a worker thread, then return a NodeJS Buffer object containing the JSON response string.

In cases where you only wanted to return the result to a client over the network anyway, pre-rendering the JSON has a significant speedup. It significantly unburdens the main NodeJS thread, allowing more concurrent work.

Timeline of a 5000x5000 table request and serialization over the network before this change:
screen shot 2018-09-01 at 12 30 04 pm

Timeline of a 5000x5000 table request and serialization over the network after this change:
screen shot 2018-09-01 at 12 29 19 pm

The Main Thread here represents the primary NodeJS event loop - less work done here means the event loop stays clear to handle i/O.

The time spent in the pthread_body has increased slightly in the second image - this shows the additional time spent rendering the JSON outside the main thread.

Tasklist

Requirements / Relations

Link any requirements here. Other pull requests this PR is based on?

@ghoshkaj ghoshkaj added this to the 5.19.0 milestone Sep 5, 2018
…string (this avoids a lot of overhead, and moves JSON string rendering out of the main event loop).
@danpat danpat force-pushed the danpat_node_buffer_response branch from bfe11b3 to 4058eae Compare September 5, 2018 21:40
@danpat danpat merged commit 0971f06 into master Sep 5, 2018
@chaupow chaupow deleted the danpat_node_buffer_response branch September 28, 2018 21:41
datendelphin added a commit to fossgis-routing-server/osrm-backend that referenced this pull request Nov 19, 2020
  - Changes from 5.18.0:
    - Optimizations:
      - CHANGED: Use Grisu2 for serializing floating point numbers. [Project-OSRM#5188](Project-OSRM#5188)
      - ADDED: Node bindings can return pre-rendered JSON buffer. [Project-OSRM#5189](Project-OSRM#5189)
    - Profiles:
      - CHANGED: Bicycle profile now blacklists barriers instead of whitelisting them [Project-OSRM#5076
](Project-OSRM#5076)
      - CHANGED: Foot profile now blacklists barriers instead of whitelisting them [Project-OSRM#5077
](Project-OSRM#5077)
      - CHANGED: Support maxlength and maxweight in car profile [Project-OSRM#5101](Project-OSRM#5101]
    - Bugfixes:
      - FIXED: collapsing of ExitRoundabout instructions [Project-OSRM#5114](Project-OSRM#5114)
    - Misc:
      - CHANGED: Support up to 512 named shared memory regions [Project-OSRM#5185](Project-OSRM#5185)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants