Monday, 23 October 2023

Job Interview : switch(char) again! Twenty years on!

I've spoken about some of the strange interviews I've enjoyed over the years; notable engineering highlights were having test driven development spooned to me, when I simply didn't know the particular syntax they were demanding, or having StringBuilder suggested for a C# task where they actually needed to cross-core thread their problem, or pointing out that the CI solution being described would lead to an alien environment of folks being paralysed by fear of red flags; only to witness their development floor being paralysed by exactly those issues as I walked out the door.

Today however, I'm going to go way back and talk about one of the very early development interviews I had; this maybe like the third interview I ever had after graduating, it was with a rail rolling stock company, in a very bright and sunny room with three guys who seemed to know their entire universe was correct and they'd take no new information into their sphere of understanding.

I share this one as a friend of mine was in touch over the weekend, he had an interview, and strangely he had the exact same conversation I had... THE EXACT CONVERSATION, twenty something years later.

Let us start by sharing some perfectly valid C++ code:

bool validate(const char& value)
{
switch (value)
{
case 0: return true;
case 'x': return true;
default: return false;
}
return false;
}
int main()
{
const bool isValidOne{ validate('a') };
const bool isValidTwo{ validate('x') };
const bool isValidMe{ validate(12) };
}

Read this code, we should see an output (if we output the bools) of "false", "true" and "false"... I think we can all agree.  This is perfectly valid C++.

It isn't very clean nor pretty, we're relying on the compiler standard decaying char to int and vice versa; which is not very nice and I wish compilers would complain here, that we're comparing int to char in the first case and we're converting the integer 12 to char, I think it should really strong type this.

But that is not the point, the point is that both in my friends case and mine all those years ago the interviewers got really uppity about that "switch" statement.  They insisted that you can not "switch" on a character type...

Just flat insisted.

I remember in my experience they had me write up what they wanted on a blackboard, with chalk, and they wanted me to go through the code line by line; I wrote more or less the above, they insisted they had a character not a byte (uint8_t: though there was no standard for this at the time) and then they went off on this tangent amongst themselves about the value of the if-elseif-else stanza's they insisted would work over my solution above.

Now I was talking to these guys in the early 00's, my friend however was talking to them in 2023.  He jumped on this with the point of "well if the character is known at compile time, yes then constexpr maybe of use" and he showed them:

if constexpr(foo) and if consteval(bar) examples.

He described their faces screwing up and two of the chaps he was talking to staring at each other before making subtle notes on their paper.

It was very clear at this point that the CTO performing the interview was not keeping his knowledge up to date, the group seemed to cluster onto the idea of just maintaining what they have, stagnation like this in our joint experience can be problematic.  Both team morale and high turn over of engineers always happens when things stagnate.

I have to say in my interview I actually sat it out from beginning to end and just knew I was not interested in the role; and I did not hear from them again.

My friend however, he was genuinely concerned he'd done a good interview and they'd be offering him a role, they kept him talking for over an hour and a half!

He's a late 40's engineer of 25+ years experience, just like me; the three chaps interviewing him, including the CTO, were ten or more years his junior, it seemed they were looking to back up their team with an older head to lend legitimacy to things; and they had lost two original engineers of their project to literal retirement without replacing them.

He saw the issues and he thanked them for his time and left, with a negative to whether he would be interested in going further.  This stunned them.

It has to be said, so many interviewers forget that though they are interviewing you, you are also interviewing them like most all human interaction it is a two way street and it's crucially important to remember that in a job interview.  We spent a significant amount of our time at work, if you are not comfortable there or feel you are going to be acting out a role instead of filling one and making it your own.

Thursday, 14 September 2023

C++: I was today years old when I learned this about shared_ptr

I was on a review for a colleague and there was this piece of code which stood out to me, I could not figure out what was being achieved.  Without being specific it was essentially

static std::unique_ptr<B> Create(const std::shared_ptr<A>& referenceToA);

Just the function prototype didn't sit right with me and the use case was even more strange to my eye for the "B" structure here being created has the shared pointer in the members.

struct B
{
    B(const std::shared_ptr<A>& referenceToA)
        : mMyA(referenceToA)
    {
    }

public:
   static std::unique_ptr<B> Create(const std::shared_ptr<A>& referenceToA)
    {
        return std::unique_ptr<B>(new B(referenceToA));
    }
};

private:
    std::shared_ptr<A> mMyA;

And my head just lot it.  To my eye this API, the create function here, is given a constant reference to the shared_ptr and what can be constant about a shared_ptr?  Well the internal reference count, or so I thought.  I believed this was a mistake, I believed the compiler would throw this one out and say "No, you can't change the reference count of this const object".

Never had I ever thought that the shared_ptr is actually referencing some other controlling block elsewhere.  My understanding was therefore flawed and I'm happy to admit naive.

So what would happen here?  Well, the B constructor, actually copies the shared_ptr control block, it therefore does and can increment the reference counter to the shared_ptr.

As counter intuitive as the const is therefore.  We aren't actually saying the shared_ptr itself is constant, rather the reference to it is, we should be reading the parameter type as std::shared_ptr<A>const &  referenceToA.

