Giter VIP home page Giter VIP logo

Comments (7)

antonmedv avatar antonmedv commented on August 11, 2024

Awesome! Can you create PR for this? If you also can provide a benchmark, it will be really interesting to see the performance increase.

from expr.

deanm avatar deanm commented on August 11, 2024

PR #54

Only a ~10000x performance improvement on my contrived example : )

from expr.

deanm avatar deanm commented on August 11, 2024

I actually realized this problem is a general pattern in the runtime code. There is a difference between a reflect.Value and putting that value into an interface, a reflect.Value is a way of accessing the pre-existing memory, where putting something into an interface is going to make a copy.

So another example in the code, if you have an embedded struct:

type Foo struct {
  Bar struct {
    Field int
  }
}

When you do 'Bar.Field', fetch will return a copy of Bar, not a pointer to the original Bar. So if we do something like my previous example and stick a big chunk of memory in the Bar structure, it's going to value copy all of Bar. Now that can be pretty easily fixed in the case of above (when Run() was called with an environment as a pointer), because then Bar's value will be addressable (CanAddr() is true) and you could create an interface of (type, *Bar) instead of (type, Bar) by returning value.Addr() instead of value. But I didn't look how it would work for other types, I guess slices are fine since the slice itself can be value copied (and what the slice points to isn't copy), but for fixed sized arrays, etc, I imagine the code now will value copy the entire array for an index operation like 'a[0]'. The additional problem is CanAddr() will be false if you passed a value to Run() instead of a pointer, but at least this is something callers can easily control.

Basically I feel like it should be a principle of expr (for a few important reasons) to be zero-copy of the environment (when a caller calls Run() with a value and not a pointer, they create a copy at their callsite of course, but that doesn't really have anything to do with expr). Right now it seems like expr is copying all over the place.

from expr.

deanm avatar deanm commented on August 11, 2024

I tried making a patch for this but it's actually a bit harder than it seems, because I thought perhaps I could just return a *struct and a slice instead of an array in fetch(), but then the problem comes to method/functions calls when the arguments are pointers and they expected values, etc, and it seems pretty ugly to try to map that back at call sites, etc. I think the real solution is probably working in reflect.Value's instead of interface{}.

from expr.

deanm avatar deanm commented on August 11, 2024

See #56

from expr.

antonmedv avatar antonmedv commented on August 11, 2024

I think the real solution is probably working in reflect.Value's instead of interface{}.

I also was thinking about it.

from expr.

deanm avatar deanm commented on August 11, 2024

#60

Easiest thing to do without a big change is just allow to opt in to the zero copy behaviour

from expr.

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.