Learning to use, rather than learning to write STL and boost

Source: Internet
Author: User

Library is an important part of language. The knowledge and mastery of any language and development platform is inseparable from the skillful use of libraries. We can say that the C language has CRT and posix api, Java has j2se/j2-j2ee, C # has. NET Framework, and Python also has its own library.

For most languages, the use of standard libraries attached to languages is simply a matter of course. However, the problems that such a person knows are very serious in C ++. Not to mention boost as a quasi-standard, even the standard STL will be rejected by countless people on the grounds of "inefficiency.

In a previous post, I had a long debate over this issue with others. Of course, it was definitely not enough. The use of the C ++ standard library has become a matter of belief and philosophy. This is also a wonderful thing in computer language.

Here, I will post valuable and representative messages in this post, and I will share them with you here.

------------------------------------------

W:

(As a game engine) Are you hesitant to write it yourself or use STL and boost?

G:

They are all self-written and do not need any other.

Qj:

I still use a lot of STL without using DLL export.
However, boost is generally not used. Although boost is excellent, most of its content is "toys of experts" or "tools for fashion people to show off ", or the feeling of "connecting to what is the cause" means that the remaining practical items are either included in the standard library, there are alternatives, or there is no need. In this case, I don't want to add such a large dependency to my project ......

GMM:

My engine is used in large quantities. Most people are afraid of what they don't know.

Qj:

Generally, STL can be used. However, if you write core code that is sensitive to performance, do not use STL. STL will do unnecessary bounds check to affect performance, but the most important thing is memory allocation, in many cases, special memory splitters are required, but the STL memory splitters fail to be designed. The memory splitters of containers are actually bound to the class type.
As for boost, there is no need to use it. Some libraries are syntactic sugar, such as lambda, functional, and smart PTR, but the writing method is simpler, but the cost is to increase the Compilation Time, in addition, due to the limitations of C ++ itself, some of the statements are still awkward, and it is better to directly use other more advanced languages. Boost tries to introduce many FPL features into C ++, simply because it cannot be used as a reverse dog. Of course, some libraries do some practical work, such as ASIO, but in my opinion, the overall design of these libraries is actually quite bad, it is estimated that the authors have spent their thoughts on how to accumulate skills and forget how to design a program architecture.

3222:

I have a deep understanding of the engine! Because STL is a public library, it has to be considered in many cases. STL of its own engine is only for its own, and the speed and performance are much faster. It also helps to manage the memory allocated by itself.

SSSa:

Some STL implementations do have cross-border checks, such as STL port. But do I remember this is only true under debug?
The memory adapter does need to implement one by itself.
I don't think there is anything wrong with STL. If I find the bottleneck on STL, I think it is likely that it is not used correctly or that there is a mistake in design, I don't think writing a container by myself can greatly improve the performance.
It may occur that I only need a very simple function of a container, but STL provides more functions than needed, and I have to pay for the extra functions, but even in this case, I think it can be improved from other STL usage.

Smile:

We write it all by ourselves. Of course, with macros, we can easily convert it into STL. the experiment found that the self-compiled EXE is more than 20 KB smaller than the STL. we have added a large number of assert in our template library, such as the array [] operator. c_str ()....... MAP is implemented by hashing. at the same time, we also made our own memory management. The biggest difference between our vector is that it is not increased by multiples, but by the size of actually allocated memory. for example, if I need 4 bytes, the memory manager actually allocates 16 bytes, and the vector capacity increases to 16 ....... currently, the results are quite good.

Mxh:

How many people strictly test and release the STL/Boost library? How can it be worse than the robust written by their heads?
I usually see performance problems caused by STL/boost, which are either poorly designed or not usable.

LDD:

A bit. The growth of vector multiples is the most efficient growth method actually proved. If you need to save memory, it can compact to the actual memory usage. In addition, hash_map and unordered_map currently have STL.

GMM:

Summary:

"I don't want to add such a large dependency to my project"
Boost is composed of many basically independent sub-databases, not all of which have to be put in. I don't know where it is.

"STL will do unnecessary bounds check to affect performance"
STL won't, VC's STL implementation will, and can be turned off, especially for VC10, will not be checked under release.

"But the most important thing is the memory allocation. A special memory distributor is required in many cases, but the STL memory distributor design is very unsuccessful. The Container memory distributor is actually bound to the type, it's really too simple. "
The stlport method is two layers of Allocator, one layer is type-independent, and the above layer is type-related. The Standard specifies the type correlation. If it is not equal to, there cannot be a type-independent. As long as the interface is the same as Allocator, you can write a type-independent interface.

"Some libraries are syntactic sugar, such as lambda, functional, and smart PTR"
Functional, is smart PTR a syntactic sugar?

"We have added a large number of assert in our template library, such as the array [] operator"
The STL of vc8 + also exists, and macros can be used to control whether to perform checks.

