Coder Social home page Coder Social logo

Comments (11)

dadhi avatar dadhi commented on June 12, 2024 1

@weichx Back to looking into it.

from fastexpressioncompiler.

dadhi avatar dadhi commented on June 12, 2024

What do you mean here by ref locals.
Could provide an example/pseudocode?

Regarding memory usage, are you using LightExpressions?

from fastexpressioncompiler.

weichx avatar weichx commented on June 12, 2024

Sure, here is the C# I'd like to emulate

Vector3[] array = new Vector3[100]; // struct btw
for(int i = 0; i < array.Length; i++) {
    ref Vector3 v = ref array[i];
    // do stuff with v and have the array[i] value updated (because its a reference)
    v.x += 12;
    v.Normalize();
      
}

from fastexpressioncompiler.

weichx avatar weichx commented on June 12, 2024

I'm not currently, I'm not that worried about memory usage at expression compile time (which happens once on startup), but I care a lot about this stuff while the game is running

from fastexpressioncompiler.

dadhi avatar dadhi commented on June 12, 2024

Sure, here is the C# I'd like to emulate

Thanks.. So you want to be able to turn this code into expression and compile it?

from fastexpressioncompiler.

weichx avatar weichx commented on June 12, 2024

yes exactly, right now the ref keyword cannot be used with ParameterExpression and I think thats just because the expression tree API got frozen before c# 7 when ref locals were introduced to the language. I would imagine this is possible to implement in IL, just nobody has done it yet. We can't change the Microsoft compiler to support this, but since you generate IL yourself I figured it might be a. possible and b. pretty useful for a lot of people using your library

from fastexpressioncompiler.

dadhi avatar dadhi commented on June 12, 2024

Yes it should be possible, just get the address instead.
But do you need to put all that code into expression or for instance only the body of the loop and convert the ref var to ref parameter?

Vector3[] array = new Vector3[100]; // struct btw
for(int i = 0; i < array.Length; i++) {
    DoStuff(ref array[i]);
}

static void DoStuff(ref Vector3 v)
{
    // do stuff with v and have the array[i] value updated (because its a reference)
    v.x += 12;
    v.Normalize();
}

Then make lambda expression representing the DoStuff with the Parameter(typeof(Vector3).MakeByRef(), "v").

Does this work?

from fastexpressioncompiler.

weichx avatar weichx commented on June 12, 2024

It would need to behave identically to the C# compiler's behavior. The example above is contrived. I'm building a language for people to write game code in that compiles to C# at production time and to IL (via expression trees) during runtime so I can support hot reloading. the syntax is 90% identical to C# to the point where you can basically copy + paste C# code into my script language it will run. Ref locals are the one thing I can't work around in my compiler via lowering and so I'd like to find a way to implement it for real.

from fastexpressioncompiler.

weichx avatar weichx commented on June 12, 2024

Yes it should be possible, just get the address instead. But do you need to put all that code into expression or for instance only the body of the loop and convert the ref var to ref parameter?

Vector3[] array = new Vector3[100]; // struct btw
for(int i = 0; i < array.Length; i++) {
    DoStuff(ref array[i]);
}

static void DoStuff(ref Vector3 v)
{
    // do stuff with v and have the array[i] value updated (because its a reference)
    v.x += 12;
    v.Normalize();
}

Then make lambda expression representing the DoStuff with the Parameter(typeof(Vector3).MakeByRef(), "v").

Does this work?

No, this won't work, since I'm making a scripting language, I don't know the input ahead of time and can't make that transformation work in all cases.

from fastexpressioncompiler.

dadhi avatar dadhi commented on June 12, 2024

@weichx Just updated my prev answer with code.

Ok. I need to experiment with by-ref locals and ref assignments then.
I'll keep you posted.

from fastexpressioncompiler.

weichx avatar weichx commented on June 12, 2024

@weichx Just updated my prev answer with code.

Ok. I need to experiment with by-ref locals and ref assignments then. I'll keep you posted.

Awesome, thanks a lot!

from fastexpressioncompiler.

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.