A blog about my rantings, including Games, Game Development, Gaming, Consoles, PC Gaming, Role Playing Games, People, Gaming tips & cheats, Game Programming and a plethora of other stuff.
Monday, 10 April 2023
Disgusted by the BBC (Taiwan & Ukraine)
Wednesday, 29 March 2023
A mini-break in C++ Optimization (Pt 1)
I have had one of those evenings, in reviewing some code for a friend I came across a repeated pattern where he would iterate over collections, strings and other linear items in a loop and mutate them. We're of course talking C++ here, and he had this whole myriad of loops all over sanitizing, processing, event summing values.
I asked whether he had access to the STL he immediately got a bit uppity, he and I have never seen eye to eye about using the STL.
I advocate it be used, even just standing something up which you go back and make more performant or more memory efficient later, just stand up your project, don't get bogged down in details of the best string, or the best vector.
He however stands by his having "long tested examples I use all the time, just ignore them"...
No.
Simply put his examples, used in a smattering of programs, peer reviewed very infrequently by a miniscule handful of people is not good enough, use the STL, it is seen by literally millions of engineers, it is very portable and well adopted, use it to your advantage.
We sparred a little about this, but then I simply asked whether he had any parallelism in his algorithms?
Nope, of course STL can just use them.... You can provide the execution policy.
Lets take one of his examples, a simple function to give him the time & date as a character array.
{
time_t now = time(0);
char* buffer = new char[64];
memset(buffer, 0, 64);
ctime_s(buffer, 63, &now);
for (int i = 0; i < 64; ++i)
{
if (buffer[i] == '\n') buffer[i] = 0;
}
return buffer;
}
This returns a character array buffer raw pointer.... No, he interjected, it returns a string. A raw pointer to memory is NOT a string folks, this is where my argument on that point begins and ends. This code is very unsafe, I don't like it and I certainly insist it can easily leak memory, for whom is going to delete that buffer?
So I erase that buffer with an actual standard string:
{
time_t now = time(0);
std::string buffer(64, 0);
ctime_s(buffer.data(), 63, &now);
for (int i = 0; i < 64; ++i)
{
if (buffer[i] == '\n') buffer[i] = 0;
}
return buffer;
}
he didn't like this, but accepted perhaps that was better, he argued about the string constructor taking a length and a character (zero) to initialize itself with, but went with it. It's safer, simpler, easier to read code.
I'm still not happy, I don't like that loop:
{
time_t now = time(0);
std::string buffer(64, 0);
ctime_s(buffer.data(), 63, &now);
std::transform(
buffer.begin(), buffer.end(), buffer.begin(),
[](const char character) -> char
{
if (character == '\n')
{
return 0;
}
else
{
return character;
}});
return buffer;
}
He hated this, and I have to be honest I don't like the lambda either, but this is correct, using the transform algorithm and the lambda could be expressed in a location it could be reused, or just as a free function somewhere, it doesn't have to be inline here making a mess of reading the code.
What however is it doing? It changes any new line to a zero in the buffer string, why? Well ctime_s puts a new line at the end, so if we reverse the iteration we may get a much better performance we can early out of the loop. But we can even just do away with the whole transform, we know it's the last character so just set it to zero as an optimization and simplification. My friend can get behind this now:
{
time_t now = time(0);
std::string buffer(64, 0);
ctime_s(buffer.data(), 63, &now);
buffer[std::strlen(buffer.data())-1] = 0;
return buffer;
}
We can even further think about this code, what will strlen do? It might count from 0 to N to find the first zero character as the value, could we therefore count backwards from the 64th position in the buffer? Well, we know the format of the data and time we get is known:
Wed Mar 29 23:36:01 2023
Twenty five characters with the new line, adding another for the null termination character, so we can always just set the twenty fourth to zero and reduce the buffer size at the same time to perfectly fit!
{
time_t now = time(0);
std::string buffer(26, 0);
ctime_s(buffer.data(), 26, &now);
buffer[24] = 0;
return buffer;
}
Tuesday, 28 February 2023
Will I ever Buy a Laptop Again?
2. Upgradable Drives/Storage
3. Changeable Battery
For point 2, I was again able to get deliver with a single 500gb mechanical hard drive, and for merely £65 I was able to get a really nice M.2 nVME SSD and install that the moment it arrived. I am still able to replace the standard 2.5" SSD SATA drive if I want. And I was also able to install a 1TB MicroSD card for more cold storage of large files when on the move and disconnected from my file server.
For point 3, being a Lenovo is a huge boon, there are a lot of resellers of their components and the battery is no exception, plus it's a battery just installed with a sticky pad and a single 4 pin connector, very simple to open the chassis and swap it out. After four years mine is due a change, it has gone from around 9 hours on a full charge to only 4.
I did look briefly at modular options, such as the ones touted recently by Linus Sebastian, but getting them here in the UK is a massive pain in the rear.
So simply put will I ever buy a laptop again? I don't know.
Or even my laptop purchase from 2012, http://megalomaniacbore.blogspot.com/2012/01/custom-laptop-for-virtual-deeds.html
Sunday, 19 February 2023
Home Game Engine : Physics Debugging Tool Progress
I also have flash backs of delivering data from a server on a provisioned box, where you pay by the megabyte per monthly usage (and I honestly don't know why I couldn't host a service like this server publically on my massive 500mbit home fiber, which has no cap on data, it's all I can eat) but practically, I'd not like to host this here, except for debug, and if I were to make a game, it'd need to be on an AWS instance or something.
Anyway, that's all very much future stuff, my next problems are all about the game, improving and cleaning up my game object authoring, improving my model making skills (I might actually have to double down and actually learn Blender) and the working out a few issues I know exist in my control scheme.
Saturday, 18 February 2023
Programming the Thought Triangle
You, the interrupter are pretty much responsible for costing me a complete mental problem space unload and a complete mental space reload.
That is very stressful for a programmer, it's a huge amount of mental gymnastics, which is tiring, difficult and above all unnecessary.
If you have a child at home who shows any interest in programming, and they're busy doing it, or "playing" on the computer, but there's no actual game showing, just let them, leave them be.
Tuesday, 7 February 2023
The Old Lady and the Server
No, this isn't some strange nursery rhym (though I wonder if it could be) no this is a story of a literal old lady whom I believe was trying to collect as many notches on her bed post as possible...
Let's start at the beginning, I lived in a house I rented, I had a spare bedroom and my younger brother broke up with his long term girlfriend. He came to live with me, quite unsuccessfully, as his life was very chaotic, and indeed in the end I threw him out in the end.
But in the nine months he inflicted himself upon me he had a few lovers through his door. One of them was said "old lady".
I say old she was maybe early to mid-forties, with children, but that was old, my brother was nineteen... heck I was only twenty two.
He was going through a series of different jobs at the time, and somehow he met this woman, who promised him a bunch of things; like a car, and MR2 if I call correctly. Which he went on about constantly.
These rewards never appeared, but one day she did, one morning to be precise, I came down into my own kitchen and there was this woman in my non-smoking house smoking. If looks could have killed she would have murdered me when I asked her to go outside to smoke. Until she realised this was my house; so the stories clearly went both ways between them and my brother.
Well, when my brother broke things off with her she continued to communicate with me, I'm not even sure how she knew my contact details to be honest, I think she had a nominal business address or something, in fact thinking back now, I think one of the mysterious things she promised my brother as she bedded him was to get me a job; despite my not asking.
But it seems she was looking for a NOC engineer.
Indeed, I spoke to her briefly where she was talking to me about setting up BSD servers, "My servers run BSD". She never explained what her servers were doing (I suspected porn).
She did however offer me a job, at £4,000 a month. That's £48,000 a year. At the time I was in a £21,500 job doing network maintenance (which I hated, and have commented about in earlier posts).
