Why is the reflection of language relatively slow, and what is the meaning of reflection? Why is C + + not reflective?

Source: Internet
Author: User
1.php,java,c# have reflections, but many tutorials say that reflection is slow, so why is reflection slower?
2. Since he is slow why do these languages provide reflection, is reflection necessary for these languages?
3. Why C + + does not reflect, C + + is Object-oriented AH

Reply content:

It's not usually the reflection slow, but the code that uses reflection does not do certain optimizations ...

A slow reflection may be the inverse optimization of the call, or the design of the language and reflection API, so that the reflection call must add some extra work such as re-discharging the exception of the unboxing parameter.

Even in the same language, the speed of reflection is also related to implementation. For example, runtime object metadata is preserved, and the object structure is largely not optimized, and the reflection is as fast as the normal method. Now the compile time through improved program analysis, you can also optimize some of the reflection code as fast as the normal code level.

Say "reflection" even C can do, for example __typeof__ can compile period to get a variable type, no __typeof__ some macros you just can't write ... And Dlsym can be seen as "runtime reflection" of C. C + + can also use template specificity to obtain some types of information at compile time. As for what to use, the code is much more often think about how Don ' t Repeat yourself to understand. When I first learned. NET, I always wondered ... What are you doing with the goods? The key book to cite examples of inexplicable attribute incredibly is to add comments to the code ... Snake Essence is sick (or maybe I don't understand this layer at the moment)
Then I recently in the WebForm MVC (for years it feels like the MVC tide is going to keep up with the tide) and then I'm down a mvcmusicstore looking here. The first model layer directly adds a [Required] attribute to the attribute ... And then I was thinking about the gutter and the gutter. The labor force is still hard to write various checks on the ASPX page!
And then I found out that this was only MVC, and then I didn't, so I made myself a set of controls. directly bind the field (in fact, basically, http://ASP ) with the set of controls added a BindTo property)---You see, it's going to be reflective.-----The crud Logic of the page doesn't have much to do with it. And now that I have the bindto attribute, then I can get the [Required] feature directly, so I loop through every control that has a BindTo property when the page is initialized. and see if there's a required feature--------you look here or reflect .--------then group A dictionary into the JSON to the front end, the front end to write a JS to verify the----here is the use of JS embedded in the DLL-----and then I completely farewell to the egg-bound field code and validation code

Reflection slow what, in fact, is wrong. You compare him with who you are, you can't do something without reflection, so when you do something, you have to use reflection. Of course, when you do a=b+c this kind of thing, reflection is slow, which is the same as the difference between interpreting and JIT.


In other word, reflection is actually used. I www.gaclib.net use C + +, to create a set of reflection, not reflected a lot of declarative programming method can not imperative programming language do ah, It's all about creating a DSL. You have to DSL, but also DSL and the original language structure can interact with it, is not to ask your language has a certain dynamic, reflection at this time is useful ...


As for C + +, this is his father's taste problem.

The earliest computers were programmed in their native assembly language , which were inherently reflectiveAs these original architectures could be programmed by defining instructions as data and using self-modifying code . As programming moved to higher-level languages such as C, this reflective ability disappeared(Outside of malware ) until programming languages with reflection built into their type systems appearedThe problem is that people who use javac# to do projects know the benefits of reflection, especially the loading of the project's framework. Reflection can not generalize performance, and some places reflect quickly, in some places, performance is not a problem, such as the loading of frame modules, usually only reflect and then load once, of course, hot-swappable modules are dynamically loaded.
It's very nice to have a virtual machine or a platform to run, such as a native byte of C + +, to provide a runtime type.
Also, object-oriented is object-oriented and reflection does not belong to object-oriented tough.
Finally say, CPP has CPP, reflect what away from the point. I'm only talking about the meaning of reflection in the programming language I understand.

You are at work, go to lie down outhouse come back to find a note on your desk, it says "XX leader let you go to find him a trip, immediately!" ”。 At this point, you will be running to find the leader.

All right. This scene, "XX leader let you go to find him a trip, immediately!" "is a statement instruction, and you" run nonstop to find the leader "is the execution of this statement instruction.

Because you are human, you are a high-level animal, you have the ability to introspect. That is, you are running to find the leader on the way or after, you will know: I saw the note on the "XX leader let you go to find him a trip, immediately!" "I just go further, and you'll know it's at work time, so the leader is looking for you to have work on things to ask you, and so on and so on, which means : You know what you're doing.

The question is: Does the computer program know what it is doing after executing a series of statement instructions? Does he know what he's doing? Reflectionis to try to provide one such ability at the language level: let the code be self-reflective and let the code know what it's doing ., although the current implementation is still very elementary and shallow. Some implementations do not use reflection, the program is not beautiful and does not automatically write.
I see some Java projects, a action-mappings configuration at the sky,
Then look at the latest features of struts do not need to configure this time (there may be wrong, contact Java not long), I cried silently there is no!

In many places, the use of reflection, you can achieve a very elegant implementation, and just a few short lines of code, you know!

Application scenario, there is one, permission assignment. According to the module, controller, method for granular permissions configuration.
You need to use reflection to know which controllers and methods are available.

Of course, I'm talking about PHP, and it's a little bit more reflective! For example, in the database there is a data table with several fields, we have a class in the code, there are several members.
If our requirement is to write an instance of that class in the code to a data table, there is a requirement that the value of which member of the class is written to which field in the data table.

This requirement can be achieved without reflection, but it is clumsy, inefficient and inflexible. If the language has a reflection grammar, then the implementation of this requirement can be done with a simple and flexible (write code efficiency) efficient.

As for the performance of reflection .... This is not a thing, if you think the reflection performance is not good, then do not use reflection just.
In my practice, repeated measurements of the performance of a block of high-frequency use of the reflected code, how to measure the code block time is 0-1ms (measurement accuracy is also 1ms). So at least in my application scenario (the response time is 0.6-1.2 s), the performance cost of reflection can be negligible.

Finally, one more cliché: don't think about optimizations (and performance issues) prematurely, because the performance bottlenecks that you guess are often not real system performance bottlenecks when you don't make the whole system. Have you ever heard of data-driven
With reflection will be happy many language reflection why relatively slow, reflecting this is more added to a piece of things, plus certainly later than not add before slow (at least not faster, if not used).

Reflection is a means of programming, reflective language built into this ability, non-reflective language can generally be self-realization reflection.
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.