I felt rather silly for not realising this earlier, not least as I did once write a C program to switch out the qualifiers on a function call to make things like this stand out in formatting my code!!

But it has slipped my mind! Okay! I'm old, shut up.

Here is the full code of what I put together to understand this: 

#include <memory>
#include <mutex>
#include <queue>
#include <string>
#include <cstdio>
struct A
{
A()
:
mIndex(GetNextIndex())
{
}
~A() = default;

const int mIndex;
private:
static int GetNextIndex()
{
static int index{ 0 };
return ++index;
}
};
struct B
{
public:
B(const std::shared_ptr<A>& parent)
: mParent(parent)
{
}
std::shared_ptr<A> mParent;
static std::unique_ptr<B> Create(std::shared_ptr<A>const &  referenceToA)
{
return std::make_unique<B>(referenceToA);
}
~B()
{
mParent.reset();
}
const int& GetParentIndex() const { return mParent->mIndex; }
};
int main()
{
std::shared_ptr<A> original{ std::make_shared<A>() };
printf("Original %i: %i\n", original->mIndex, original.use_count());
auto createdB{ B::Create(original) };
printf("Copy %i: %i\n", createdB->GetParentIndex(), createdB->mParent.use_count());
printf("Original %i: %i\n", original->mIndex, original.use_count());
}

However, I have to say, I still don't like this; clever as it is, there's a hidden copy going on, in the B constructor the copy of the shared_ptr control block and it's reference count incrementing from 1 to 2.

It does quite neatly move ownership of the pointer, but the const and the reference just make my brain spin.  So where this can only gain 10/10 plaudits for C++ smarts, it only gets a mere 3/10 on the maintainability ladder for me, and only when we have the const& together, when written as at the top of the page this drops to 1/10.

Performance is also a factor here, if we wanted better performance we would have to consider whom owns the original A here.  If no-one and it is always added into the shared member in B, well move it... Create once and move it, this code doesn't communicate that as an option, but it certainly could be.



Wednesday, 30 August 2023

tBs - My First Clan

Today I want to regale you with a story of my past gaming glory, before Eve... before WoW... Back in the days of 2002 I joined a clan.... Now, I've read Ready Player One over and over, I do not get this whole "I don't clan" ethos in that writing because back then being in a Clan was the thing.

Guilds and corps in my later gaming life are very different things, this clan was cut throat.  We were

{tBs}

The Butchers

This is where I met my long time co-killer Chaplain (wave, hi chap!) for we were together the scourge of Avalanche.


This is in the Half Life engine mod "Day of Defeat", and it didn't look this good (this is from the later Source version); all important in this game play was the tick rate of the server, most serious clans hosted their own servers and they tweaked them to the max.

I remember our server admin was a chap called Twister, but that's about all, he never much liked any input about the status of the server.  He didn't play very often, so it was always quite curious that us experienced players were not welcomed into giving him feed back; let me tell you as a now professional game developer and server administrator listen to your expert users, they will know when things are off, they may speculate, but they will know and help you spot issues up front they're a resource, use them, never dismiss them.


Anyway tBs were well known, playing on the European Enemy Down ladder we were very often top 10 in Europe, one time top three, playing on again off again matches against the other members of the ladder; we were effectively the second most active clan behind "Scotlands Finest Highlanders" or SFH.

At one time I myself organised our own "Butchers League" and for four weeks in the summer of 2003 I ran the website and helped organise five guest teams to play pre-arranged matches in a knock-out for the win.  SFH won.  Yes, SFH won the tBs League.  That was fine.

What wasn't fine was the reaction of another clan OAP or "Old Aged Players" they kicked off big time, they could not connect their players to the server; they didn't do any preparation up front and get really really angry, really for no reason, when they were disqualified from a match they simply could not raise five players to compete in.  They were offered a different match slot, but they even turned up to that with only four players and a bad attitude.

Within tBs ourselves there were a bunch of interesting characters, but lets talk more about the game.

Day of Defeat

I loved that game, or mod, my favourite map was Avalanche, a wholly unfair map for the allied side (and yes, I admit I enjoyed playing the German side more) but Chaplain and I were demons of the Church.

The Church, pictured above sat on the flank of the map, entered via the smashed bell tower at the top or the rubble strewn wall at the bottom, the actual doors were closed.  Inside you had two side by side, but slightly offset rooms on the ground, then rubble streaming up to a mezzanine level, with then either the option to climb a ladder to the exposed but dominating tower top, or to carry on up and through a wall to the tiled rooves beyond.

As an axis player you began from the top side, coming into the church via the tower.

As an allied player you began on the ground and were forced up through the rubble.

Defending that tunnel route was critical to holding the map, you controlled access to the opposite side for the allied players, you pinned them in their spawn.

Or you held the tower and dominated the center.

It was probably not well thought-out in design terms, but it was such a wonderful piece of level spacing I loved it.

