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

Unnecessary mem usage and GC calls (low importance) #73

Open
Mashrien opened this issue Jul 30, 2024 · 0 comments
Open

Unnecessary mem usage and GC calls (low importance) #73

Mashrien opened this issue Jul 30, 2024 · 0 comments

Comments

@Mashrien
Copy link

In funcs that're called when a draw is needed, you're declaring multiple objects when it's actually better to declare and assign the objects in the constructor and avoid creating/destroying objects in the actual draw/paint methods

Below is taken from DarkUI/Forms/DarkForm.cs

    var g = e.Graphics;

    using (var p = new Pen(Colors.DarkBorder))
    {
    var modRect = new Rectangle(ClientRectangle.Location, new Size(ClientRectangle.Width - 1, ClientRectangle.Height - 1));
    g.DrawRectangle(p, modRect);
    }

Rather than creating a new series of objects (eg; NEW pen => NEW var modRect = blah, NEW size) you can declare these objects in the constructor and only update the values if/when needed-

    public partial Form {
        clientModRect = new Rect(blah);
        clientBorderPen = new Pen(blah);
        }

    public eventClientSizeChanged() { // or borderColorChanged, whatever update triggers needed
        //update the clientModRect, clientBorderPen, and anything else that needs changing
        }
            
    public Draw(PaintEventArgs e) {  // negating as many obj creations as possible to reduce mem usage and GC calls
        base.OnPaintBackground(e);
        if (!flatBorder) return;
        
        e.Graphics.DrawRectangle(clientBorderPen, clientModRect);
        
        } // end draw

This is entirely a performance-oriented suggestion/issue, and if it's not something y'all care about feel free to close and ignore :)

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

1 participant