From e1041ea20e6b025357be63e3ee698cf0588b7ac6 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 19 Aug 2023 03:25:17 +0900 Subject: [PATCH] internal/atlas: replace a global map with a struct member This should be a pure performance improvement. Updates #2586 Updates #2601 --- internal/atlas/image.go | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/internal/atlas/image.go b/internal/atlas/image.go index ac214928b2af..fab56151f761 100644 --- a/internal/atlas/image.go +++ b/internal/atlas/image.go @@ -67,9 +67,11 @@ const baseCountToPutOnSourceBackend = 10 func putImagesOnSourceBackend(graphicsDriver graphicsdriver.Graphics) { // The counter usedAsDestinationCount is updated at most once per frame (#2676). for i := range imagesUsedAsDestination { - if i.usedAsDestinationCount < math.MaxInt { + // This counter is not updated when the backend is created in this frame. + if !i.backendJustCreated && i.usedAsDestinationCount < math.MaxInt { i.usedAsDestinationCount++ } + i.backendJustCreated = false delete(imagesUsedAsDestination, i) } @@ -83,10 +85,6 @@ func putImagesOnSourceBackend(graphicsDriver graphicsdriver.Graphics) { } delete(imagesToPutOnSourceBackend, i) } - - for i := range imagesBackendJustCreated { - delete(imagesBackendJustCreated, i) - } } type backend struct { @@ -132,8 +130,6 @@ var ( imagesUsedAsDestination = map[*Image]struct{}{} - imagesBackendJustCreated = map[*Image]struct{}{} - deferred []func() // deferredM is a mutex for the slice operations. This must not be used for other usages. @@ -168,7 +164,8 @@ type Image struct { imageType ImageType disposed bool - backend *backend + backend *backend + backendJustCreated bool node *packing.Node @@ -229,6 +226,10 @@ func (i *Image) paddingSize() int { func (i *Image) ensureIsolatedFromSource(backends []*backend) { i.resetUsedAsSourceCount() + // imagesUsedAsDestination affects the counter usedAsDestination. + // The larger this counter is, the harder it is for the image to be transferred to the source backend. + imagesUsedAsDestination[i] = struct{}{} + if i.backend == nil { // `theSourceBackendsForOneFrame` already includes `backends`. bs := make([]*backend, 0, len(theSourceBackendsForOneFrame)) @@ -236,17 +237,10 @@ func (i *Image) ensureIsolatedFromSource(backends []*backend) { bs = append(bs, b) } i.allocate(bs, false) - imagesBackendJustCreated[i] = struct{}{} + i.backendJustCreated = true return } - // imagesUsedAsDestination affects the counter usedAsDestination. - // The larger this counter is, the harder it is for the image to be transferred to the source backend. - // This counter is not updated when the backend is created in this frame. - if _, ok := imagesBackendJustCreated[i]; !ok { - imagesUsedAsDestination[i] = struct{}{} - } - if !i.isOnAtlas() { return }