My best memory came playing another clan, we as axis and they as allies.  For some ungodly reason they had a machine gunner run right up into my door way... and it was my door way, I had gone from spawn, swept over the roof into the church and down in sprint fashion time.  This guy thought he could take my church and lay MG fire on my team from my church tower?!  Nope.  A burst of MP40 and he was down, but do was his MG.

Now it was not often ever we took the MG to the church, it wasn't practical in the close-quarters environment SMG was the usual call to arms.  But this day I could not resist, I lay in the rubble pounding their own MG fire down on them.  Their only hope to remove me was to pre-cook a grenade and time it perfectly to explode in my face.  They did not do this; my score was astronomical with chaplain bringing me a reload of ammo and they throwing himself into fire to respawn and bring me yet more.

That was our church on Avalanche.

A few other maps floated our boat, but were were just so good at that map, specialist even that when we played on open public servers we were often accused of cheating, being so good someone is convinced you are cheating is just the ultimate thrill.  I've only managed it through good strong team work.

The other tBs characters though:

Weeman - constantly threatening to self-harm, a disturbed kid to say the least.
Hopper - Cheating chap, he blanked chaplain and I after botting in WoW (and obviously so) undermining any respect we had for this "skills".
Remus - an older guy, interesting fellow to talk to.
Twister - the hard put server admin, who didn't want to know really about our issues.
Dodi - a nice lad who came and went.
Mako - Not the shark he thought he was.
and of course "The Butcher" himself, Butcher being his surname... Always in charge, but never there.

I learned a lot from this, my first clan, I learned about team work, about trusting others and appointing them correctly and appropriately, all from a computer game, backed up with my martial experience in karate I believe I'm quite rounded in letting people both prove and earn their reward and garnering them with praise for a job well done; whilst also being even handed.

All experience for a computer game, and they call is a game; well, isn't life just one big game too?


Monday, 7 August 2023

Amber Valley Planning aren't very good

I think I've come to the conclusion Amber Valley Planning Department are either incompetents' or simply idiots.  Having had to interact with them myself and now in relation to a nearby development, they've demonstrated incompetance which can't easily be explained... Let me explain.

You see a development was proposed, ironically the owners only sought planning permission after the fact, fine whatever get on with it...

So the ground-works and building are complete?... Yes.

The official visited the site?... Yes, there was a woman with a clipboard.

Does the block plan represent the reality on the ground?... No, not even close.

Any school child with a ruler and pencil would be able to very quickly look at two reference points on the ground, extrapolate two lines then compare them to the building work which took place.  They don't match, not even close.

Further more the building work significantly changed the levels of the area in question.  This is not mentioned on the planning permission request at all.

And the newly raised area is several hundred tonnes being held back by concrete posts... No no structural engineering posts, we're talking 4 inch garden posts, with postcrete and concrete weather board.

In the July & August ran we've had they're already bowing ominously.

And I find these two points extremely curious, for you see we were the ire of this department having skimmed 2 inches of soil and spread road stone we had to account for a "Change of level"... yet someone else can dump several hundred weight and build up a new embankment to around 6 feel of altitude?

We presented drawing plans from overhead shots, real shots from Google Earth, showing the true scale and location; it being a photograph after all.  We had to go back and be exacting, that was the order "be exacting" in the block out diagram shown.... Yet someone else can be approximately six feet off level of reality in two axis?

When this goes over, and it is likely to, could it be a danger to life?  Yep.  Will the planning department comment?  Will they heck as like, they just tick their box and waltz off, it is almost criminal in this case.

Either the planning are incompetents', and I strongly suggest they explain their decision; oh but wait, one can not challenge them, only the applicant can appeal?!?
I must therefore air on the side of caution and suggest they're merely idiots, bureaucratic machination maniacs of the paid civil servant ilk just there to tick the boxes and dot the lowercase J's, since the service... Clearly the service is broken.

Want to know the major comment about the works carried out?  That they must ensure the hedge remain for at least 5 years.  This is a hedge in which (at least further up) I found bottles tangled in the roots with a date of 1914 on them, so this is already very much an older hedge... so a) 5 years makes no sense, b) you ignored they changed the level, c) the works do not match the plans provided - and I told you of this! and finally d) they're already subsiding, dangerously so!

Incompetence demonstrated, well done Amber Valley Planning Department, well done.


I am extremely glad my hard earned council tax money no-longer goes to them, indeed I've spoken to my local duty planner, they were highly available very interested in my requirements and helped me out immensely; Amber Valley, not so much.

Saturday, 5 August 2023

Wiley IT Manager Saving on Microsoft Licences

It is late 1998 and I observe one of the smartest PC acquisition steps ever; the company I work for is reequipping every one using a certain application to a new specification machine.  This means a major purchase and roll out for those of us in IT.

A little background first the company I work for has one customer, just one single customer, this may sound crazy but at the time it made perfect sense and the customer was a reassuring British stalwart of the high street the business was rock solid.  They were however also extremely protective of their brand and selling our whole product line exclusively they knew they could tell us how to do everything.

One of the key things they specified was a hard encryption model to their stock and control system software, we had to run the software they provided, we had to run is with "secure" physical dongles performing periodic security authorisation checks and we did all this on a specification of machine they laid down to us.

