Skip to content

Commit

Permalink
internal/atlas: replace a global map with a struct member
Browse files Browse the repository at this point in the history
This should be a pure performance improvement.

Updates #2586
Updates #2601
  • Loading branch information
hajimehoshi committed Aug 18, 2023
1 parent 77fd151 commit e1041ea
Showing 1 changed file with 10 additions and 16 deletions.
26 changes: 10 additions & 16 deletions internal/atlas/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -83,10 +85,6 @@ func putImagesOnSourceBackend(graphicsDriver graphicsdriver.Graphics) {
}
delete(imagesToPutOnSourceBackend, i)
}

for i := range imagesBackendJustCreated {
delete(imagesBackendJustCreated, i)
}
}

type backend struct {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -168,7 +164,8 @@ type Image struct {
imageType ImageType
disposed bool

backend *backend
backend *backend
backendJustCreated bool

node *packing.Node

Expand Down Expand Up @@ -229,24 +226,21 @@ 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))
for b := range theSourceBackendsForOneFrame {
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
}
Expand Down

0 comments on commit e1041ea

Please sign in to comment.