Chapter 287 - 165: Forget It (Part 2)
Chen Zhuo paused, then added, "Vector-sparse matrix multiplication has a time complexity of O(N). Even if someone has ten thousand connections, a computer can perform this kind of one-dimensional array multiplication in less than a millisecond."
In the internet cafe, Chu Ge had already bitten the candy clean off the stick.
The hard plastic hit his teeth, and he snapped back to reality, flinging the stick to the ground.
His eyes were glued to the instant noodle lid stained with red chili oil in front of him.
On it were written two somewhat messy lines of formulas.
The database deadlock had plagued the Capital City startup for two full weeks, a problem several top students from Qinghua’s Department of Computer Science had deemed an unsolvable hardware bottleneck.
Yet, coming from this twelve-year-old boy, a few basic linear algebra concepts had just smashed it to smithereens.
"I get it."
Chu Ge’s voice was trembling slightly.
"Ditch the relational queries. Pull all the relation tables out and maintain a sparse matrix in memory. Run all queries as vector multiplications in memory. Once you get the result, you just use the user ID to pull the avatar and name from the database."
"Right," Chen Zhuo replied.
"But there’s a problem."
Chu Ge’s programmer mindset quickly found a flaw.
"A knows B, B knows C, and C knows A. That’s a cycle in graph theory. If the vectors keep multiplying, won’t it create an infinite loop and burn out all the computing power?"
"Chu Ge."
Chen Zhuo sighed on the other end of the line, a hint of exasperation in his tone.
"This is an engineering problem, not a mathematical proof. In the results from the vector multiplication, for any value greater than 0, just use a hash table to record the ID. The next time you multiply, if you encounter an ID you’ve already recorded, just skip it."
Chen Zhuo gently shook his head.
"You’re a coder. Don’t expect a mathematical formula to calculate how many squares of toilet paper you need to wipe your ass. Use some of your programmer’s common sense, would you?"
Hearing the teasing tone over the phone, Chu Ge suddenly broke into a wide grin.
"Alright. I owe you one. This time, it’s on me."
Chu Ge folded the noodle lid twice and carefully tucked it into his pocket.
"When I get back to Keda after the New Year, I’ll treat you to a month’s worth of stir-fry from the cafeteria. I’m hanging up now. I need to hurry up and write this low-level memory daemon."
A dial tone buzzed from the receiver before Chen Zhuo could even reply.
Chen Zhuo put down the phone, shook his head, a slight smile on his lips.
He turned and walked to the kitchen to get a bowl and chopsticks, ready to eat dinner.
「...」
Over a thousand kilometers from Zeyang City, in the Capital City.
Inside a dilapidated residential building near Zhongguan Village.
The room was dark, lit only by the pale glow of three or four monitors. Outside, the occasional flash of brilliant fireworks illuminated a floor littered with cigarette butts and leftover takeout containers.
Wang Xing was wrapped in a thick cotton jacket, slumped in his computer chair. His eyes were bloodshot, glued to the backend log on the screen.
They wanted to build a true real-name campus social networking site in this wild west era of the internet. The idea was brilliant. Wang Xing and his partners had even managed to secure an angel investment of several hundred thousand with just a PowerPoint presentation.
But a technical bottleneck had them dead in the water.
Tomorrow was the third day of the Lunar New Year, and their investor was demanding a system demo that could support five hundred users online simultaneously.
Wang Xing hadn’t slept in three days.
As soon as concurrency rose, the database queries for the core "friend of a friend" feature would crash the entire system like an avalanche.
The MSN icon in the bottom-right corner of the screen suddenly started blinking.
Wang Xing’s bloodshot eyes twitched. He grabbed the mouse and clicked it open.
It was Chu Ge, the freakishly talented hacker from Keda, all the way in Huizhou, whom he’d roped in as free technical support.
"Old Chu, save your energy."
Wang Xing typed a line and sent it, his fingers a little stiff.
"We tried adding a cache, tried building an index. Nothing works. A relational database can’t handle this kind of multi-level network query. The hardware can’t take it."
A few seconds later, Chu Ge sent a compressed file.
It was less than 20KB.
Immediately after, a message from Chu Ge popped up.
"Delete all your previous relational query code. Run this C-language daemon on the server’s backend."
Wang Xing froze for a moment.
"What is this?"
"A memory-level sparse matrix multiplication tool."
Chu Ge’s reply was simple.
"Just export your relation table as a plain text file and feed it to this. From now on, for all friend queries, don’t go through the database. Just send a command to this process. It’ll do the calculations in memory and return an array of friend IDs to you."
Wang Xing stared at the message, completely baffled.
But he was at his wit’s end. It was a long shot, but he had nothing to lose.
He accepted the file, unzipped it, and dragged the source code into his compiler for a quick look.
The moment he saw it, a cold sweat broke out on Wang Xing’s back.
The code was extremely concise. There were no library files for database connections, only low-level memory pointer operations and a few one-dimensional array loop multiplications that he couldn’t quite understand.
There wasn’t a single SQL statement inside.
Wang Xing took a deep breath. He uploaded the compiled executable to the test server’s backend, then fed it tens of thousands of lines of test relational data.
The process started silently, using less than fifty megabytes of memory.
"Run a stress test."
Wang Xing turned his head and shouted to Wang Huiwen, who was slumped over next to him, half-asleep.
Wang Huiwen groggily rubbed his eyes and typed a stress test command into the keyboard.
"Simulating five hundred concurrent requests, three-degrees-of-separation friend query."