So it was in 1998 a new version of this software was hoving into view, the specification leapt from a mere 486 running 25hmz to a Pentium III running at or over 120mhz.  The RAM requirements went from 4MB and windows 3.11 for workgroups to the then brand new Windows 98.


So it was my boss (shout out to Dave) set about working out the best platform for this.

He had previously been in charge of the purchase of new server stack from Compaq and with a positive impression he turned to them.

With a little wrangling I believe he had a roll out of 30 machines, with three years support, for £890.  In terms today this is approximately £2000 a seat and was just for the machine a 17" monitor, keyboard, mouse and windows 98.  Nothing else.

Folks had to do other tasks on these machines, not just this customer software, therefore he set about buying Office.  Homogenising the previous smorgasbord array of different spreadsheet and word processing software variously in use.

Adding Office 97 SBE unfortunately pushed the machines up another £80 per seat, this included £19 off for bulk purchase, but it was a crazy price.

But then Dave taught me an extremely valuable lesson, to play the edge cases.

Could you get Office 97 for les than £80?  Yes, you could get it for £49.95 a seat.  But only if it was an upgrade.  Hmm, what could we count as an upgrade from?

Well, it turned out Office 97 could be an upgrade from MS Works 95.  At the time Works was my go to office package, I've never felt the ease and familiarity with office ever again since.  But works was canned by Microsoft; probably because of Wiley IT managers like Dave.

For a full new copy of Works would be had for just £12.95.

Doing a little mathematics, £12.95 + £49.95 is a mere £62.90.

The company was already duty bound to pay me as part of my regular services, so installing Works and installing Word over the top, taking hours to get through all the machine did result in quite a saving.  About £400 for the whole project; meaning Dave was well under budget and everything worked as intended.

It did however leave one literally huge problem; for the next working year our already tiny IT office was overrun with these dozens of double boxes of Works and Office upgrade, just in case Redmond came knocking asking about licenses.

Monday, 31 July 2023

Blog Security Updates

Blog Security Updates....

I have just enabled a forced HTTPS redirect on my blog, unbeknownst to me you could still get to it via an unsecured link!  Thanks google for not making that more obvious.

I've also changed the comment rules to require you to have a google account to comment.

Believe you me I do read every comment, I reply to quite a few.  But for the most part I spent time removing adverts, and I have had some bizarre adverts posted as comments.  However today I had the most in appropriate advert ever for illegal items, which I not only completely disagree with but found abhorrent could be posted anonymously.

So you now need to have a google account.

Thanks for sticking with me!

Wednesday, 26 July 2023

My Worst Development Argument Ever

I have had a really excellently interesting sprint with the work I'm currently doing, like you know one of those sprints where you get a real technical tooth into the pie of problems.

That however is neither an interesting story, nor one I can actually tell.

Instead I will fling my mind back a score of years and we'll discuss THE WORST DEVELOPMENT ARGUMENT I HAVE EVER HAD!

The problem started with the platform I was then working on, it was a quite low-powered single core Celeron PC base, running a favour of Windows for Embedded systems (I think it was Windows 2000 Embedded), but it was basically used as a host for a C# application stack which itself was more or less a wrapper around a C hardware driver talking to the various light, button and money handling mechanisms in the machine.


This system of ours then simply would process launch, at the shell level, another child process which was the actual game content.  And there were lots of different games we could fire up.

Our menu and the hardware polling all would back off, it was actually only polling for button and light IO and every second polling for cash changes from money physically being entered; so that was slow to update, but otherwise it was fine and backed off.

Being a Win32 environment it was fairly easy to back off most all the threading and just launch the child process.

Our testing showed that we had a 92/8% CPU split, the system would take on average 8% CPU whilst in this active child mode; it had a few spikes and it had a few troughs, that included windows itself.  Otherwise it was pretty much that most all the CPU was available to the child process.

It was therefore with some scepticism that we had the development manager in the content area wonder over complaining that one of his guys was having issues getting smooth frame rates from the platform.  We accepted the install and ran the child process, indeed we immediately saw the issue.

A popular UK TV show game, with a target circling the board...


The target would stagger and slew around, this was immediately explained to us that the system was so busy the game was being forced to drop frames, the frame rate icon the game had programmed into it went up and down like a pair of kangaroo's in the mating season.  This was the whole argument, our game drops frames your system takes too much time on the CPU.

It has to be pointed out that our manual actually said we only make 75% of the system time available to the game, our demonstrating we were making over 90% available was well within tolerance, and this is the only game showing this kind of issue.  We were therefore skeptical about the claims being made; especially about the high quality and tested nature of their content executable.

After an amount of head scratching we decided to measure how often the "Present" function was being called in DirectX, a little hackery later and we had a measure... Was Present being called consistently and then the system not presenting, or was the game itself changing the length of time a frame took, staggering when it presented?

Yes, the game itself was almost immediately measured to be staggering how often it called Present.  So the question came up "When you move these items are you interpolating between the position and so smoothing where the target is?  Or are you moving them a fixed distance each frame?"

