Friday, October 10, 2014

Using Assembly For Scalable Optimizations

I've been learning Assembly for Intel x86 processors. I'm hoping as an end result that I will be able to use inline Assembly in C/C++ in order to speed up some processing. I find it critical to optimize clock cycles during certain computations in order to provide faster response times between client and server.

Let's assume this (without regards to hardware): One clock cycle is 1/8,000,000 of a second. 1,000 milliseconds are in a second. Therefore one clock cycle is 1/8,000 of a millisecond.

If we were to have a million clients on one server and they requested a computation that required around 150 clock cycles. That would mean a total of 150,000,000 clock cycles have to be performed. Let's also note that the server can handle this request one at a time because of necessary synchronization.

Each request requires a total of 0.00001875 seconds.
In order to serve every client its request, it would take 18.75 seconds to complete.

And we can always vertically scale our hardware to increase the amount of clock cycles per second.

When players play an online game, they expect to have a latency of around 150 ms and lower. This sort of latency would not be acceptable.

If we were able to cut down the clock cycles of 150 to an amount of 50, then it would only require 6.25 seconds to complete. If we were to be able to cut it down to an amount of 25, it would then require only 3.125 seconds to finish entirely.

With careful multithreading of certain I/O tasks and inline Assembly, we can see a huge difference in performance if done correctly. Finding ways to reduce O(n) to O(1) is also beneficial, however, it may run at the cost of memory.

Wednesday, December 18, 2013

In the past 2 nights..

Here's my progress on SoftServer in the past two nights.
  • 1st night: 802 lines of code.
  • 1st day: 532 lines of code.
  • 2nd night: 691 lines of code.

All of the code compiled and ran as expected. As of now I haven't ate really anything in around 10 hours, most of the time just trying to shove it down. I'd say I lost about 2-4 lbs one the course of these night and days -- but honestly I don't care enough to check.

Normally, you'd get tired and just do something else. However, you just feel like staying there until you've finished what you've set out to do. You stay there for hours and hours, time goes by quick when all of your focus is on that one goal. Hell, you don't even care about time anymore (the sun rising is wonderful evidence.)

It gets you stuck. Sometimes it gets you hours on cleaning. The reason why you get stuck is because you feel like you have a mission -- no matter how long it takes, you have to complete it. 

Now all I want to do is get on the computer. Start up that Debian on VirtualBox and hammer away on Code::Blocks.

The educational experience you can get from it is priceless: I learned a little bit of Template Meta-programming because I had a problem with static-typed parameters, I learned a little bit of std::map and the Standard Template Library because I wanted to organize my data better, and I rewrote two separate server applications just because I found out a more efficient way in handling and using packets.

I've actually been working on my general server software project SoftServer, the first beta build looked great. Then I pushed that build to GitHub and started another empty build again (and I don't know why). I have a bit of a compulsion to rewrite applications.

It was great the second time around, to recreate the architecture once more despite it being that you've already went through the process -- you can carefully re-walk your steps this time around (and make your code more pretty).

Everything has art in it. Even the most logical thing you could ever do, programming. There are many different ways I can design and build my server's architecture. Some ways more efficient than others. Other ways are not as great.

