Search Unity

WebGL exceptionSupport & performance

Discussion in 'Web' started by made-on-jupiter, Mar 6, 2015.

  1. made-on-jupiter

    made-on-jupiter

    Joined:
    May 19, 2013
    Posts:
    25
    Continued from this thread, sort of.

    We're having a bit of trouble here building without the WebGL exception support (WebGL player settings with exception support set to 'None').

    We need the extra performance that would get us, but the build errors in the browser at load. Without exception support, we can't actually debug what's wrong. However, when switching ON exception support to find the cause of the error, the build actually runs fine: no errors! So we're stabbing in the dark.

    I think it might have to do with the Emscripten try-catch injection that exception support enables. A few questions:

    1. Where and how are the try-catch blocks injected? Around every C#/C++ function/method?
    2. How much do they affect performance? It was mentioned blocks like that can't be as well optimized for asm.
    3. With exception support OFF, do try-catch blocks we have in our C# code still function?
    4. Do those C# try-catch blocks interfere with the Emscripten-injected try-catch blocks?
    5. Does the loss of (asm) performance also apply to the C# try-catch blocks?
    I understand these are fairly Emscripten-biased questions, but with IL2CPP in the mix I thought I might ask it here.

    Any other advice to solve our catch 22?
     
    Last edited: Mar 6, 2015
  2. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    Basically, try/catch is not supported in asm.js, so code which uses it will drop out of asm.js every so often. And unfortunately, once you enable it, "code which uses it" will not only be C# code which explicitly uses exceptions, but also a lot of other code, where llvm implicitly uses exception mechanisms for variable lifetime handling, etc. The real performance cost may be very different per case, though, so i can only recommend profiling.

    As for solving your issue:

    -Make a development build
    -Open the generated .js file (in a text editor which can handle very very large files, like vi).
    -Search for the cxa_throw function
    -Add the line "console.log(stackTrace());"

    You should now get a proper stack trace for your unsupported exception.
     
  3. frevd

    frevd

    Joined:
    Aug 29, 2013
    Posts:
    8
    Thanks for the hint, Jonas, quite helpful, should be in the build settings.

    I found it easier to extend the script on the fly, by editing the index.html and adding this


    // **************** insert stacktrace printing:
    var p = code.indexOf('___cxa_throw(ptr, type, destructor) {');
    if (p != -1)
    {
    code = code.substring(0, p + 37) + "console.log(stackTrace());" + code.substring(p + 37);
    console.log('Successfully extended cxa_throw.');
    }
    else
    console.log('Could not extend cxa_throw.');
    // *****************


    directly under the line

    var code = codeXHR.responseText;


    In order for this to work, one must also disable the direct script block insertion above that, by adding a "false &&" to the condition:

    if (false && !(!Math.fround)) {


    This also works in the non-development build, of course the function names are of no help.
    The non-dev JS is minified, so the string to search for is '___cxa_throw(ptr,type,destructor){' and the two 37s must be 34s.

    PS: The cxa_throw can be used to exclude certain types of non-fatal errors, or ignore them altogether (just adding a "return;" does the trick).
     
    Last edited: May 10, 2015
  4. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    FWIW, In Unity 5.1, Unity will automatically print exception stack traces when exception handling is disabled.
     
  5. frevd

    frevd

    Joined:
    Aug 29, 2013
    Posts:
    8
    Really? Didn't see that, I'm building using 5.0.1f1, all I get is some disturbing message about exception handling being disabled (and the stacktrace cannot really be helpful with minimized function names).
    Anyhow, I'm currently happy having discovered that I can completely catch all exceptions and rigorously ignore them instead of having the player crash with a message box (I'm having some dynamic gui code and sometimes there are GUILayout errors which have no real effect and can be ignored).

    Update: Oh, you are talking about the future 5.1, not the just released revision. Great.
    How about including (in the build settings) a list of exception types to ignore (or better a text to match, or a source function). Crashing the player is never a good option, errors aren't ever so fatal (all other unity platforms don't crash).
     
    Last edited: May 10, 2015
  6. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    I don't think that ignoring exceptions is ever a good option.

    Either make sure your code does not need to throw exceptions, or enable exception catching (which will have much less of a performance hit in 5.1 compared to 5.0).
     
  7. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    Seems counter-intuitive :)

    If i disable exceptions, why would it print the stack traces.. ?
     
  8. jonas-echterhoff

    jonas-echterhoff

    Unity Technologies

    Joined:
    Aug 18, 2005
    Posts:
    1,666
    Currently, it will just abort when you hit an exception and exceptions are disabled. It is useful to get a stack trace, so you know how to fix it.
     
  9. frevd

    frevd

    Joined:
    Aug 29, 2013
    Posts:
    8
    When then will we see 5.1 :D
     
  10. yuliyF

    yuliyF

    Joined:
    Nov 15, 2012
    Posts:
    197
    Now.. we have Unity 5.3.5xxx - are exceptions work? Or mayby Application.logMessageReceived ?