"The frames are a fixed length"... The developer and their manager said.  We had just measured and shown that the frames were changing length, we couldn't look at their code, so we'd had to hack about but we'd shown their code was calling the present with the same staggering it was not a fixed frame rate as they said it should be.

We video recorded (literally with a camera on a tripod) measured the frames on the camera and calculated the stutter.  Then measured with our test harness when present was being called and saw a direct match, when they called present the present happened.  Our conclusion, the only conclusion really, was the content executable needed to be calling present more consistently; or their DirectX present be set up to sync with the screen or something.

The argument then began, they insisted that their game was presenting at a fixed interval, it was fixed to 30 FPS; they refused to turn on VSync, their platform did not allow this setting to be part of the DirectX initialise... It really should have been.  But we had explained the staggering, the delay in present being called, neither were o do with the system all appeared to be in their code.

"But the system is taking 100% CPU"

Yes at this point three member of my department all pulled this face:



It has to be said the manager in charge of this developer is perhaps the worst development manager I've ever met, a man so inept he alienated and lost developers at a prolific rate, who spent £40,000 on migrating to Perforce rather than paying his team and just using git.  Who seemed to think he could pick up modern development techniques by osmosis.

The developer himself?  Well, lets see how he handled all this evidence from the camera footage....


That's right, he lost his shit.

Despite myself, a coworker and our manager all confirming our observations that the hitching and stuttering were coming from the child process itself seeming to go idle and so it calling present at differing intervals he clearly took this to be a personal attack.  He stood in our development lab and basically tore into our system; our whole team took a verbal berating at this juniors gob and his manager relished it (this was one of the earliest examples I saw of this manager relishing in his minions going to town on others instead of his reeling them in).

Rocked by their insistence and being in the unhappy situation of having to prove our innocence our manager asked for us to be able to review their code.  They refused.  There were some issues, because our company had just conjoined with the place this developer worked with, they still saw us as interlopers.

My colleague was known to be somewhat more volatile than myself, so our manager left me on this horrible issue, trying to figure out what was wrong.

Come the Friday, and a little exacerbated after two days, I started to decompile their executable.  It appeared to me that their main loop was miss-behaving, it seemed to be just calling Sleep with a fixed value on top of the work it had carried out each loop.  And sleep takes a variable amount of time.... It was not taking a check of how long their update took and then sleeping for the difference to meet the frame rate target and then it dawned on me.... Did you spot it too?

They were calling SLEEP.

The Win32 API documentation literally says sleep is not a fixed interval, it relinquishes your thread for the remainder of your time slice up to or more than the sleep time given, returning when the windows scheduler next wishes to make your thread active.

It still says similar today, the time slept is not guaranteed.

This client process main function seemed to be calling sleep with a fixed value each pass, it was hardcoded into the executable.  If you sleep for X+Unknown, you are going to see exactly this staggering.  Our demo child application has a busy wait, it never slept, it yielded by passing a sleep of zero (which make it give up its time slice but remain ready, and it would more or less always be rescheduled before the fixed frame rate next time point, plus we interpolated our animation example).

This child process was just proving more and more to not be to specification.

Their manager insisted it was to spec... Which was very frustrating, as he grandly declared "I have read the code, I know it is correct" and "when we run it, we do not see this issue".

It felt like a fundamental miss-understanding, none of the folks on the team I was within were being treated with any respect, nor acknowledging our collective experience and understanding.

I stewed on this all Friday, come the Monday things were getting fractious.  This game had to be out!  It came up in the master development round up that this game was held up by our system.  This started the direct antagonism.

Everything I did, everything other members of the team did, all showed this content application was just staggering and stuttering on it's own volition, by design, by intent it staggered.  It was not our system!

To prove this I therefore set about writing a harness, which would just give the client process the same DLL to load, but they were all stubbed out calls, and it would run WITHOUT our system.  The content could be tested locally and checked.  We got one of the same Celeron PC's, just a flat install of the OS and double clicked my harness.... Sure enough the game staggered about in the exact same way!

I presented a A4 page example and showed how the loop in our example application worked, explaining that Sleep is not a fixed time interval and a busy loop should be used with a yield not a time span anyway.  I recorded this with my harness and their game, I presented both recordings too.

The developer went apoplectic....


He literally shouted at me that Sleep was guaranteed to come back after that amount of time, and when he ran my DLL shim locally it ran really smoothly.

He was right, it did, but he had a much different dual core machine with 4GB of RAM and a graphics card.  Our platform was a single core Celeron 1GB of RAM and built-in Intel graphics shared vram, in short very different.

At this point the director who sat between my manager and this development manager ordered that I be allowed to look at their code.

The worse development argument I have ever had then hit a peak, as I walked over, flanked by my manager and their manager.  I lent over his shoulder and pointed to the sleep function and said that is not going to be a fixed period.

That was all I said, he never let me explain any further, he just went MENTAL!  He started shouting, screaming, and called me a few choice names.  He would not accept all the evidence that his loop was not a fixed length, that it was changing from frame to frame, he just could not figure out that:

