"Maximum call stack size" using Web Worker.

0 favourites
  • 9 posts
From the Asset Store
With this template you will learn how to use the GooglePlay Games native plugin
  • I'm repeating a function a few hundred times on initialization, it reaches "Maximum call stack size" when using web workers but it works fine when not using them.

    Is this supposed to happen? I've tried making the function asynchronous as some "Promise" discussions on Stack Overflow suggested, but it made no difference for me.

    Also, what am I losing by not using web workers? I understand they "deparallelize" work by opening virtual threads, but aren't computers already multi-threaded? Or is the cpu multi-thread feature limited by the browser?

    Tagged:

  • Best guess is that you've ran into an event that causes an infinite loop. As to how it only happens without worker mode is hard to say. Can you provide more info?

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I have a simple matrix of arrays of 0s (zeros), like below:

    	const Matrix = [
    		[0,0,0,0,0,0,0,0,0,0,0],
    		[0,0,0,0,0,0,0,0,0,0,0],
    		[0,0,0,0,0,0,0,0,0,0,0],
    		[0,0,0,0,0,0,0,0,0,0,0],
    		[0,0,0,0,0,0,0,0,0,0,0],
    		[0,0,0,0,0,0,0,0,0,0,0],
    		[0,0,0,0,0,0,0,0,0,0,0],
    		[0,0,0,0,0,0,0,0,0,0,0],
    		[0,0,0,0,0,0,0,0,0,0,0]
    	]
    

    I have a function that turns some of them into 1s based on some rulesets. It doesn't run forever, it's observable due to it working flawlessly without web workers. It's also observable that they are really fast, it takes a fraction of a second for them to run without web workers. I'd be glad to share the file if it helps, but there is really nothing more to it.

    Could this be some issue with Web Workers?

    EDIT: github.com/dupuqub/constructs/blob/master/Web_Worker_Map.c3p here's a simplified version. You can check that while using Web Workers it will fail sometimes, but without it, it always works. You can confirm it further by increasing "Globals.Destroy_Max" ("Globals" is a sprite) to a higher value.

  • It seems Chrome's web worker stack size is smaller than the main thread. Judging by the fact that sometimes it work, sometimes it doesn't, you are probably barely over the limit. So depending on the RNG, your recursive function Road_Destroy might cause a stack overflow.

    I suggest rewriting the function in an iterative manner (using a while). Less function calls means less overhead added to the call stack.

    Also, this particular function sometimes do nothing if the RNG select the same road twice or more, wasting numerous operations. It might not be noticeable in CPU time, but it probably explain why the recursive function sometimes caused the exception. It could be rewritten so it only select "undeleted" road every time.

  • Thanks for the reply. The "while" approach really solves it for web workers.

    As for it being rewritten so it selects only undeleted roads every time, I can't see how this is possible since the selection is random and for something like that to work I would have to repeat the process, therefore repeating the function in its entirety again. Is there some other way you can see it happening?

  • I would use permutation tables built with the AdvancedRandom plugin. One table per "matrix", with width*height (and a maximum of Globals.Destroy_Max) permutations.

    Here's a copy of your project file modified with my suggestion.

    drive.google.com/file/d/1u65oIXb1i1r-2E1awDpd3AOxyhon4cLY/view

  • What you did there seems really really good, but kinda overkill for my scope. I'll definitely be reading your code to learn more though, I've never used Advanced Random before and this seems like a good place to start.

    Thanks a bunch!

  • I don't think it is overkill at all! I think it is an awesome example of how to randomize something like this. I haven't had a need to use Advanced Random, but have worked on procedurally generated environments before (using a noise plugin). Advanced Random makes this a lot easier.

    the really nice thing about Advanced Random is that you can set a seed to always get the same results - for testing purposes or to let player share their seed so friends can play the same map they got to compare how they did.

    You would also want to set Advanced Random to replace the system random function - click the check box in the Advanced Random properties box. (you can set a seed there, or set one at runtime)

  • I get this error even when Worker is disabled. And I agree, you should avoid calling functions recursively so many times.

Jump to:
Active Users
There are 2 visitors browsing this topic (0 users and 2 guests)