2

When adding a new page or user control to an ASP.NET webforms application, the code-behind class contains an empty Page_Load() event handler:

public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

I have an existing web app, where many pages and controls still contain these empty event handlers (they are not used).

Question: Is there any performance impact due to these empty event handlers and should they therefore be removed from all pages and controls?

Please note: I'm not mainly (or not only) concerned about any runtime-overhead, due to the empty event handler being called. I also wonder about any overhead while the page (markup) is JIT-compiled (because the event handlers have to be wired up to the events - probably using some reflection code).

Update: there was no real answer so far, so I can't accept any of them.

M4N
  • 94,805
  • 45
  • 217
  • 260
  • Does this answer your question? [What is the performance cost of autoeventwireup?](https://stackoverflow.com/questions/257112/what-is-the-performance-cost-of-autoeventwireup) – TylerH Jun 18 '21 at 21:50

3 Answers3

4

AutoEventWireup is not done at the compile time. When it set to true, the runtime has to look for each of the page event handlers using Delegate.CreateDelegate method for this. Here is a great article which describes this behaviour: Inside AutoEventWireup.

There is a similar question here: What is the performance cost of autoeventwireup?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Oleks
  • 31,955
  • 11
  • 77
  • 132
2

While the stack frame must be adjusted to enter and leave your method (and doing nothing) as opposed to simply calling the base implementation (in this case System.Web.UI.Page ), the performance impact is incredibly small and most likely unmeasurable so you should be fine.

Matt Bishop
  • 1,010
  • 7
  • 18
  • There is no base implementation, since Page_Load is an event handler and not an override of a virtual method! The event handler has to be wired up to the Load-event at runtime. – M4N Mar 23 '11 at 21:13
  • By base implementation I refer to the internal Page methods that trigger the delegate, I apologize if it was misleading or if it implies inheritance to some. The concept is the same however since the stack frame will be adjusted. Also, a common approach in many .NET shops is to create a class that inherits from Page that acts as a base implementation that an application's pages inherit from. – Matt Bishop Mar 24 '11 at 01:39
0

I'm fairly certain Page_Load occurs whether it's there or not. Much like PreRender occurs, or Page_Init.

Removing it will do nothing for performance.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Jack Marchetti
  • 15,536
  • 14
  • 81
  • 117
  • 1
    What do you mean with "Page_Load occurs"? Can you elaborate? PreRender or OnLoad are overridden virtual methods, while Page_Load and Page_Init are event handlers. I don't think they can be compared. – M4N Mar 23 '11 at 16:35
  • Well what I mean by that is, doesn't Page_Load get called whether or not you actually define the method? PreRender happens even if you don't define the method. – Jack Marchetti Mar 23 '11 at 16:40