{
    UpdateStuff();
    Render();
    Sleep(33);
}

Was not going to always take a fixed amount of time, first of all I pointed out that doing anything and then sleeping like this will be the time the work takes plus at least 33, and then there was no guarantee that the sleep would immediately come back.  The windows scheduler would decide when you can come back after at minimum that amount of time rounded to the nearest platform tick.

His manager immediately backed him up and agreed with him, they both talked down to me.  They insisted loudly and angrily that sleep was fixed and the functions they have took such a trivial amount of time they were not worth measuring.... 

Even my manager backed me up here, of course doing a function call, any function, will take some amount of time and they need to take that into account.

I had just had these two idiots literally shouting at me, whilst I had to stay so calm, it took an icy handful of minutes for them to accept the argument that 33+N is > 33 where N is none zero.  It was just fundamental and they were not having it.

Their code became:

{
    startTime = Now();
    UpdateStuff();
    Render();
    endTime = Now();
    Sleep(33 - (endTime - startTime));
}

Slightly better, but we still saw hitches and stutters, they were far far less frequent now.

This massive drop in frequency I immediately and without changing my argument pointed me to the sleep, as I said the sleep is not a fixed time, it was not going to sleep for X and come immediately back, that's not how Windows worked.

Their argument was that Sleep was fixed, that it was guaranteed to return after X.

They were very loud, very obnoxious and very adamant.

We returned to the developers desk:  "Show me why you think Sleep is fixed".

I expected him to bring up some code, some harness, some proof of his thinking.  Instead he opened Internet Explorer, went to MSDN and showed me the Sleep function documentation.

Sure enough it said "fixed interval".  He was so smug.  So infuriatingly smug.  His manager was ultra smug too.

I reached down, scrolled the mouse and pointed to the screen.... 



He was reading Sleep in the Windows Mobile SDK.  He's right on Windows Mobile sleep is a fixed interval.  However, we're not on Windows Mobile are we.

My manager looked at the screen, I looked at the screen, they looked at the screen.  And immediately the developer called me a horrible name, yup, just straight up called me a name.

I have to admit I didn't react well, fisty-cuffs didn't happen, though the way he erupted out of his seat raging I expected the guy to swing for me.



He could not take it, his manager still argued he was right, so invested in their mistake were they that they could not admit their miss-understanding.  The manager always claiming he only hired the best minds, this guy being quite arrogant and the whole lot of them generally being very dismissive of both myself and the department to which I belonged.

I walked away with my head up high.  My manager stood and pair programmed with both their manager and their developer for maybe twenty minutes and a new version of the content executable quietly appeared without any stutter; even when we ran obrut!

It was a horrible moment in my time with that employer, I remember how the guy never apologised, that development manager never apologised and the game went out without any further delay, but they never received any censure for the episode.

Our department also never shook this kind of effect either, for some reason because their manager had gone to bat for them from the off every following time a performance issue arose we had to prove everything to the Nth degree without ever seeing the other side doing the same.  Very rarely was it ever truly our issue.

I have never forgotten, I have never forgiven.

When you foul up, just admit it, owning it and learning from it is far more wholesome than being uptight and obtuse.

Friday, 14 July 2023

Publishing Tribulations

Okay so I will start here...


Now I have that out of my system, what am I talking about?  I am talking about English.

As you may notice I am English, I speak English, I write in English I am fluent in English.

I recently sent a piece literature for publishing, I've self published before, this time it was going to a publisher.... Their reply:

Sorry to hear you are unable to response in person for your trities will not meet our publishing guideline.  Please check and correct spelling, punctuation and parographing, formatting and font.

Very seriously this was the reply I was sent.

I have withdrawn my manuscript for consideration with them, sending it to penguin... No no the publisher, just a penguin, it'll be able to give me a more coherent reply.

Sunday, 25 June 2023

Soooo... About the Ballad of Buster Scruggs....

It has taken me a very long time to watch this film in a single sitting, and a very long time tolerating it, so much so that upon completion and having given it some years, I immediately searched the internet for people of a like mind as me; that being that it's a load of rubbish.

There are a few out there, mostly on hidden secret dark little corners; of which clearly this is another example.

The open chorus of plaudits for this film, if we can call it that, all fall into two broad but terrifying categories.  The first are Cohen brother fans, they won't be deterred and I can agree with them on some of the points they raise.  It looks great, the cinematography and sets are for the most part great and it has call backs to great westerns.... Like the name of the Bank.... Little nuggets of gold itself.  And individually the performances are great.


The second group are those voices which really annoy me, they say "you have to be smarter" to get the joke... To read between the lines.... To understand the history.... These people are the worst, not the least as their attitude is very insulting, which it very much is, and condescending.  But really we are not dumb, we are not being obtuse and really should we need a social education background in order to comprehend some of these stories?  No, not really.

So in order, and I am going to go from memory here, we have:

Buster the gun slinger story.
The Bank robbery.
The Gold Prospector.
The limbless Thespian.
The Wagon Train.
The Stage Coach.

