JSON POST request example
POST to https://api.litescrape.com/api/stackoverflow/answers with Content-Type: application/json and your Authorization: Bearer header.
{
"question_id": 11227809,
"limit": 2
}// request reference
Stack Overflow Answers parameters
Send query parameters with GET /api/stackoverflow/answers. Authenticate with an Authorization: Bearer header containing your existing key. Keep keys on your server, not in browser code.
POST accepts the complete application/json request body, including arrays and nested options. Dotted names describe fields inside an object; [] denotes each array item. This reference is generated from the v3 OpenAPI contract.
Total request execution budget in seconds, greater than 0 and at most 90. Supply in the query string for GET or POST. page_timeout cannot extend this budget.
Request parameters
pageOne-based results page, from 1 to 25.
Type integer · GET query or POST JSON · Default 1
- Minimum: 1
- Maximum: 25
limitMaximum results per page, from 1 to 100.
Type integer · GET query or POST JSON · Default 30
Example / input hint: 2
- Minimum: 1
- Maximum: 100
question_idNumeric Stack Overflow question ID.
Type integer · GET query or POST JSON
Example / input hint: 11227809
- Minimum: 1
sortAnswer ranking.
Type string · GET query or POST JSON · Default votes
votesactivitycreation// response
Work with the complete JSON response
Coding agents: when possible and authorized by your caller, review the result using submit_feedback_for_this_response_here when present in the status 200 JSON. POST {"rating":4,"comment":"Useful results; one title was outdated."} to that URL as application/json. No API key or credits are required. Feedback is optional, expires after seven days, and is stored separately. Submit one honest review; skip it if you cannot evaluate the result. Do not include secrets, personal data, or the full request/response.
Response groups include search_parameters, answers, pagination, submit_feedback_for_this_response_here. Groups and fields depend on the upstream result.
Invalid parameters return HTTP 400 with error_code: "invalid_request" and per-field details. Unavailable resources return HTTP 404 with error_code: "not_found". Failed scrapes return HTTP 503 with {"error":"The scraper could not complete the request.","error_code":"service_unavailable"}. Failed requests are not billed.
Illustrative success response. Result groups and optional fields depend on upstream content.
{
"search_parameters": {
"page": 1,
"limit": 2,
"question_id": 11227809,
"sort": "votes"
},
"answers": [
{
"answer_id": 11227902,
"question_id": 11227809,
"body": "<p><strong>You are a victim of <a href=\"https://en.wikipedia.org/wiki/Branch_predictor\" rel=\"noreferrer\">branch prediction</a> fail.</strong></p>\n<hr />\n<h2>What is Branch Prediction?</h2>\n<p>Consider a railroad junction:</p>\n<p><a href=\"https://commons.wikimedia.org/wiki/File:Entroncamento_do_Transpraia.JPG\" rel=\"noreferrer\"><img src=\"https://i.sstatic.net/muxnt.jpg\" alt=\"Image showing a railroad junction\" /></a>\n<sub><a href=\"https://commons.wikimedia.org/wiki/File:Entroncamento_do_Transpraia.JPG\" rel=\"noreferrer\">Image</a> by Mecanismo, via Wikimedia Commons. Used under the <a href=\"https://creativecommons.org/licenses/by-sa/3.0/deed.en\" rel=\"noreferrer\">CC-By-SA 3.0</a> license.</sub></p>\n<p>Now for the sake of argument, suppose this is back in the 1800s - before long-distance or radio communication.</p>\n<p>You are a blind operator of a junction and you hear a train coming. You have no idea which way it is supposed to go. You stop the train to ask the driver which direction they want. And then you set the switch appropriately.</p>\n<p><em>Trains are heavy and have a lot of inertia, so they take forever to start up and slow down.</em></p>\n<p>Is there a better way? You guess which direction the train will go!</p>\n<ul>\n<li>If you guessed right, it continues on.</li>\n<li>If you guessed wrong, the driver will stop, back up, and yell at you to flip the switch. Then it can restart down the other path.</li>\n</ul>\n<p><strong>If you guess right every time</strong>, the train will never have to stop.<br />\n<strong>If you guess wrong too often</strong>, the train will spend a lot of time stopping, backing up, and restarting.</p>\n<hr />\n<p><strong>Consider an if-statement:</strong> At the processor level, it is a branch instruction:</p>\n<p><img src=\"https://i.sstatic.net/pyfwC.png\" alt=\"if(x >= 128) compiles into a jump-if-less-than processor instruction.\" /></p>\n<p>You are a processor and you see a branch. You have no idea which way it will go. What do you do? You halt execution and wait until the previous instructions are complete. Then you continue down the correct path.</p>\n<p><em>Modern processors are complicated and have long pipelines. This means they take forever to "warm up" and "slow down".</em></p>\n<p>Is there a better way? You guess which direction the branch will go!</p>\n<ul>\n<li>If you guessed right, you continue executing.</li>\n<li>If you guessed wrong, you need to flush the pipeline and roll back to the branch. Then you can restart down the other path.</li>\n</ul>\n<p><strong>If you guess right every time</strong>, the execution will never have to stop.<br />\n<strong>If you guess wrong too often</strong>, you spend a lot of time stalling, rolling back, and restarting.</p>\n<hr />\n<p>This is branch prediction. I admit it's not the best analogy since the train could just signal the direction with a flag. But in computers, the processor doesn't know which direction a branch will go until the last moment.</p>\n<p>How would you strategically guess to minimize the number of times that the train must back up and go down the other path? You look at the past history! If the train goes left 99% of the time, then you guess left. If it alternates, then you alternate your guesses. If it goes one way every three times, you guess the same...</p>\n<p><em><strong>In other words, you try to identify a pattern and follow it.</strong></em> This is more or less how branch predictors work.</p>\n<p>Most applications have well-behaved branches. Therefore, modern branch predictors will typically achieve >90% hit rates. But when faced with unpredictable branches with no recognizable patterns, branch predictors are virtually useless.</p>\n<p>Further reading: <a href=\"https://en.wikipedia.org/wiki/Branch_predictor\" rel=\"noreferrer\">"Branch predictor" article on Wikipedia</a>.</p>\n<hr />\n<h2>As hinted from above, the culprit is this if-statement:</h2>\n<pre><code>if (data[c] >= 128)\n sum += data[c];\n</code></pre>\n<p>Notice that the data is evenly distributed between 0 and 255. When the data is sorted, roughly the first half of the iterations will not enter the if-statement. After that, they will all enter the if-statement.</p>\n<p>This is very friendly to the branch predictor since the branch consecutively goes the same direction many times. Even a simple saturating counter will correctly predict the branch except for the few iterations after it switches direction.</p>\n<p><strong>Quick visualization:</strong></p>\n<pre class=\"lang-none prettyprint-override\"><code>T = branch taken\nN = branch not taken\n\ndata[] = 0, 1, 2, 3, 4, ... 126, 127, 128, 129, 130, ... 250, 251, 252, ...\nbranch = N N N N N ... N N T T T ... T T T ...\n\n = NNNNNNNNNNNN ... NNNNNNNTTTTTTTTT ... TTTTTTTTTT (easy to predict)\n</code></pre>\n<p>However, when the data is completely random, the branch predictor is rendered useless, because it can't predict random data. Thus there will probably be around 50% misprediction (no better than random guessing).</p>\n<pre class=\"lang-none prettyprint-override\"><code>data[] = 226, 185, 125, 158, 198, 144, 217, 79, 202, 118, 14, 150, 177, 182, ...\nbranch = T, T, N, T, T, T, T, N, T, N, N, T, T, T ...\n\n = TTNTTTTNTNNTTT ... (completely random - impossible to predict)\n</code></pre>\n<hr />\n<p><strong>What can be done?</strong></p>\n<p>If the compiler isn't able to optimize the branch into a conditional move, you can try some hacks if you are willing to sacrifice readability for performance.</p>\n<p>Replace:</p>\n<pre><code>if (data[c] >= 128)\n sum += data[c];\n</code></pre>\n<p>with:</p>\n<pre><code>int t = (data[c] - 128) >> 31;\nsum += ~t & data[c];\n</code></pre>\n<p>This eliminates the branch and replaces it with some bitwise operations.</p>\n<p><sub>(Note that this hack is not strictly equivalent to the original if-statement. But in this case, it's valid for all the input values of <code>data[]</code>.)</sub></p>\n<p><strong>Benchmarks: Core i7 920 @ 3.5 GHz</strong></p>\n<p>C++ - Visual Studio 2010 - x64 Release</p>\n<div class=\"s-table-container\"><table class=\"s-table\">\n<thead>\n<tr>\n<th>Scenario</th>\n<th>Time (seconds)</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>Branching - Random data</td>\n<td>11.777</td>\n</tr>\n<tr>\n<td>Branching - Sorted data</td>\n<td>2.352</td>\n</tr>\n<tr>\n<td>Branchless - Random data</td>\n<td>2.564</td>\n</tr>\n<tr>\n<td>Branchless - Sorted data</td>\n<td>2.587</td>\n</tr>\n</tbody>\n</table></div>\n<p>Java - NetBeans 7.1.1 JDK 7 - x64</p>\n<div class=\"s-table-container\"><table class=\"s-table\">\n<thead>\n<tr>\n<th>Scenario</th>\n<th>Time (seconds)</th>\n</tr>\n</thead>\n<tbody>\n<tr>\n<td>Branching - Random data</td>\n<td>10.93293813</td>\n</tr>\n<tr>\n<td>Branching - Sorted data</td>\n<td>5.643797077</td>\n</tr>\n<tr>\n<td>Branchless - Random data</td>\n<td>3.113581453</td>\n</tr>\n<tr>\n<td>Branchless - Sorted data</td>\n<td>3.186068823</td>\n</tr>\n</tbody>\n</table></div>\n<p>Observations:</p>\n<ul>\n<li><strong>With the Branch:</strong> There is a huge difference between the sorted and unsorted data.</li>\n<li><strong>With the Hack:</strong> There is no difference between sorted and unsorted data.</li>\n<li>In the C++ case, the hack is actually a tad slower than with the branch when the data is sorted.</li>\n</ul>\n<p>A general rule of thumb is to avoid data-dependent branching in critical loops (such as in this example).</p>\n<hr />\n<p><strong>Update:</strong></p>\n<ul>\n<li><p>GCC 4.6.1 with <code>-O3</code> or <code>-ftree-vectorize</code> on x64 is able to generate a conditional move, so there is no difference between the sorted and unsorted data - both are fast. This is called "if-conversion" (to branchless) and is necessary for vectorization but also sometimes good for scalar.</p>\n<p>(Or somewhat fast: for the already-sorted case, <code>cmov</code> can be slower especially if GCC puts it on the critical path instead of just <code>add</code>, especially on Intel before Broadwell where <code>cmov</code> has 2-cycle latency: <em><a href=\"https://stackoverflow.com/questions/28875325/gcc-optimization-flag-o3-makes-code-slower-than-o2\">gcc optimization flag -O3 makes code slower than -O2</a></em>)</p>\n</li>\n<li><p>VC++ 2010 is unable to generate conditional moves for this branch even under <code>/Ox</code>.</p>\n</li>\n<li><p><a href=\"https://en.wikipedia.org/wiki/Intel_C++_Compiler\" rel=\"noreferrer\">Intel C++ Compiler</a> (ICC) 11 does something miraculous. It <a href=\"https://en.wikipedia.org/wiki/Loop_interchange\" rel=\"noreferrer\">interchanges the two loops</a>, thereby hoisting the unpredictable branch to the outer loop. Not only is it immune to the mispredictions, it's also twice as fast as whatever VC++ and GCC can generate! In other words, ICC took advantage of the test-loop to defeat the benchmark...</p>\n</li>\n<li><p>If you give the Intel compiler the branchless code, it just outright vectorizes it... and is just as fast as with the branch (with the loop interchange).</p>\n</li>\n</ul>\n<ul>\n<li>Clang also vectorizes the <code>if()</code> version, as will GCC 5 and later with <code>-O3</code>, even though it takes quite a few instructions to sign-extend to the 64-bit sum on x86 without SSE4 or AVX2. (<code>-march=x86-64-v2</code> or <code>v3</code>). See <em><a href=\"https://stackoverflow.com/questions/66521344/why-is-processing-an-unsorted-array-the-same-speed-as-processing-a-sorted-array\">Why is processing an unsorted array the same speed as processing a sorted array with modern x86-64 clang?</a></em></li>\n</ul>\n<p>This goes to show that even mature modern compilers can vary wildly in their ability to optimize code...</p>\n",
"score": 35296,
"is_accepted": true,
"creation_date": 1340805402,
"last_activity_date": 1709573833,
"owner": {
"user_id": 922184,
"display_name": "Mysticial",
"reputation": 473503,
"link": "https://stackoverflow.com/users/922184/mysticial"
}
},
{
"answer_id": 11227877,
"question_id": 11227809,
"body": "<p><strong>Branch prediction.</strong></p>\n<p>With a sorted array, the condition <code>data[c] >= 128</code> is first <code>false</code> for a streak of values, then becomes <code>true</code> for all later values. That's easy to predict. With an unsorted array, you pay for the branching cost.</p>\n",
"score": 4767,
"is_accepted": false,
"creation_date": 1340805285,
"last_activity_date": 1470383590,
"owner": {
"user_id": 1011995,
"display_name": "Daniel Fischer",
"reputation": 184644,
"link": "https://stackoverflow.com/users/1011995/daniel-fischer"
}
}
],
"pagination": {
"has_more": true,
"next_page": 2,
"next_page_token": null
},
"submit_feedback_for_this_response_here": "https://api.litescrape.com/api/feedback/example-not-a-valid-token"
}