Optimize the code for graphicslayer