Click here to Skip to main content
15,885,435 members
Articles / Desktop Programming / WPF
Tip/Trick

WPF: Getting Rid of "magic strings" Reduces Speed

Rate me:
Please Sign up or sign in to vote.
4.11/5 (3 votes)
2 May 2016CPOL1 min read 14.6K   59   1   9
Speed reduction that may occur when using expressions instead of string constants in INotifyChanged paradigm and a way to work around this

Introduction

A common practice is to use lambda expressions instead of string constants - "magic strings" - as the names of the properties in the INotifyChanged paradigm: something like NotifyPropertyChanged(()=>Property); instead of NotifyPropertyChanged("Property");
We will show that this reduces the speed of Binding execution. Also, we will show a trivial way to work around this.

Property Name Infer From Expression

Generally such construction requires a property name infer from the expression:

C#
public static string NameInfer<T>( Expression<Func<T>> itemExpression )
{
    switch ( itemExpression.Body.NodeType )
    {
        case ExpressionType.Constant:
        default:
            return ( ( itemExpression.Body as ConstantExpression ).Value as String );

        case ExpressionType.MemberAccess:
            return ( ( itemExpression.Body as MemberExpression ).Member.Name );
    }
}

This calculation takes a certain time. Below - the results of time testing. An average of twenty thousand fold sendings of a text ("Lorem ipsum...") to property is calculated. Left side column - result for string constant property name; middle column - result for expression derived property name:

Image 1

Numerical values above may seem ridiculous, but with intensive communication between View and ViewModel can be significant; timing difference also becomes important in the case of ViewModel - ViewModel communications.

Repair

Fortunately repair is straightforward and somewhat dull. It is enough to declare readonly string variables and evaluate them once in a ViewModel's constructor using expression to string conversion, and to use these variables instead of constant property names in all further calls to NotifyPropertyChanged - something like in the code snippet below:

C#
    ...
private readonly string sr_view1Text2;
    ...
#region ctor
public View1Model()
{
    ...
    sr_view1Text2 = NameTypeHelper.NameInfer( ( ) => View1Text2 );
    ...
}
#endregion ctor

    ...
private string _view1Text2;
public string View1Text2
{
    get { return _view1Text2; }
    set
    {
        if ( _view1Text2 != value )
        {
            _view1Text2 = value;
            NotifyPropertyChanged( sr_view1Text2 );
        }
    }
}

Below - the results of testing: left side column - timing for constant string property name, middle column - for expression derived property name and the right side column - for readonly string variable which value is derived from expression only once.

Image 2

Timings for "magic strings" property names and readonly string variable property names are the same.

History

  • 3rd May, 2016: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Israel Israel
fan of Microsoft technology and Microsoft programming style

Comments and Discussions

 
QuestionUse nameof Pin
Markus Eßmayr4-May-16 1:46
Markus Eßmayr4-May-16 1:46 
AnswerRe: Use nameof Pin
Simon Bridge18-May-16 18:30
Simon Bridge18-May-16 18:30 
Exactly! But this assumes you have VS 2015/c# 6.0 - there's still lots of people on older versions.

OnPropertyChanged(nameof(SomeProperty));
PraiseInteresting Pin
Serge Desmedt2-May-16 20:29
professionalSerge Desmedt2-May-16 20:29 
QuestionThanks for posting. Pin
Pete O'Hanlon2-May-16 19:20
mvePete O'Hanlon2-May-16 19:20 
GeneralMy vote of 4 Pin
Klaus Luedenscheidt2-May-16 19:02
Klaus Luedenscheidt2-May-16 19:02 
Question[CallerMemberName] Pin
pajopajo2-May-16 18:51
pajopajo2-May-16 18:51 
AnswerRe: [CallerMemberName] Pin
JLuis Estrada2-May-16 19:01
JLuis Estrada2-May-16 19:01 
GeneralRe: [CallerMemberName] Pin
J4Nch2-May-16 21:16
J4Nch2-May-16 21:16 
AnswerRe: [CallerMemberName] Pin
William E. Kempf3-May-16 2:57
William E. Kempf3-May-16 2:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.