How to read the source code? What is the point of learning the source code?

Many people think that people with the ability to read code are awesome, and they have tried reading themselves, but they find that when they fall in, they will always lose their way but eventually get nowhere. They don’t know how to read the code correctly. Some of the experience of reading the source code, I hope it will be helpful to everyone. The content is organized on the Internet and deleted.

Source Know: https://zhuanlan.zhihu.com/p/38341879

Recently, a friend suddenly asked me a question "Why do you see the UE4 engine code so deeply?"

After seeing the problem, I was stunned, because this is the first time someone has tagged me "Deep UE4". In fact, I have been in contact with Unreal Engine for only two years. Just relying on the accumulation of usual study habits, I wrote some related technical articles.

But this incident made me suddenly realize that the learning habits that are most easily overlooked by me may have some value and significance. I just want to share my summary of my experience on engine learning, but I never thought about sharing my learning methods, perhaps the latter is more important.

Everyone has his own style and habits. When you find that a person around you is excellent, you go to see how he spends his 24 hours, and then compare your 24 hours, the answer is very clear. In the same way, if you find it difficult to learn the source code, you might as well ask the more advanced "people" and take a look at the process of others learning source code modules. Of course, to be specific, there are many dimensions, details, and prerequisites that affect a thing. It may not work to copy other people's methods. For example, others can learn for 10 hours a day, which is almost impossible for some people. Everyone understands these principles, and I will not elaborate on them.

Returning to the topic, since the title is "How to learn the source code of a large project", I will share my ideas and process of learning Unreal Engine source code (C++) with you.

The Unreal Engine source code is about a few million lines (no exact statistics, you can refer to the screenshot of the pure code plus static library folder below), and it can be traced back to the 3D game independently developed by Epic in 1998-Unreal. For a game engine that provides such a complete function, it can be imagined that his code is quite complicated. Therefore, at the beginning of the study, you must be clear that your goal should not be to read all of his source code from beginning to end, but to sort out the content of a specific module after determining the learning goal. .

Insert picture description here

UE4 pure source code folder information here is a summary of the simplified version, and then I will further elaborate on each item.

Preparation: It is recommended to prepare a large and continuous time (the small time is easy to interrupt the sorting of the relationship), an IDE or tool that is more convenient to find (VS, Notepad++, UltraEdit, Source Insight, etc.. Some friends in the comment area recommend the Source Insight effect Similar to VS+Visual AssistX), class diagram tools (staruml, Edraw, etc.)

Learning steps (simplified version):
1. Decide the module to learn, find official documents, related summary articles, sort out the general learning content and goals
2. Run the program, observe the performance
3. Run the source code, breakpoint debugging, and follow from the beginning On the side of the execution process of the source code, pay attention to the function stack
. 4. Draw class diagrams and flowcharts, and first record the important classes encountered to indicate the relationship between each class.
5. Record problems, and record the classes or content that you don’t understand in a problematic way down
6. write articles, notes, to try to resolve the remaining issues one by one before

2-6 It may be necessary to continue to repeat the learning steps (detailed version):
1. Find official documents and related summary articles

For example, if I want to study the network module, I first go to the official documents, forums, and wikis to go through all the network-related content. At this time, solve the problems that I don’t understand as much as possible. If I can’t solve them, write down the problems and go to the official documents first. I think it is very necessary because the article here is the most authoritative and the error rate is very low. Then, go to Google and Baidu to search for related articles and posts. At the same time, you can join some technical QQ groups (some water groups decisively retreat and keep some high-quality communication groups), and go through these articles and materials. At present, the relatively good technology websites that can be seen are generally the official websites (forums) corresponding to each technology, StackOverflow, Zhihu, Blog Garden, Jianshu, CSDN, some personal websites, etc. Of course, some websites have serious copy-and-paste phenomena and require their own filter. It is recommended that if you can find the link to the original text, go to the original text as much as possible, because you may see more excellent articles from the original author.

