Make node bindings optionally return pre-rendered JSON buffers #5189
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 NodeJSBuffer
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:Timeline of a 5000x5000
table
request and serialization over the network after this change: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?