From: Egon Elbre <egonelbre@gmail.com>
splitTransform func was creating multiple copies of f32.Affine2D
due to not having access to the internal and passing around non-pointer.
This makes splitTransform from ~8ns to .Split ~2ns.
It seems that the non-pointer receiver was ~0.6ns slower in this case.
Signed-off-by: Egon Elbre <egonelbre@gmail.com>
---
f32/affine.go | 9 +++++++++
gpu/gpu.go | 13 ++-----------
2 files changed, 11 insertions(+), 11 deletions(-)
diff --git a/f32/affine.go b/f32/affine.go
index 667f7e98..b056f720 100644
--- a/f32/affine.go
+++ b/f32/affine.go
@@ -112,6 +112,15 @@ func (a Affine2D) Elems() (sx, hx, ox, hy, sy, oy float32) {
return a.a + 1, a.b, a.c, a.d, a.e + 1, a.f
}
+// Split a transform into two parts, one which is pure offset and the
+// other representing the scaling, shearing and rotation part.
+func (a *Affine2D) Split() (srs Affine2D, offset Point) {
+ return Affine2D{
+ a: a.a, b: a.b, c: 0,
+ d: a.d, e: a.e, f: 0,
+ }, Point{X: a.c, Y: a.f}
+}
+
func (a Affine2D) scale(factor Point) Affine2D {
return Affine2D{
(a.a+1)*factor.X - 1, a.b * factor.X, a.c * factor.X,
diff --git a/gpu/gpu.go b/gpu/gpu.go
index 43300492..0977415b 100644
--- a/gpu/gpu.go
+++ b/gpu/gpu.go
@@ -821,15 +821,6 @@ func (d *drawOps) addClipPath(state *drawState, aux []byte, auxKey opKey, bounds
state.cpath = npath
}
-// split a transform into two parts, one which is pure offset and the
-// other representing the scaling, shearing and rotation part
-func splitTransform(t f32.Affine2D) (srs f32.Affine2D, offset f32.Point) {
- sx, hx, ox, hy, sy, oy := t.Elems()
- offset = f32.Point{X: ox, Y: oy}
- srs = f32.NewAffine2D(sx, hx, 0, hy, sy, 0)
- return
-}
-
func (d *drawOps) save(id int, state f32.Affine2D) {
if extra := id - len(d.states) + 1; extra > 0 {
d.states = append(d.states, make([]f32.Affine2D, extra)...)
@@ -889,7 +880,7 @@ loop:
op.Decode(encOp.Data)
quads.key.outline = op.Outline
bounds := f32.FRect(op.Bounds)
- trans, off := splitTransform(state.t)
+ trans, off := state.t.Split()
if len(quads.aux) > 0 {
// There is a clipping path, build the gpu data and update the
// cache key such that it will be equal only if the transform is the
@@ -935,7 +926,7 @@ loop:
// Transform (if needed) the painting rectangle and if so generate a clip path,
// for those cases also compute a partialTrans that maps texture coordinates between
// the new bounding rectangle and the transformed original paint rectangle.
- t, off := splitTransform(state.t)
+ t, off := state.t.Split()
// Fill the clip area, unless the material is a (bounded) image.
// TODO: Find a tighter bound.
inf := float32(1e6)
--
2.34.2