List
[Unity] TrackableResult.GetTrackable produces too much Garbage => bad performance
Posted Date: 2019-11-14 13:10     Edited Date: 2019-11-15 9:25     Writer: inactive

1. SDK Version: 4.1.4
2. Development Environment: (eg. Unity-Android, Unity-iOS, Native Android, Native iOS) Unity-Android
3. Tracker/Scanner: ImageTarget
4. License Type(Free / Pro-One Time Fee / Pro-Subscription / Enterprise): Free
5. Target Device(Optional):
 

While integrating MaxST as our replacement for Vuforia in an already existing application, we stumbled upon severe performance-issues on all our devices. Using the Unity-Profiler, I noticed that due to MaxST, 1 MB of Garbage was produces each frame, resulting in frequent, long invocations of the GC.

This garbage comes from the Trackable-class, which is created every frame in the call of TrackingResult.GetTrackable:

public Trackable GetTrackable(int index)
{
    Trackable t;
    t = new Trackable(NativeAPI.maxst_TrackingResult_getTrackable(cPtr, index));

    return t;
}
 

Incidentially, that Trackable-class contains an 1MB large array, which is thereby allocated each frame as well:

    public class Trackable
    {
        private ulong cPtr;
        private float[] glPoseMatrix = new float[16];
        private byte[] idBytes = new byte[100];
        private byte[] nameBytes = new byte[100];
        private byte[] cloudNameBytes = new byte[100];
        private byte[] cloudMetaBytes = new byte[1024*1024];

We were able to resolve the issue by creating one static Trackable-instance, whose cPtr-value was set in each GetTrackable-call:

    public partial class TrackingResult
    {
        private static Trackable trackable = null;

        public Trackable GetTrackableCached(int index)
        {
            var trackable_cPtr = NativeAPI.maxst_TrackingResult_getTrackable(cPtr, index);

            if (trackable == null)
                trackable = new Trackable(trackable_cPtr);
            else
                trackable.SetCPtr(trackable_cPtr); // custom method

            return trackable;
        }
    }

We belive that this, or a similar fix should be integrated into the SDK, as the difference in performance is quite large.

Posted Date: 2019-11-15 9:23     Edited Date: 2019-11-15 9:25     Writer: inactive

Thank you for good advice. We will review this internally.

 

Leo

Maxst Support Team