2. When running the program, we need to adjust various parameters to perform different situations, and then observe its performance effects to verify our conjectures and conclusions.
For example, whether the properties of a dormant Actor can be synchronized normally, if the client Whether the end attribute is the same as the server, whether it will execute the callback function, etc. Executing the program can quickly get a conclusion, and then we can analyze it more quickly and accurately based on the conclusion. In order to improve efficiency, it is best to set different configurations, GM, etc. at the beginning to dynamically change the running content when the project is running. Because large projects are generally compiled languages, we may need to frequently modify the code to compile and re-run.

3. Debugging can be said to be the most critical step. 80% of the details require you to understand
when the function is executed (function breakpoint) during debugging ? What is the value of each attribute at each step? When does the attribute value change (conditional breakpoint, memory breakpoint)? What is a complete execution flow (function stack)? These problems require you to track and debug bit by bit, and finally solve them.

4. Many people may not have the habit of drawing pictures, but I personally think this is an essential link, because it can greatly improve the speed of your understanding of the framework.
For any complex project, each module will involve a lot of Classes (excluding pure C projects), the relationship between classes is intricate, and various design patterns may be used in order to reduce coupling. These greatly increase the difficulty of reading the code. It is very likely that you will be completely after reading 3 or 4 classes. Confused what they do. For example, when I was looking at the UE4 attribute synchronization module, I was completely confused by the relationship between the various classes, so I saw a class and took a picture of its class, and when I saw a related class, I recorded their relationship. , And finally got the following simplified class diagram. After sorting out, I can summarize the relationship between them in a few words.
Of course, in addition to class diagrams, there are also flowcharts, sequence diagrams, and even "module relationship diagrams" that you invent for convenience. At this time, the type and specification of diagrams are not that important, as long as they can help you understand.
Insert picture description here
5. There are two kinds of recorded problems: problems that others give you and problems that you give yourself problems that others give you:
you can’t be smooth sailing when you are reading, you may have encountered countless in the first step problem. Don't worry at this time, and solve them in order of importance. Simple problems can be searched directly online. If you can't solve them, you can find the gods around you and the great online guys to solve them. If it still doesn't work, just write down the problem and continue to study. When you get to a certain level, your problem may not be solved. Questions for yourself: After you solve the problems others have given you, you should already understand more than half of them. However, it is impossible for anyone to write an article to cover all of them. You need to dig out more and deeper questions by yourself, and then answer them by yourself, so that you can learn more than others and reflect your own value. .

6. There is a difference between writing an article and writing notes, but they are both meaningful
. Compared with the above steps, I think writing an article is not necessary. This is suitable for those who pursue perfection and squeeze time. What you write is for others to see, so your goal is to explain it clearly to others. When I write an article, I will consider whether this is true, whether I am sure, and whether the consideration is comprehensive. Under this kind of self-torture, I will try my best to improve my knowledge system and revise the content that seems inaccurate in the article. If you can make it clear to the readers at the end, then you really will.

Writing notes is much easier than writing articles. I just record the content of my summary and the learning gains, without having to consider too much. Although not as perfect as writing articles, we may also think a lot more in this process, and quickly recall our learning experience in the subsequent learning process.

Finally, I would like to emphasize that if you just want to use a tool well, you don't need to fully understand everything, because your time is limited. If you really study the source code details with a learning attitude, please be prepared to spend a lot of time and stick to it.

For source code learning:

1. Nginx source code
1. Nginx infrastructure
2. HTTP architecture
3. Inter-process communication mechanism
4. Nginx advanced data structure
5. slab shared memory
6. Up stream mechanism design

2. Redis source code
1. Redis storage system principle
2. Data model and key value mapping
3. Memory operation and disk synchronization
4. Master-slave synchronization, atomic operation
5. Analysis of the underlying IO implementation

3. Skynet source code
1. Network module realization and data sharing
2. Timer realization
3. Actor realization and cluster cluster
4. Lua auxiliary library realization
5. Load balancing and hot update solution 6. C
service development and docking client
7. skynet tricks

Four, zeroMQ source code
1. Message model release push/push-pull model
2. Communication protocol inproc/ipc/tcp/pgm
3. Performance analysis and classic MQ comparison
4.
Low-level network communication implementation mechanism5. Zmq system architecture and implementation principle

# Source eBook:

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_28581269/article/details/112655332