The weakest is the Stage Coach, just extremely weak, and unlike the others it looks the worst.  And it was really hard to watch, really quite boring and ultimately had no point.  You can tell me it did have a point, but watch it again... Did it?  Really?

The Wagon Train, it had something, there was something there.... I was even hoping that Billy/William and Alice would tie the knot but then the other Wagon Guide would fly into a jealous rage, like there was some homosexual tension or active romance the younger man was basically calling off; just something which would have explained the story, nothing did... Sure I watched the story, but it was a pointless story.

The Bank Robbery had a very strong start, you had the explanation of the previous robber being kept alive for the Marshal for three weeks and ending up doing hard labour, instead we cut to James Franco strung up... Twice... Just... Erg, there was no fabric, the two halves of it made no connection to one another save for his horse not listening to him, that was the only link between front and back.

We're half way through this production, I can already see in the last story it was simply rushed and done very much to a budget, but was pointless, so we're not far from useless.

Again the Wagon Train, slow burn start, quite interesting middle, the tension over the marriage... But then, just as you think... "Oh the lover of Billy saved Alice, now he's properly torn between the two" or something... boom... literally, killed the plot.

The title carrier, Buster Scruggs, well that was my favourite of the stories (closely followed by the Prospector) I thought that was going to be the whole style of the film, heck it is its very title.  But then they shot that down fast, and the looney toons soul rising to heaven scene.... It reminded me of the laughing hyena's at the end of Who Frames Roger Rabbit.... And just was no in keeping, it was so jarring for the whole rest of the film.  And it ultimately made no sense, like if we saw the spirits of the other gun slingers he fought, maybe.... There might have been an Undead PD thing, or a Frighteners Wild West style thing... But no, no pay off, just... a song.

This leaves the Prospector to save the film, and I have to be honest, it was slow, but my favourite story.... His throwing away ANY gold though, even little flakes in the first few pans... Just so they can say "keepers, we're getting to keepers" is pointlessly annoying, and really flies in the face of the "you need to know the history kind"... really, know the history, ever seen anyone knowingly discard gold?  But it had an interesting parable with an echo of echo message, he entered a virgin, un touched wild, dug holes, shared with the owl, nature reclaiming it almost immediately with the deer at the end... Even though they were just holes he made little impact but got what he wanted, which is a good way to think.

Rather than watch, ride on someone else's efforts and steal from nature, from the Prospector.... It's a moral tale and the only one to pick out of the bunch.

If either of the brothers were to ever read this meager gaggle of gripes, I'd say to them to loose the yes men, sometimes things aren't very good, you done goofed boys.

Sunday, 11 June 2023

React Physics 3D : First Impressions

With my home engine coming to the state of being able to visually represent a world and move about within it, I've decided to set about sorting out a physics representation.

Very much as I did with the mathematics where I set about writing all my own code and then adopting a library; in order to best learn the process from first principles I also set about writing my own physics engine and visualizer (the latter using the much more familiar to me fixed function pipeline of DirectX 9 - Yes it still has a use in 2023!)

The physics implementation I went with was a simple rigid body test for cuboids and spheres.  Then a ray cast.  At which point I set about seeking a library as it's a huge topic I didn't want to sink too much time into.

I settled, after about a fortnight of reading and trying on React Physics 3D.

My initial impressions are that it is easy to set up, supports CMake and just worked; though I'm yet to fully get to grips with it, I am happy I have my world and simulation set up and working.

I've also happily separated my game objects from my visual objects with the use of my Ecs and I'm able to do the same, by registering a new Ecs update system with my entity list and simply piping the commands to create and destroy physics objects per frame.

The update pumping of time matches my engine instantly, so I had a lot of wins.

However, I am not building in a permissive manner, I have maximum warnings as errors, I have strictness on and permissive off; I also remove all compiler specific extensions, so I am using only C++ in it's rawest, most portable form.

Immediately, even though I've compiled React separately and then just link to the library statically, I have a bunch of the React headers giving me errors; notably map, BroadPhaseSystem and DefaultLogger.

The first two are the same sort of issue, which just surprised me, there's a constexpr uint64 set to minus one (so it's max) an unsigned value set to a signed negative to under roll it... Well it's quite bad practice:

static constexpr uint64 INVALID_INDEX = -1;

And I immediately updated this to:

static constexpr uint64 INVALID_INDEX { std::numeric_limits<uint64_t>::max() };

I may feed this back to the author, he most likely knows, maybe there's even a reason for it, personally I'd always stick to numeric limits and max in this case though, not least as it is constexpr and not a hack ;) 

The other one was a lambda expression in a call to std::transform for making all logging strings lower case, the code simply used ::tolower.  However, it was not type safe, it was converting int to char, but that's not explicit and so I changed it over to read:

std::string toLowerCase(const std::string& text) {
                std::string textLower{ text };
                std::transform(textLower.begin(), textLower.end(), textLower.begin(), [](const char& character) -> char { return static_cast<char>(std::tolower(character)); });
                return textLower;
            }

The lambda here is hard to read, so lets split it out:

