Git Product home page Git Product logo

Comments (5)

VitaliyMF avatar VitaliyMF commented on May 27, 2024 1

@some1one this issue is actually about reflection-based invocations made with InvokeMethod.cs: they may be even faster when compiled to delegate, this is possible in some cases (when no method overloads for the same number of arguments).

As I understand, you mean caching of variables; I've no idea why you need one more "Eval" overload as you can easily cache variables on your side with public object Eval(string expr, Func<string,object> getVarValue) overload, for example:

Func<string,object> realGetValueDelegate = ... ;
var varCache = new Dictionary<string,object>();
var exprResult = lambdaParser.Eval(exprStr, (varName) => {
	if (varCache.TryGetValue(varName, out var v))
		return v;
	var val = realGetValueDelegate(varName);
	varCache[varName] = val;
	return val;
});

No need to inherit from LambdaParser; if you like you can add extension method that adds this 'variables caching' behavior and use it in your project.

However, caching of variables may cause various side-effects and usually this is very project-specific thing. For now I'm not sure that it is better to include this code into library.

from lambdaparser.

lafar6502 avatar lafar6502 commented on May 27, 2024

Hi, i was just going to add an issue for that, but what you described is quite close
The idea i had was to use NReco lambda parser to dynamically build lambda expressions for Linq queries

assuming I have IQueryable interface, i'd like to build filtering predicate expression for the where function: Expression<Func<T, bool>>
but im not sure if it's possible with NReco to build right type of expression (lambda with correct parameter type). Could you give me some suggestions here if/how to do that?

** edit
I see how the parsing works in nreco - all parameters identified in the expression are then translated to lambda parameters - so you have to supply all of them to invoke.
But i'd like something different - could the parser treat all parameters as fields on the input object (the single parameter to the lambda)? This way we could easily make lambdas for linq operations on collections.
Example

this expression
"\"Hello, \" + Name + \", your birth year is \" + BirthDate.Year"

will be parsed as such lambda:
(Name, BirthDate) => "Hello, " + Name + ", your birth year is " + BirthDate.Year

what i'd like to get is
x => "Hello, " + x.Name + ", your birth year is " + x.BirthDate.Year

where x is an object of some type T (T can be known at the time of parsing)

possible?

Thanks
RG

from lambdaparser.

VitaliyMF avatar VitaliyMF commented on May 27, 2024

But i'd like something different - could the parser treat all parameters as fields on the input object (the single parameter to the lambda)?

yes - this is possible. In fact you already can do that with some piece of extra code smth like:

Expression expr = new LambdaParser().Parse("a>5");
var exprParams = LambdaParser.GetExpressionParameters(expr);
// then add code that maps Func<,> arguments to exprParams
// and compile it to Func<,bool> delegate that can be used in IQueryable 

Also you can check how object Eval(string expr, Func<string,object> getVarValue) is implemented in LambdaParser.cs, I believe its code can answer on most of your possible questions.

I guess it is possible to add overloads like Expression<Func<T1,T2>> LambdaParser.Parse<T>(string expr, string t1VarNameInExpr) that will make all these things inside LambdaParser library; feel free to add new issue for this.

BTW, this issue is about quote another thing - evaluation performance optimization. Linq expressions itself are strongly typed, but in most cases this is badly usable for user-defined formulas or expressions, especially if variables are not strongly typed. LambdaParser supports evaluation that is similar to C# 'dynamic' type (thats why internally LambdaParameterWrapper is used) but this has some implication on execution performance - it starts to be noticeable when expression is evaluated > 100,000 times (say, for dataset filtering). It is still a place to get better execution time by replacing reflection methods invocation with cache of delegates.

from lambdaparser.

some1one avatar some1one commented on May 27, 2024

So, I am using this for some configurable formula evaluations. I can always make a class that inherits from LambdaParser but it would be nice if this was already included. I want two things out of this; 1) To have a cached compiled delegate returned and 2) to have a dictionary of Func (value getters) so values could be dynamically retrieved upon invocation.

I might have gotten confused about the reason for this issue but here is a sample idea of how this could be done. A cached of value getter results may be useful too if something is static but that would take some thought as it kind of conflicts with this. This is also not very useful if caching is turned off.

public Func<object> CreateDelegate(string expr, IDictionary<string, Func<object>> valueGetters)
{
    return () => Eval(expr, valueGetters);
}

public object Eval(string expr, IDictionary<string, Func<object>> vars) {
    return Eval(expr, (varName) => {
        Func<object> val = null;
	vars.TryGetValue(varName, out val);
        return val?.Invoke();
    });
}

from lambdaparser.

some1one avatar some1one commented on May 27, 2024

Okay, I understand now. I don't need that kind of performance for this project, but the custom filtering use case has already come up on another project that I am considering this for now. I'll have to do some benchmark s to see if will actually be a problem though.

And for the variable caching, yes, that makes a lot more sense when I think about it that way.

from lambdaparser.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.