Skip to content

RuntimeBinder Memory leaks #308

Closed
dotnet/arcade
#16884

Description

@DmitryDem

Method GetProperty(object target, string name) in the PathBinder.cs and IteratorBinder.cs cause memory leak in highload application. Could you use another way for getting property value instead of using RuntimeBuilder class?
image

Activity

  1. rexm commented on Aug 1, 2019

    @rexm
    Member

    PR welcome

  2. DmitryDem commented on Aug 1, 2019

    @DmitryDem
    Author

    Hi!
    Maybe we can use ExpressionTrees instead RuntimeBuilder?

    For example:
    var parameter = Expression.Parameter(typeof(object));
    var cast = Expression.Convert(parameter, value.GetType());
    var propertyGetter = Expression.Property(cast, propertyName);
    var castResult = Expression.Convert(propertyGetter, typeof(object));//for boxing
    var propertyRetriver = Expression.Lambda<Func<object, object>>(castResult, parameter).Compile();
    var retrivedPropertyValue = propertyRetriver(value);

    I know that's not a cheap operation because expression threes should be compiled. But i think that's it should be faster then DLR. What do you think?

  3. rexm commented on Aug 1, 2019

    @rexm
    Member
  4. DmitryDem commented on Aug 1, 2019

    @DmitryDem
    Author

    Yea, sorry, didn't notice

  5. DmitryDem commented on Aug 1, 2019

    @DmitryDem
    Author

    What do you think about CallSite caching? I think it's should help. The main problem occurs when creating CallSite objects.
    For example:

    internal static class CallSiteCache
        {
            private static readonly ConcurrentDictionary<string, CallSite<Func<CallSite, object, object>>> getters = new ConcurrentDictionary<string, CallSite<Func<CallSite, object, object>>>();
    
            internal static object GetValue(object target, string name)
            {
                var callSite = getters.GetOrAdd(name, CreateSite(target, name));
                return callSite.Target(callSite, target);
            }
    
            internal static CallSite<Func<CallSite, object, object>> CreateSite(object target, string name)
            {
                return CallSite<Func<CallSite, object, object>>.Create(Binder.GetMember(CSharpBinderFlags.None, name, target.GetType(), new[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) }));
            }
        }
    
  6. DmitryDem commented on Aug 2, 2019

    @DmitryDem
    Author

    I don't understand how this api can help. Usually this api uses for custom DynamicObject implementation and contains describing get methods of custom DynamicObject. Could you please clarify idea for using this api.

  7. DmitryDem commented on Aug 2, 2019

    @DmitryDem
    Author

    I have just implemented some solution. What do you think?

     private static object GetProperty(IDynamicMetaObjectProvider target, string name)
            {
                var self = (Expression)Expression.Constant(target);
                var metaObject = target.GetMetaObject(self);
                var binder = (GetMemberBinder)Binder.GetMember(CSharpBinderFlags.None, name, target.GetType(), new[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) });
                var member = metaObject.BindGetMember(binder);
                var final = Expression.Block(
                    Expression.Label(CallSiteBinder.UpdateLabel),
                    member.Expression
                );
                var param = Expression.Parameter(typeof(object));
                var lambda = Expression.Lambda(final, param);
                var @delegate = lambda.Compile();
                return @delegate.DynamicInvoke(target);
            }
    
  8. rexm commented on Aug 2, 2019

    @rexm
    Member

    That all makes sense to me except I don’t entirely understand the Lambda wrapper bit - can you explain?

  9. DmitryDem commented on Aug 2, 2019

    @DmitryDem
    Author

    Yes lambda wrapper could be simplified. I think we could replace Lambda wrapper with this

    var lambda = Expression.Lambda<Func<object>>(final).Compile();
                return lambda();
    

    These lines do not make sense.

     var param = Expression.Parameter(typeof(object));
                var lambda = Expression.Lambda(final, param);
    
  10. rexm commented on Aug 2, 2019

    @rexm
    Member

    The DynamicMetaObject has other properties that look like they give direct access to the underlying value without creating a new expression tree - take a look at the Properties section on https://docs.microsoft.com/en-us/dotnet/api/system.dynamic.dynamicmetaobject?view=netframework-4.8

  11. DmitryDem commented on Aug 2, 2019

    @DmitryDem
    Author

    Do you mean "Value" property? It does't work for ExpandoObject. It just return null.
    image
    Instead of we can call Expression that contains logic access to the member
    image
    Every custom DynamicMetaObject that inherited from DynamicMetaObject can override BindGetMember and provide Expression for access to their members.
    Look at example for JObject from Json.NET library
    image
    If you know another way I will be grateful for the help

  12. rexm commented on Aug 2, 2019

    @rexm
    Member

    ExpandoObject implements IDictionary<string,object> so you may want to consider reordering the type checks to give that precedence. The structure of the routine should be that if a value isn't resolvable after one attempt, fall back up and keep trying others.

  13. XcoderBlood commented on Jan 16, 2020

    @XcoderBlood

    Was this ever fixed?

  14. added a commit that references this issue on Apr 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions