Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getdata<T> allocates an extra array of T[] #8304

Open
2 tasks done
circle601 opened this issue May 12, 2024 · 2 comments
Open
2 tasks done

Getdata<T> allocates an extra array of T[] #8304

circle601 opened this issue May 12, 2024 · 2 comments

Comments

@circle601
Copy link

Prerequisites

  • I have verified this issue is present in the develop branch
  • I have searched open and closed issues to ensure it has not already been reported.

MonoGame Version

3.8.1

Which MonoGame platform are you using?

MonoGame Cross-Platform Desktop Application (mgdesktopgl)

Operating System

Windows

Description

Getdata allocates an extra array of T[] GC runs reducing the frame rate

Steps to Reproduce

            texture.GetData<Color>(0, r, TextureCheckbuffer, 0, 1);

Minimal Example Repo

No response

Expected Behavior

No extra allocations needed

Resulting Behavior

Color[] is allocated each frame

Files

No response

@mrhelmut
Copy link
Contributor

This line:

https://github.com/MonoGame/MonoGame/blob/4a3d5e747ff570fbcd27ba7197d0eeac2606f906/MonoGame.Framework/Platform/Graphics/Texture2D.OpenGL.cs#L254C17-L254C86

and this line:

var temp = new T[Math.Max(this.height >> level, 1) * tFullWidth];

can't be optimized with traditional stackalloc or Span because of the generic type. It could be possible by working with a byte[] and converting elements in and out from T. Or having static class-wide caching array that grows when required (still garbage-prone, but much less).

This being said, polling GetData every frame isn't a good practice and it is recommended to rework your logic to avoid relaying on GetData this much.

@SimonDarksideJ
Copy link
Contributor

Does the answer suit your needs @circle601 and can this be closed now? Due to generics, this is not something solvable at this time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants