“Seven years! GTA 5 Online is still so slow to load??”
On Reddit, Steam, HackerNews, countless players complained…
Enter the game for as little as 5 or 6 minutes, and as long as 20 minutes.
Finally, a hacker couldn’t bear it. He checked the running situation one by one with the anti-compiler editor, and finally found the reason.
It turns out that the code written by R Star (game developer RockStar) is too inefficient. When loading, an if statement loops 1.98 billion times….
Who takes up a lot of time?
How slow is it to load GTA 5 Online?
In a survey initiated by Reddit related groups, more than 80% of players have to wait for more than 3 minutes, and some even exceed 15 minutes.
Moreover, from the online launch of 7 years ago to today, the situation has not improved at all.
If you have a bad temper, you’ve already started swearing…
But the strange thing is that if you choose the story mode (stand-alone version), the loading will be much faster, and it even feels like a game developed by two different studios.
Take this hacker’s example, his own hardware specifications are as follows:
The CPU is the old and tough AMD FX-8350. It was launched in 2012. It adopts the “Bulldozer” architecture and has amazing overclocking potential. The graphics card is still GTX 1070.
This looks like an old configuration today, it takes 1 minute and 10 seconds to open the stand-alone version of GTA 5, and it takes 6 minutes to load the online version.
The hacker used the simplest Windows Task Manager to determine which computer resources were used when the online version of GTA 5 was started.
On the 1-minute time boundary, the basic content common to the stand-alone and online version was loaded before, and the content unique to the online version was then loaded.
It can be seen that the online version of GTA 5 calls a lot of CPU resources for at least 4 minutes when loading.
At the same time, the usage of memory, GPU, and hard disk has hardly changed significantly. Therefore, there is a high probability that the problem lies in the code.
R star code is too bad to write!
Before opening the R Star code, the hacker said:
I smell bad code…
In order to find out which part of the program stuck to the CPU, he used a toolLuke Stackwalker, To sample and analyze the CPU task stack.
For closed-source applications, Luke Stackwalker can dump the stack of the running process, and the position of the current instruction pointer, and build a call tree at a certain time interval.
Finally, by integrating the data, the program running statistics can be obtained.
From the results, there are two functions that “stuck” the CPU:
So he used a professional code disassembly tool to give GTA 5 a “open mouth”.Go down the call stack and find that the problem lies in onesscanfFunctionally.
The function of sscanf is to read the data in the formatted string. In GTA 5, it is reading a JSON file of about 10M with more than 63,000 entries.
What exactly is this file for?The dude speculated that this might beRelated content of the in-game purchase store.
In the specific operation, sscanf reads each character one by one for each valid value, and then returns the result. After that, the pointer moves to the next value, and it loops back and forth…until it scans all the 10M files.
Looking at the second question again, this is a storage command, the object isitem, What exactly is unknown.
But before saving, there is an if statement that compares the hash values of the items in item one by one to check whether they appear in a certain list.
According to his calculations, this step if, to be executed (63000^2+63000)/2 = 1984531500Times!
That’s right, GTA 5 used your CPU to execute 1.98 billion if commands in the ten minutes or so before waiting for loading.
Such a simple and rude programming idea makes this old man dumbfounded:
Since the object has a unique hash function, why not use ithash map???
(Hashmap stores data according to the hashCode value. In most cases, its value can be located directly, so it has a fast access speed, but the traversal sequence is uncertain.)
As for why this is the case, some netizens speculated that at the beginning, the number of if loops was not so many, but with the development, the number of entries continued to increase, and finally reached the point where it was hard to return.
No one wants to move the previous code structure.
In this way, 1.98 billion ifs were staged over and over again on player CPUs all over the world…..
Problem solved, saving 70% loading time
As for the first question, the hacker uses the hook method, instead of reading the strings one by one, but:
hook strlen
The start and current length of the “cache” string.
If the function is called here in the string range, the cached value is returned. As for the question of if statement, it is even more straightforward-skip repeated checks completely and insert items using hash map, because these values are unique.
The final result is as follows:
Now, the loading time of the GTA 5Online version has dropped from the original 6 minutes to the current 1 minute and 50 seconds! Moreover, the hardware specifications used are still seven or eight years ago.
Here, you should manually @R星: Have you lost your studies?
The hacker did not leave any identity information in the article, nor did he disclose the anti-compilation tool used, but he did not leave a name for good deeds, uploaded the packaged tool to Github, and the player can use a single line of code. download:
git clone —recurse-submodules https://github.com/tostercx/GTA O_Booster_PoC
After that, copy the dll file to the game root directory and it’s OK!
– .