I used to "serialize" (I wouldn't even call it serialization) packets by separating them with semicolons. This was horrible practice, I knew it was -- but that's what you get for cutting fast corners in lieu of quality.

            My First Packet Idea: "JOIN_QUEUE;2;"
The integer after the first semicolon was to denote the queue's difficulty. The semicolons, themselves, are there for easy tokenization of the packet. I could separate JOIN_QUEUE and 2 with one simple function -- then use them as I please. 

Of course, it didn't go so well when I removed the semicolons (nothing to tokenize, except spaces). And it didn't help my collaborative partner much either when he had questions about sockets.

He had decided to tag along for this journey into networking with my project: User Datagram Transfer Protocol, UDTP. We were developing a simple scientific control, file transfer protocol using TCP, to compare with our own. When suddenly, during testing, bits and pieces of the packets were leaking to the next and the next.

After sitting for thirty minutes with both of us mumbling at the screen -- we had stumbled upon the Discovery Channel featuring a special program on these ravenous sockets...
                #define PACKET_SIZE 420
They were just reading way too much! Reading two or three packets as one whole packet. Ultimately, too fast for our sequential yet precise send() then read() architecture. To even further discourage our interest and belittle our knowledge in networking, Google had no shown results about this problem.

2 weeks later, we had met up in class again. Class was at 6:30pm.  I've forgotten to eat the entire day. He said that he had discovered something that would prove a remedy to our extreme disappointment.

I had told him I wasn't cognitive enough to comprehend anything. I was excited, of course, but the hunger was too much to bear. He suggested we go visit, "The Taco Truck." After one expensive taco, I reverted back to my normal self within half a minute or so.

He had gotten a piece of paper out since that's how he usually explains things. Telling me about structures and typecasting to character pointers (char*). Scribbling down so quick, I wasn't sure at first what he was saying. Until I realized where he aiming at -- he had solved the problem.

It had really made my day. I complimented him a countless number of times, sitting in the chairs of the ghetto outdoor dining scenery provided by yours truly, "The Taco Truck."  Such a small and silly thing to be happy about but that's what being a scientist (computer) is all about. 

He had told me that he was going to send me the source code on his experiment tomorrow. I couldn't wait though, and rushed home to test out the experiment myself.
1.  struct UDTP_Packet{
         PacketType type;
         char message[50];

                                      2.   send(socket, (char*)&sendPacket, sizeof(UDTP_Packet));

          3. UDTP_Packet readPacket;


                                      4. read(socket, (char*)&readPacket, sizeof(UDTP_Packet));

         5. readPacket.type;        readPacket.message;

Pink is the server. Red is the client. Don't you see how beautiful that is? Let me number it out for you.

  1. Sockets knows exactly what size to read and send because both the client and server have the same UDTP_Packet struct using sizeof();
  2. Being that send() and read() will only accept character pointers (char*) as a buffer argument. We can actually convert (typecasting) the UDTP_Packet struct to a character pointer (char*).
  3. After sending the raw data, and receiving character pointer (char*) -- we can reassemble it back on the client's side by converting it back to the same UDTP_Packet struct.
  4. You can actually access the members of readPacket with the dot member access operator. It "magically" went through the sockets of hell unharmed.

The world of networking and server architecture is confusing at first with having to play the role of two people (client and server). However, it is extremely rewarding and actually gets you thinking about the efficiency / scalability / optimization with all of your programs. Just think about it, you're actually making a server.

And no this is not Linux Server Administration, you are building it from the ground up with nothing but some experience and a little bit of interest.

Sunday, December 8, 2013

Bought an external 23" inch monitor


Acer 23" inch monitor
Sale price: $129.99
Regular price: $179.99
Lucky's savings: Priceless (...$50 bucks)

This is by far the most useful thing I have ever bought in my entire electronic consuming career. Well other than the computer itself, this is the second most useful thing. I didn't think a monitor would increase my productivity, I've always thought it was me -- that I could never get motivated to do anything. When this baby came, I was proven wrong. This increased my productivity by ten-fold. 

Coming from a 13" inch Macbook Pro monitor, 23" was seriously bigger than I had imagined it to be. The week before I order this monitor, I would go scout and size compare other monitors in Best Buy or Staples. They ranged from 19" to 27", and for some reason they looked small. It was only until I got my monitor and sat in front of it that I realized -- perspective is important. You really won't know how big a monitor is until you sit in front of one. 

23" inches is perfect for me. I wouldn't go any higher. Though, I'd probably get another monitor and set up that dual monitor cream dream.

Buy a monitor! You won't regret it. If you are on the fence on buying one, just hit that buy button. You won't regret it, buy it.

Friday, October 25, 2013


I love Debian. Actually, I just love installing new Operating Systems in general.

It feels good to set up everything over again. Plus you have a new look so it's even better.

Looks better than Ubuntu! Never going back.

Tuesday, August 6, 2013

The Warts

Dr. Scholl's.

I infamously know of you by the tens of boxes lying around the dinner table. It is not to say that I hate you or anything, Dr. You've helped her enough. But they just keep coming back.

I have given her hope. Hope is always good for people.

I laugh, treating it like a joke -- though as a mechanism to keep her smiling. "This isn't the right one.. it's for feet, mom!"

She agreed. But I always knew there was that lingering thought in the back of her head that nothing would work.

That was why she agreed without hesitation.

I went to the store with her. We looked through aisles and aisles for something that damn Dr. Scholl's (I'm not hating you).

"It's not here," She said hopelessly. Looking so thoroughly.

I couldn't stand to see her like that. Her eyes just spoke to me with such hopelessness.
It was a blank stare.

I had to give her hope again, I just had to! I grabbed the store brand wart remover and showed it to her.

"It will work," That piece of hope I keep pounding into her head.

But damn, this world. It is the cause of her burning desire to remove those numerous and disgusting warts on her hand.

...I shouldn't say disgusting, though. Because it puts me on the same level of you guys -- that is the root of her mind-consuming need to remove them.

But I cannot also say that they are not disgusting. I admit, I have refused food prepared by her naked and infectious hands.

"I'm not hungry," I would always say and then make myself dinner later that night. Is that hurting her? Is it killing her slowly?

The eyes of strangers and the rejection of her own home-cooked meals by her own son.

Why has she abstained from relationships that could bring the color back into her life drained by the passing of her late husband?

Mom, please. Don't be sad. Don't worry. I accept your warts. I accept it wholeheartedly. I won't rewash the dishes in secrecy! I will eat your cooking! I love you!

Please, just live a little more! Don't be afraid, they may not be seen in approval by others but they will now by me!

She grew several more in return of getting rid of several more.

Though, it keeps me just a bit happy when she runs into my room and shows me that she got rid of one.  I just know that there's still that lingering thought in her head.

Sunday, June 30, 2013

The Tumble

"Why would they do this to her?" The young boy cried alongside his even younger sister staring through the plexiglass in full spectacular display of their mother tumbling around in an enormous machine.

It's been 5 hours since she's been locked inside. The machine's whirring echoed through the chambers. She's never been the one to handle motion sickness, much less be excited on an airplane.

Her cheeks were stained with vomit, enough time to achieve a crusty-like feel. Her tears made pathways through it that seem sort of like rivers side by side amongst puke.

The sight of her kids kept her strong but that wasn't enough to hold back the food they've eaten at Denny's not much more eight hours ago.

"I don't think I can handle much more," she sounded almost as though not aware of where she was. The kids cried even harder as they heard her voice reverberate once more through the loud speaker.

What kind of sick and beautiful sight is this? For kids to see such a strong figure, their single mother, tumble like a piece of meat like something akin to mere clothing and a washer and regurgitate such nasty looking byproducts of the stomach unwillingly and helplessly.

The machine finally stopped, the painful sound of circulation no longer there. But it was too late as the mother lifelessly lied there surrounded by the unrecognizable mac and cheese her kids asked her to try and what looked like blood mixed with brown unidentifiable chunks.

Saturday, June 29, 2013

Know The Passing

The moment a person reaches the age of 75 -- their loved ones and family take them to the grave site where they shall be laid to rest for all of eternity.


She sits comfortably upon the grassy felt of her everlasting bed in a beautifully pure white dress. Surrounded by those important to her: her sisters and brothers, her son along with his wife-in-law and their children.

They all smile at her and she at them. For three days, she will endure no eating and in turn the weakening of her body as all of her organs devour each other as a source of food.

It's a happy event. An event ridden of sadness and melancholy. Why?

Knowing and expecting the death of a loved one, for there will be no shock of death like a sudden cancerous tumor or a highway accident. This is where they will spend all their time and attention on her.

Laughter and reminiscents fade away into night. The living prepare to leave for home and freshen up for the next day of celebration.

"Don't go just yet, okay? Make it all the way. I want to see you tommorrow, Nana." One of her grandchildren spoke to her, hugging tightly.

"You betcha I will, kiddo." She pat him on the head.

Car doors shutting, ignitions starting. She watches them leave tranquilly. The absence of sun could not hinder her white glow. "It was time," she thought to herself as she relived the time of her husband's death.

They were all happy, not a single tear was shed for him because of those three special days. "He made it too.. and so will I," she confidently proclaimed of herself surrounded by hundreds of decomposed bodies who've even done so.

The family came back the next day to find her white gown stained with blood, her body lied there lifeless. The grandchildren surmount to tears, hugging their granny without any fear of the sight of crimson red. The adults stood there in shock.

It was something that she didn't want to happen because it would mean turmoil and years of mourning for them. She didn't make it the three days.

She had not died in peace, she had died because of something out of their control -- that was why tears were shed.

And there sat a casket unburied, while others were six feet under. It was against customary traditions to bury someone who had not served their three days. It was to show the unprecedented pain that this person's passing has caused.