Issue #5968: refactor cc.LayerGradient for performance