[](const char& character) -> char 
{
    return static_cast<char>(std::tolower(character));
}

We take in a character, return a character, explicitly, so we have to cast the int return of std::tolower... simple really, but hard to read.  It compiles down well; but just using "::tolower" well that's a decay to int and you have to be permissive in your type exchange... I don't like that, express yourself in your code type correctly, and it'll be type safe.

Friday, 2 June 2023

Thursday, 25 May 2023

Just Stand It Up: About Premature Pessimization

Engineers often talk about premature optimization, but today I'm going to just talk briefly about the opposite, premature pessimization.

I currently work on a very large code base, it has been developed over four years from scratch.  One of the first things performed were a series of investigations into "best performing" data structures, such as maps, lists and so forth.

Now of course one has to totally accept one can optimize any data structure that little bit more for a specific use case.  One also accepts that when in C++ the standard library lends itself to being replaced bu defining standard operators, iterators and the algorithms going with all this use those standard exposed APIs, so you can implement your own.

I just want you to stop though and think... Do you want to?

Too early in a project and you can start to introduce bloat, either in terms of slight differences in the optimized cases, the code from whatever third party "best" you picked and even from your build chain, as you are bringing in dependencies on someone else.

The standard library doesn't do any of this, its dependencies are guaranteed by the ABI.

So why not just use standard map, or standard vector and standard string, or standard formatting?

Quite often I'm finding it is due to premature pessimization, that developers voice who cries out about some issue they had, either when some technology was new and emerging or late in an earlier project's life where they had to optimize for that specific case I mention, where the standard version did prove itself to be a detriment.

These engineers carry with them the experience, sometimes scars, from such exposure to edge cases and bugs they had to quash.  Rightly and understandably they do not want to experience these self same issues again; their minds therefore are almost averted from just standing it up with the standard version.  They immediately seek and even proactively nay-say the standard versions in favour of domain specific "best" versions.

This is in my opinion the very definition of premature pessimization, the standard library is wonderful, diverse and full of very well tested code and will have nearly zero overhead in adding and using to your project in C++.

I would therefore coach any developer with such mental anguish over just using the standard library to simply stand it up, just get across that line of things both building and running, then extend it to remain maintainable.  And finally as you think you're getting close to stable, well then you can expend more time looking at, profiling, and understanding the edge cases.

Sunday, 23 April 2023

Missing Emergency Alert

Just 30 minutes ago the UK was meant to test a nationwide all handsets Emergency Alert system... They did, my wife received the message and we saw someone at the Crucible in Sheffield at the Snooker also get the alert.

But I didn't get it.... Precious few others I know seem to have received it, in total I can say two, the strange on the Tele and the Wife, that's it... No-one else has mentioned getting int, quite the opposite, lots of people interested in the technology didn't receive the message.

So what gives?

Has there been some technical glitch?

Did handsets turn out to not be compatible?

Did some spam protection in the networks stop the message?

It is curious, and I'd love to find out more.

Monday, 10 April 2023

Disgusted by the BBC (Taiwan & Ukraine)

I'd like to bring a heart warming post this Easter Monday; but I can't... Instead I just sat down and listened to the 1 o'clock news by the BBC and I'm shocked, annoyed and disgusted by their describing Taiwan as a "self governing province".

Taiwan is a country in it's own right!  I admit a fall out of the Chinese civil war, but the BBC describing Taiwan so is like my British head calling the Republic of Ireland a "Self Governing Province"... or Canada, or New Zealand, or Australia.  It would simply not be tolerated.

And I think this narrative can only be for one purpose, to placate Beijing!

I say, this Chinese exercise to "encircle Taiwan" is illegal, should be utterly condemned, not just in the strongest words but by physical action to see them off.

And whilst I'm at it, the same rhetoric left to the humanitarian catastrophe in Ukraine and I can only express my opinion that the west needs to stop with the words and act.

A warmonger I am happy to be called, if just one person retains their own self determined liberty, and that is what is at stake with Russian in Eastern Europe, and with China as a whole.  Countries led by literal megalomaniacs, unlike  me who hold the free world hostage!

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.

char* GetDateTimeStringHis()
{
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:

        std::string GetDateTimeStringV1()
{
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:

        std::string GetDateTimeStringV1()
{
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:

        std::string GetDateTimeStringV2()
{
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!

        std::string GetDateTimeStringV3()
{
time_t now = time(0);
std::string buffer(26, 0);
ctime_s(buffer.data(), 26, &now);
buffer[24] = 0;
return buffer;
}

I feel I'm getting somewhere, I just want to make sure we use the STL version of the time functions now and use brace initialization for the "now".

        std::string GetDateTimeStringV4()
{
std::time_t now{ std::time(0) };
std::string buffer(26, 0);
ctime_s(buffer.data(), 26, &now);
buffer[24] = 0;
return buffer;
}

This ended my concerns and improved the performance of the code, he was calling this function nearly every time he logged, nearly every time he sent a network message and for very many of his database transactions.  This was 10 minutes of just thinking about the problem.

Now... Back to <algorithm> and execution policy with him...