Skip to content

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

@Mashrien

Description

@Mashrien

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 :)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions