How to make sure the AOT compiler generates code to instantiate grids on iOS

Sometimes, the AOT compiler cannot figure out all thee code it needs to generate for an iOS project. When you call this code, the game crashes.

There are a few different causes.

  • The one is generic methods that call other generic methods, where one of the parameters is a value type. Grids implement points use structs, so many of the generic methods have value-typed type parameters. Most of these work fine, but some of them call other generic methods, and they sometimes don’t get generated, so that your came crashes when the original method is called.
  • Some collections (List, HashSet, etc.) generate equality comparers automatically. This works fine as long as the items in the collection are not value types.
  • Some LINQ expressions crash, presumably when they call the underlying collection’s methods that use the automatically generated equality comparers.

To make Grids compile with iOS, follow these steps:

  • Use PointList, instead of List, for lists of points.
  • For LINQ exressions that crash, firs convert the intermediate results to a PointList, using the ToPointList method.
  • For HashSets, and Dictionaries, where the keys are points, specify a comparer to use in the constructor of these classes. You can use the PointComparer that come with grids for this purpose.
  • For other methods that crash, you can add compiler hints. We have added some hints as a template for basic grid construction, but you may need to add one or two more depending on what you are using. The scenario is typically you call a method A, which in turns calls B. The game crashes, giving you an error saying that B cannot be JITed. To have B generated, you have to call it somewhere explicitly with the right type, B(). You can call all your hints from the same place. Anywhere is fine, although the Start or Awake method in a central class (such as your GameManager) is a good place. Make sure that they don’t get stripped by the compiler!

If you use a grid with your own cell type, you may need to add hits for the grid’s construction. To add compiler hints from the template, follow these steps:

  1. Find the method in Templates/CompilerHints.cs that corresponds to the grid you are using. For example, if you use a RectGrid, then find the method __CompilerHint__Rect().
  2. Copy this method to a place where you can easily call it (typically in the file where you instantiate your grid).
  3. Change the type __CellType to whatever cells you are using in your grid. You need a version of the hint method for each type of cell you are using.
  4. You need to call this method in a way that makes sure that the compiler does not take the call out as an optimization step. It doe snot really matter where you call it, as long as it is definitely called. A good place to put it is just before you construct the grid. The method always return true, so you can call it like this:
if(!__CompilerHint__Rect()) return;

To find out more about this issue, see AOT Compiler Limitations and Workarounds for AOT compiler limitations.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top