More Effective C ++ Terms 15

zhaozj2021-02-08  453

Terms 15: Understand the system overhead of abnormal processing

In order to process an abnormality at runtime, the program is to record a lot of information. Regardless of the way, the program must be able to identify if an exception is thrown here, the program must be released, the program must know each entry point to exit from the TRY block; for each TRY block, they It must be tracked with its associated Catch clause and the type of exception capable of capturing these CATCH clauses. This record is not available. Make sure the program meets the comparison of the exceptional specification (Runtime Comparisons), and does not have additional overhead when an exception is thrown to release the associated object and match the correct CATCH sentence. But the abnormality handle is indeed a price, even if you don't use TRY, Throw, or catch keywords, you can also pay some costs.

Let us first talk about the price of you don't use any exception handling features. You need a space to establish a data structure to track whether objects are fully constructed (Participate Terms 10), and you also need to keep these data structures constantly updated. These overheads are generally not very large, but programs that are compiled with unusually compiled methods are generally smaller than those who support exceptions.

In theory, you can't choose this: C compiler must support exception, that is, when you don't have an abnormality, you can't let compiler producers eliminate this overhead because the program is generally generated by multiple independent generations. The target file is composed, and only one target file does not perform an exception handling does not represent other target files without exception processing. And even if the target files that make up the executable file do not perform an exception handling, then what they are connected? If any part of the program uses an exception, the other part must also support an exception. Otherwise it is impossible to provide the correct exception handling in the runtime program.

However, this is just theory. In fact, most of the compiler producers that support abnormalities allow you to freely control whether it is in the generated code contains content. If you know any part of your program, you don't use Try, Throw or Catch, and you also know that the connected library does not use Try, Throw or Catch, you can compile the method that does not support exception processing, this can Reduce the size of the program and increase the speed, otherwise you have to pay for a unwanted feature. Over time, the library that uses the use of different trends began to become general. The above method will gradually be used, but according to the current software development situation, if you have decided not to use any abnormal characteristics, then use Method of compilers do not support an exception is a reasonable way for performance optimization. Similarly, it is also a good way to avoid an exception, which guarantees an exception that does not pass the process sequence from the client program, but in doing so, it will also hinder the client program redefined library. The virtual function declared is not allowed to have a callback function defined on the client.

The second overhead using an exception processed comes from the TRY block, whenever you use it, that is, whenever you want to capture an exception, then you have to pay for this. Different compilers implement the method of try blocks, so the compiler is different from the overhead between the compiler. Roughly estimated, if you use the TRY block, the size of the code will increase by 5% -10% and the running speed is also slower. This is also assumed that the program has not thrown an exception, and I discuss it here is just the overhead of the TRY block in the program. To reduce overhead, you should avoid using useless TRY blocks.

The code generated by the compiler is as much as the code generated by the TRY block, so an exception specification generally spends the same system overhead as TYR blocks. what? You said that you think the exception specification is just a specification, do you think they don't produce code? So well, now you should have new met. Now we have come to the core part of the problem and see how to throw an exception. In fact, we don't have to care too much about this problem, because the exception is rare, and the incident is often described as Exceptional (unusual, rare). 80-20 Rules (see Terms 16) Tell us that this incident does not have much impact on the performance of the entire program. But I know that you are still curious to know how much overhead will be thrown out, the answer is that this may be relatively large. Compared to a normal function, return to the exquisite level by throwing an exception. This overhead is very large. But only when you throw an exception, there will be this overhead, usually not. But if you use an exception to represent a more common condition, such as complete the traversal of the data structure or end a loop, you must re-consider.

But please wait, you ask me how do you know these things? If the support exception is a newer feature for most compilers, if different compiler exception methods are different, how can I say that the program's size will increase by 5% -10%, its speed is also Slow with the same proportion, and if there is a lot of abnormalities being thrown, the program runs slowly slowed down? The answer is amazing: some rumors and some benchmarks (see Terms 23). Truth is that most people include there is little experience in exception handling, so, although we know that the abnormality does have to bring overhead, it is difficult to predict the exact amount of overhead.

The cautious method is to understand the overhead described in this Terfement, but do not depart with specific quantities. (Ie, a certain number of uncertain translators), regardless of the overhead of abnormalities, we have to stick to the principle that must only be paid. In order to minimize your abnormal overhead, as long as you do not support an abnormal method compiler, use the TRY block and anomalous specification to limit the place you really need them, and only if it is exception (Exceptional) Those throw an exception. If you still have problems in performance, overall your software to determine whether an abnormal support is a function of a role. If so, consider selecting other compilers to have higher efficiency compilers in C abnormal processing.

转载请注明原文地址:https://www.9cbs.com/read-965.html

New Post(0)