"Map is implemented by hash instead"
MAP is useful for map, hash is useful for hash, not a replacement relationship

"At the same time, we also made our own memory management. The biggest difference between our vector is that it is not increased in multiples"
First, vector may not necessarily increase exponentially, but also in other ways. The second step is to reserve a certain amount of space to reduce the number of new/delete operations, otherwise it is unnecessary for people to do so.

Qj:

"STL will do unnecessary bounds check to affect performance"
STL won't, VC's STL implementation will, and can be turned off, especially for VC10, will not be checked under release.

Bounds check is related to STL implementation and can be disabled in VC. I used to think that STL would not perform bounds check in release. It was not until I decompiled the code and checked the source code that I found that there was a bounds check in STL. Of course, this is not a big problem.

"But the most important thing is the memory allocation. A special memory distributor is required in many cases, but the STL memory distributor design is very unsuccessful. The Container memory distributor is actually bound to the type, it's really too simple. "
The stlport method is two layers of Allocator, one layer is type-independent, and the above layer is type-related. The Standard specifies the type correlation. If it is not equal to, there cannot be a type-independent. As long as the interface is the same as Allocator, you can write a type-independent interface.

The layer-2 Allocator does not help. What I need is to specify a Allocator for a container instance. For example, if I use a list <int> container, I want each element to be allocated with the Allocator specified by me. This Allocator can directly open a temporary space on the stack, when the function returns, it is released at one time without the need for another release. Like this:
Allocator alloc = Allocator (_ alloca (10000), 10000 );
List <int> L = List <int> (alloc );

"Some libraries are syntactic sugar, such as lambda, functional, and smart PTR"
Functional, is smart PTR a syntactic sugar?

Using Native pointers without smart PTR will not affect program functions, but only write a few more lines to release code. The same is true for functional. This is the syntactic sugar.

Me:

Let's take a look at the constructor of the container...
Also, smart_ptr is the syntax sugar... Well, I am completely speechless.

GMM:

"Bounds check is related to STL implementation and can be switched off in VC. I used to think that STL would not perform bounds check in release. It was not until I decompiled the code and checked the source code that I found that there was a bounds check in STL. Of course, this is not a big problem. "
You are referring to vc9. Vc8 and 9 I will define _ scl_secure = 0 in release to turn check off. By default, VC10 does not check release.

"I want each element to be allocated with the Allocator I specified"
Of course this can be done. It can always be done.

"Using native pointers without smart PTR will not affect program functions, but write a few more lines to release code"
What does syntactic sugar mean? I believe you have to check it out. Your understanding of the three words is incorrect. When reference counting is called syntactic sugar, what else does it mean?

Me:

Not to add new features to the language. However, there is no way to implement it at the database level without changing the basic assumptions of the language, the syntax form and syntax elements of the main methodology and development paradigm, which are generally called syntactic sugar. Smart_ptr does not improve the syntax, but focuses on the functional focus of reference counting. Therefore, it belongs to the library level and cannot be called a syntactic sugar.

Qj:

Additional points:
1. List can be used to specify the distributor in the constructor, but the distributor type must be included in the type definition. This is what I said before that the STL container type is bound to the distributor.
2. smart_ptr and reference count are two different things. Com is also counted by reference, but it is not specified that com should be accessed using smart_ptr. Otherwise, the smart_ptr must be referenced for counting.
3. Speaking of these things is syntactic sugar, because these things are syntactic sugar in other languages. They do not provide functions but simply make the writing more concise, boost provides these syntactic sugar in the form of libraries. Without the sugar implemented by boost, C alone can also achieve these functions, but it is only a bit difficult to write. If you think that what is implemented using libraries is not called syntactic sugar, I have no idea, but they are essentially a class of things.

XJ:

It would be nice if STL/Boost could be as unified as the. NET Framework... When boost is used, it feels like the code style has changed, just as it is using another language.

Me:

STL and boost are basically consistent.
Actually, STL and boost are different from developer and user philosophy.
For boost developers, the emphasis is on code readability and efficiency, and on metaprogramming and programming skills.

For users, boost and STL are divided into four styles.
The first style is the Lib style, mainly providing functions. Such as the pool and graph, as well as the smart_pointer and ASIO that have been arguing upstairs. The method used for this type of style is typical dual-phase. The first stage is type-specific, and the second stage is based on the reference of the compiler/runtime interface. In STL and boost, most libraries adopt this style. This is also the most easy to use and the most frequently used style.
The second style is syntactic sugar. For each and so on all belong to this category. This type of library is usually used as-is.
The third style is the expansion of paradigm and methodology, that is, simulating other programming paradigms and methodologies in C ++. For example, spirit, lambda, and Proto. Strictly speaking, boost. MPL can also belong to this type. The usage of this type of library is divided into two steps. The first step is to customize the dialect, and the second step is to use the dialect.
The fourth type is metaprogramming. Use templates and macros for Compiler derivation to expand code and select compilation. Typical examples include boost. PP, STL/Boost. type_traits, and enable_if. This part can be used by general users.

