Live data from Hacker News

Node-fib: Fast non-blocking fibonacci server

github.com

31–40 of 124 posts

Re: Node-fib: Fast non-blocking fibonacci server

#31
The only difference in this implementation is that it uses memory-based caching (memoization) to compute given value once and then serve the cached copy.

Of course this is fast for 1000 iterations, since the effective cost is zero from the second request.

People really are misunderstanding the critique of fibbonacci as representing any CPU intensive task.

TLDR;

All the author did here was remove the CPU intensity by caching the calculation

Re: Node-fib: Fast non-blocking fibonacci server

#32

Author here, didn't really expect this to get picked up anywhere but since it has I'd like to point out the idea was to demonstrate that computationally expensive algorithms can be split across multiple iterations of the event loop to avoid blocking it. In this case concurrent requests take advantage of each others' memoisation, which would be somewhat trickier to do with threads as you'd probably need to worry about…

If I try to ask for a large number (say 1 million) I run out of RAM:

  $ node app.js 
  FATAL ERROR: CALL_AND_RETRY_2 Allocation failed - process out of memory
Somehow you're using O(n) RAM to do the calculation. Seems bad bro.

This code is the epitome of roflscale

Re: Node-fib: Fast non-blocking fibonacci server

#33

The only difference in this implementation is that it uses memory-based caching (memoization) to compute given value once and then serve the cached copy. Of course this is fast for 1000 iterations, since the effective cost is zero from the second request. People really are misunderstanding the critique of fibbonacci as representing any CPU intensive task. TLDR; All the author did here was remove the CPU intensity by…

It also uses process.nextTick aggressively (for each recursive call) so that the server can take other requests in the middle of computing a big sum.

Re: Node-fib: Fast non-blocking fibonacci server

#34
post #13

Author here, didn't really expect this to get picked up anywhere but since it has I'd like to point out the idea was to demonstrate that computationally expensive algorithms can be split across multiple iterations of the event loop to avoid blocking it. In this case concurrent requests take advantage of each others' memoisation, which would be somewhat trickier to do with threads as you'd probably need to worry about…

I think it'a a valuable tutorial to show node.js idioms. It's pretty easy to read, but I for one would struggle to write it as concisely.

Oh god, I hope you're joking.

Re: Node-fib: Fast non-blocking fibonacci server

#36
post #17

Author here, didn't really expect this to get picked up anywhere but since it has I'd like to point out the idea was to demonstrate that computationally expensive algorithms can be split across multiple iterations of the event loop to avoid blocking it. In this case concurrent requests take advantage of each others' memoisation, which would be somewhat trickier to do with threads as you'd probably need to worry about…

Yes, you too can by required by the compiler to implement cooperative multitasking by hand. In 2011. Yes. It is an answer to the criticism made, and I acknowledge that. But it is not a very good answer to the objection. The better answer is "don't do that in Node.js", which is still not all that great (it's really easy to accidentally write something that blocks badly), but is better.

"it's really easy to accidentally write something that blocks badly"

What is this nonsense? Isn't it time we knock this one on the head? Let me let you into a secret: If you write CPU intensive enough code in any framework you can eventually block all further requests. This whole debate was based on FUD and a complete misunderstanding of what node is all about. Read the previous posts and don't post generic, bullshit comments like "which is still not all that great".

Re: Node-fib: Fast non-blocking fibonacci server

#37
post #21

Hmmm... $ time curl localhost:3000/1000000 curl: (52) Empty reply from server real 1m0.392s user 0m0.006s sys 0m0.004s This actually crashes the server because it runs out of memory: $ node app.js FATAL ERROR: CALL_AND_RETRY_2 Allocation failed - process out of memory

You can probably get it work by asking it for increasingly large numbers to break up the calculation into stages.

v8's current GC implementation has a hard memory limit of 1GB which is uppable to 1.7GB on 64bit systems. There's a new GC implementation in the works which is supposed to be able to beat this.

The memory usage is due to the overhead of nextTick creating a new stack each time, and the interpreter keeping track of where the callbacks need to lead. Splitting the calculation up into batches could be incorporated into the algorithm quite simply by adding an additional task to async.series which called fib(i) for i to n with a step of 10 when n is > 100.

Obviously there are much more efficient and effective ways of calculating the fibonacci sequence, but I quite like the recursive algorithm for its clearness and its amenability to memoisation.

Re: Node-fib: Fast non-blocking fibonacci server

#38
post #17

Author here, didn't really expect this to get picked up anywhere but since it has I'd like to point out the idea was to demonstrate that computationally expensive algorithms can be split across multiple iterations of the event loop to avoid blocking it. In this case concurrent requests take advantage of each others' memoisation, which would be somewhat trickier to do with threads as you'd probably need to worry about…

Yes, you too can by required by the compiler to implement cooperative multitasking by hand. In 2011. Yes. It is an answer to the criticism made, and I acknowledge that. But it is not a very good answer to the objection. The better answer is "don't do that in Node.js", which is still not all that great (it's really easy to accidentally write something that blocks badly), but is better.

I am not quite sure what it is you are trying to say. Node does (in this case) require a bit more hands on approach than most other languages but that is because of a subtile but important difference. Node uses corporative concurrency whereas threads are not corporative. This is both a disadvantage (you are required to do more work and cannot take proper advantage of multiple CPUs) and a huge advantage (no locking is needed and you can share the results between different execution points).

Of course the real issue is that you should choose a better implementation of the algorithm.

Re: Node-fib: Fast non-blocking fibonacci server

#39
post #20

As much as this whole Ted-gate thing has turned into a amped up argument of sorts, there has been a significant amount of knowledge for everyone shaken out of the trees about node.js as a result. Take for example this implementation, how many people getting started with node knew about async or memoization? Could be just me, but I learned a lot about what not to do and how to do certain things more efficiently with n…

async is not part of the node.js core - it's an external module.

Re: Node-fib: Fast non-blocking fibonacci server

#40
post #17

Author here, didn't really expect this to get picked up anywhere but since it has I'd like to point out the idea was to demonstrate that computationally expensive algorithms can be split across multiple iterations of the event loop to avoid blocking it. In this case concurrent requests take advantage of each others' memoisation, which would be somewhat trickier to do with threads as you'd probably need to worry about…

Yes, you too can by required by the compiler to implement cooperative multitasking by hand. In 2011. Yes. It is an answer to the criticism made, and I acknowledge that. But it is not a very good answer to the objection. The better answer is "don't do that in Node.js", which is still not all that great (it's really easy to accidentally write something that blocks badly), but is better.

Well the other option for computationally expensive code is to use some sort of worker that runs a sufficiently fast language.

JavaScript on v8 is actually one of the fastest interpreted languages available, so unless you really need to drop down into C or similar, splitting across the event loop or using another node child process is not an unreasonable way to approach CPU heavy calculations.

Post reply on HN