Optimized resource encoder

Is now three times faster