The so-called boost and STL styles are not uniform, because STL only contains the first type, while boost contains the styles of all four types of libraries.
In fact, the boost and STL styles are almost identical for libraries in the Lib format. In addition, the so-called issue of long Compilation Time does not exist in libraries of the Lib series. The so-called boost is difficult to use, but the database is not classified. In addition, boost emphasizes that interfaces are user-friendly. It's just the databases at different levels. The friendliness and friendliness are different.

As for. net, do you think the reflection of. Net can be consistent with that of XML? Obviously, it is impossible. But the multi-paradigm design of c ++ has intensified this problem.

Qj:

I am too lazy to talk about anything.
What I just want to talk about is that the basic design idea of boost is incorrect, that is, to pursue simplicity and require versatility, and to maintain no performance loss, the combination of such multiple targets creates a four-character monster like boost. As Linus criticized C ++, designing is really important. In general, most of the design objectives are not orthogonal. For example, to achieve performance, we need to sacrifice some versatility. In order to be reliable, we need to reduce the complexity of some systems. Therefore, the focus of design is to choose between them, based on the design objectives, make a correct choice on versatility, performance, system complexity, and other factors. Many boost libraries are designed to meet a variety of design goals at the same time. The cost is to greatly increase the complexity of the system, and the system becomes bloated, in addition, he attempted to use programming techniques to cover up design defects, and only a bunch of bad designs were produced. In addition, the boost technique is superior to the design style. It has poisoned many inexperienced C ++ programmers and made them indulge in constructing fancy Template Techniques, I forgot the original purpose of programming.

Me:

I do not know what else to say upstairs.
Obviously, skills are used by experts. The so-called "poisoned" is only because the imitation is not ready, and it cannot blame the Language designers and database authors. As I have mentioned earlier, it is obvious that you use Lambda and spirit to discuss the complexity of smart PTR as a stupid and out-of-date behavior. Do you think the design of the smart pointer pool is bloated? Do you think any's design is redundant? Is the unordered map implementation of tr1 provided by boost "poor? So, sorry, even the C function library, cannot meet your requirements.
Library Design considerations are a trade-off, No matter what database is. Even a library like CRT. Malloc has many problems, so there will be third-party libraries such as tcmalloc. If you like it, you can use assembler to compile a set of things you want.
But do you think this is a reality?

PoC:

Wahaha, tianchao's technical staff's old problem-prove their skill by trying to hit others.
Yes, everything needs to be dialectically viewed, but it is no difference between the light-based dialectical and not the materialism.
In fact, I am a C-controller. I hope to design a number of loose coupling modules using C as the interface as the underlying layer, and then use the scripting language or advanced language with GC for upper-layer logic. I advocate the use of C ++ in the form of C, but it does not reject the features of C ++. Of course, it is still not necessary to fully understand a thing, before that, I did not dare to comment.

Qj:

I don't have to be arrogant. There are technical competitions everywhere. The debate on a technology or language on foreign forums is also endless. The debate is only about the form rather than the purpose, through arguments, we can deepen our understanding of some things and hear different voices. This is what we should gain from the arguments.
Regarding boost's smart PTR, I don't think his design is a good design. Boost's smart PTR design is an ambitious design, attempts to provide a package of solutions to provide all possible smart PTR features to users, more than the smart PTR in Loki in the early years. The problem is that it significantly increases the learning cost of users. users need to consider too many factors and are prone to errors. For example, the single-threaded cmd_ptr is used in a multi-threaded environment. Smart PTR is also frequently used in my projects. I usually implement a simple smart PTR by myself. It is designed only for the features I need and can be done in less than 100 lines of code.

GMM:

"For example, use the single-threaded cmd_ptr in a multi-threaded Environment"
Shared_ptr is of atomic, regardless of the single or multiple objects.

--------------------------------------------------

The purpose of putting this passage here is not to say who is right or who is wrong, but to clarify different people's different understandings of STL and boost. Of course, some of them are debatable, and some others, especially Qj, have many errors. GMM and I corrected some errors in the reply. In addition, some errors may be discovered only after you have a deep understanding of the template and STL code.

My point is very clear. There are too many misunderstandings and prejudices in the C ++ community in China for boost and STL. In particular, boost, because the library design skills are extremely complex, but the user interface is very easy to use. Many people equate the difficulty of using the boost library with the difficulty of design, resulting in fear.

I always insist that for most people,Learning to use STL and boost is self-evident, and it is more important than learning STL and boost design and building skills.

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.