News for hackers. Stuff that matters.
Re:Why is .NET reflection slow? by themihai on Friday December 16, 2016 @06:40PM (#13197914)
I think reflection is "slow" is most languages unless you use a kind of preprocessor. Is there any exception?
Re:Why is .NET reflection slow? by chrisseaton on Friday December 16, 2016 @07:30PM (#13198209)
> I think reflection is "slow" is most languages unless you use a kind of preprocessor. Is there any exception?

Yes! I wrote a PhD on making reflection in Ruby fast. It is possible to make most reflection operations run with no peak time performance overhead at all compared to normal operations.

http://chrisseaton.com/phd/

The main techniques needed are dynamic code compilation with speculation and deoptimisation, polymorphic inline caching, splitting and method inlining. Unfortunately I think the .NET JIT isn't dynamic (might be mistaken, not an expert on .NET) so can't do speculative optimisations.

Basically you see what method names have been used last time in a reflective call, and create a little call that's more like a conventional call using those names. Each time you do the reflective call you check the name against the list of names you have created conventional calls for, and use them if you can. You need to make string comparison fast, which can be done using a rope data structure. Then you need to be able to remove the check entirely if the string comes from somewhere you can control, like a constant. Then you need to inline the reflection method so you are just left with the synthetic conventional call.

So it isn't easy! Which is why it isn't usually done. But it's possible.

In Ruby you need to make it fast no matter how hard it is, as Ruby libraries tend to use reflection (they call it metaprogramming) in inner-loop operations.

Re:Why is .NET reflection slow? by AlphaSite on Friday December 16, 2016 @09:18PM (#13198763)
The JVM does a lot of this, right? I know there is a ton of speculative compilation for C2 and grall but is it done for reflection?
Re:Why is .NET reflection slow? by matthewwarren on Saturday December 17, 2016 @03:09AM (#13199827)
The JVM uses something called 'inflation' to make reflection faster.

See https://blogs.oracle.com/buck/entry/inflation_system_propert... and http://stackoverflow.com/questions/10082523/java-what-is-jit... for more info

Re:Why is .NET reflection slow? by chrisseaton on Saturday December 17, 2016 @12:23AM (#13199378)
I don't think it's done in any JVM as far as I know.
Re:Why is .NET reflection slow? by Locke1689 on Friday December 16, 2016 @07:55PM (#13198317)
> It is possible to make most reflection operations run with no peak time performance overhead at all compared to normal operations.

Hmm... but my discussions with people who have worked on Ruby execution engines say that Ruby's normal operations are slow (because Ruby is constrained to do many slow things for normal invocations). How does Ruby reflection compare to .NET reflection and how does Ruby invocation compare to .NET invocation?

Re:Why is .NET reflection slow? by chrisseaton on Friday December 16, 2016 @08:05PM (#13198396)
I mean that for Ruby it is possible to compile at runtime

    a + b
And

    a.send('+', b)
Into the same thing - machine code like

    add ra, rb, rx
    jo
Except for the jo to detect overflow, this is as good as you will get from any compiler including .NET, Java, C and so on.

Ruby really isn't constrained to do anything slow for normal method invocations - you can optimise I think literally all of them away entirely if you have a JIT. For normal method invocations this all all solved back in the early 1990s in the research on making Smalltalk fast, but wasn't fully applied to Ruby until my work (and Topaz, Ruby on PyPy achieved similar results around the same time actually). I extended it to work with reflection (metaprogramming).

Re:Why is .NET reflection slow? by gravypod on Friday December 16, 2016 @08:54PM (#13198656)
I think that this is still a reminant of the mentality that "interpreted languages are slow" when this is just a lazy way at looking at the problem. This hasn't been the case ever since I was in diapers. I've been going back and looking at old LISP machines and what kind of optimizations they've done to get those running fast. Some of the eairly voyagers were even powered by LISP (these being real-time space probe micro-controller-like systems that are far slower the the slowest ATMega on the market right now).

Everything can be optimized at a compiler or a runtime level. Even if you just scale it back to specific idom-optimizations (as you talk about in one of your talks you've done on your JVM work) you can tremendiously boost speeds by 200 to 300% just by writing operation-specific optimizations.

It's a sad state that because of this preceived "it's going to be slow" that no one seems to be attempting to implement things that Guy Steel and McCarthy implemented way back when. This sort of lazy thinking results in the "Just right it in C" mentality that much of the python community holds.

I'm glad at least you and the JVM people are working on this as this is quite important for all CS-focust disciplines.

Re:Why is .NET reflection slow? by aardvark179 on Friday December 16, 2016 @08:14PM (#13198458)
The project Chris works on has pretty fast normal peak operation, and it's quite possible to get stable reflection in a JVM language to be as fast as a standard method call and inlined in the same way.
Re:Why is .NET reflection slow? by matthewwarren on Saturday December 17, 2016 @03:11AM (#13199834)
> Unfortunately I think the .NET JIT isn't dynamic (might be mistaken, not an expert on .NET) so can't do speculative optimisations.

You're right. The .Net JIT will only compile a method once, so unfortunately it can't do tricks like this

Re:Why is .NET reflection slow? by velox_io on Friday December 16, 2016 @09:04PM (#13198697)
This is really interesting! (It's not often I associate a thesis with cool).

I'll definitely give this a deeper read.

Re:Why is .NET reflection slow? by dyadic on Friday December 16, 2016 @11:59PM (#13199305)
> It's not often I associate a thesis with cool

You should start to do that or, at least, remove the negative bias.

A lot of the cool stuff out there comes from research, whether in academia or commerce, and only reaches the "real world" a long time later.

Re:Why is .NET reflection slow? by camus2 on Friday December 16, 2016 @06:51PM (#13197977)
That's why macros like the Crystal implementation are often the way to go :

https://crystal-lang.org/docs/syntax_and_semantics/macros.ht...

It's a meta language that works at compile time. It's not just text substitution like with GCC, so the syntax can be validated. And no need for "go gen" like silly tricks.

Languages that have reflection should do everything so that using reflection is the exception, not the norm. Some languages which boast about simplicity are often the worst offenders in practice, when it comes to reflection, just look at Go code out there, full of reflection everywhere, because the language is way too simplistic for a lot of use cases. Go even allows adhoc type definition at run time, out of thin air. Is this really what gophers mean by "simplicity" ? because the maintainers refuse to add any new syntax they now basically cram everything they need into the reflect package. Then dare say its use is "not idiomatic" when they abuse reflection themselves ?

Re:Why is .NET reflection slow? by eropple on Friday December 16, 2016 @07:04PM (#13198048)
Crystal's implementation wouldn't work for something like C#, though, because C# supports dynamic assembly loading at runtime and must provide reflective facilities for operating on types loaded from them. (I once wrote an extensibility system[1] and on-the-fly compiler for a game engine that took advantage of this, it was neat.)

Macros don't help you here and limiting yourself to "I can compile the whole blob at once" cuts off a lot of avenues of extensibility.

[1] - https://github.com/eropple/exor

Re:Why is .NET reflection slow? by themihai on Friday December 16, 2016 @07:05PM (#13198055)
>> because the maintainers refuse to add any new syntax they now basically cram everything they need into the reflect package.

I think they want to try different avenues before to decide what features to add. Generators("go generate") are used a lot too but they are a pain to develop. Most of them still use templates or print statements instead of specilised packages such go/ast , go/types, go/parser etc to actually generate/print code. Usually you should use reflection to prototype and switch to generators only where the performance is really a big issue or where reflection can't help(i.e define new named types, import/use arbitrary packages/functions)

Re:Why is .NET reflection slow? by eropple on Friday December 16, 2016 @07:07PM (#13198071)
Alternatively, you build a type system that can express what developers actually need it to express and solve the problem without reflection at all. =)
Re:Why is .NET reflection slow? by themihai on Friday December 16, 2016 @07:18PM (#13198134)
That's easier said than done. There are tradeoffs on expressive type systems too. That being said I wouldn't mind more expressivity in the Go type system. I'm actually looking to implement my own small features(i.e optionals, variant types) in a Go superset.
Re:Why is .NET reflection slow? by camus2 on Friday December 16, 2016 @07:39PM (#13198256)
Where do you think struct tags fit in all this ? do you really think go designers took the time to consider the tradeoffs of such feature ? I don't think so.
Re:Why is .NET reflection slow? by DrJokepu on Friday December 16, 2016 @07:24PM (#13198171)
Any sufficiently advanced type system is indistinguishable from magic.
Re:Why is .NET reflection slow? by camus2 on Friday December 16, 2016 @07:37PM (#13198248)
But putting struct tags in Go was a good idea that promote simplicity ... I prefer the magic of generics or pattern matching or covariance to insane features like this, thank you.
Re:Why is .NET reflection slow? by __old_dude__ on Saturday December 17, 2016 @06:22AM (#13200249)
Java 7 has introduced a very specific API (java.lang.invoke) to be able to inline reflection calls.

So lookups are quite slow, but invocations are fully inlined.

java.lang.invoke API is fast to the point that for doing faster string concatenation in Java 9, Aleksey Shipilev has used this API instead of introducing a new opcode in the JVM [1].

[1] https://youtu.be/wIyeOaitmWM?list=PL2ekzZZrxVUmBXtrwS7pLabUu...

Re:Why is .NET reflection slow? by spullara on Friday December 16, 2016 @07:25PM (#13198178)
The JVM has very fast reflective invocations. Lookup is reasonable.
Re:Why is .NET reflection slow? by matthewwarren on Saturday December 17, 2016 @03:13AM (#13199837)
From what I understand, the JVM uses 'inflation' to make reflection faster. See https://blogs.oracle.com/buck/entry/inflation_system_propert... and http://stackoverflow.com/questions/10082523/java-what-is-jit... for more info