Blogs

Serializing Slow Functions

Quite often, on the Internet, we want software that does it all because ... it's better.

However, once in a while a function has to handle a very complicated or at least very consuming (works on a large amount of data) function.

If you have hundreds of hits per seconds (or even millions), you could end up eating all your resources as in: so many instances of that heavy functions are running concurrently that the computer has to swap out some memory, some network connections fail, the system is slow for everyone and everything.

A solution is to serialize the requests. With a bit of AJAX, ...

Is this Shisensho game impossible to resolve?

You like Shisensho?

You're really good at it?

My setup says it should only generate games that can be resolved.

This one, though, I have not been able to resolve.

Can you give it a try? (maybe at some point I'll add a Save & Load feature so we can share those and easily play them...)

A Shisensho game which I have not been able to resolved.

Click on the picture to make it much larger.

The following is how I resolve it to the best of my ability... Not that great and for sure, not complete.

Impossible to resolve best solution

Click on the picture to make it much larger.

Another complex Shisen-Sho game!

In most cases, Shisen-Sho games have tiles that can be removed at the very top. That's how you start playing, by removing the top tiles first and moving downward from there. Most often, the best move is to collect the two closest tiles together and you win first time you play a game.

This one is different. That is, you still remove tiles that are together, but you have to work in the middle and the bottom a lot before you can start working on the top. This makes the game much harder to complete.

I made a picture of the game. This is from my 4K monitor, so very high quality. Click on the ...

Not Registered On Network (Samsung Galaxy J7—Android)

Today I tried to make a call from my cell phone to my landline. It did not work. Instead, I got, pretty quickly, this error:

Not Registered On Network

I'm using T-Mobile and my 3 other family members did not complain about their functionality so I would imagine that our accounts are just fine.

Looking for the error, I found one person who said he had the problem too and by restarting the phone it would fix his problem... for some 30 seconds.

Well... I thought I would give it a try and sure enough once I had my phone restarted I received the 5 or 6 text messages I was expecting and ...

Go tests are run concurrently!

Go tests are run concurrently in their own context

I ran in a problem while writing go tests... and it looked like some parallelism or at least concurrency issues.

The initialization would run twice and not finish properly the second time. That means it would load a file from a test, a file which is rather bad (read invalid) outside of that one test.

So I looked around to see what was happening and that's how I learned that tests in go run concurrently. It sounds all good unless you want to do integration testing because such require a state which is actually not reachable if your unit tests run concurrently. But why is that?

Well... ...

Common bug in Go, maybe they did not resolve anything?

Variable shadowing, a common problem, no good solution in modern languages to avoid it.

C++ is not that bad!

With C++, and derivatives, you can shadow any one variable over and over again:

int var;

void myfunc()
{
    int var;

    if(expr)
    {
        int var;

        ...
    }
}

In this example, I shadow the var variable twice within myfunc().

In itself, it doesn't hurt, but it is very often that when you do that, you did not mean to. That is, it's not unlikely that you reuse var within the function thinking it's the global variable or the first instance in myfunc() from the outermost block of the function.

I have two rules I follow in ...

The new C++17 [[fallthrough]] "attribute"

Here I wanted to talk briefly about this new C++17 attribute:

[[fallthrough]];

I wanted that attribute like 30 years ago but the compiler programmers were definitely not ready to allow us to have safe coding practices...

Back then, any potential error was a beautiful feature. I'm really glad that some people pushed the idea that having tons of warnings about very possibly invalid code is a good thing long term. I am fixing some of my bugs just by compiling my code with newer versions of the compiler!

Now... I ran in a few problems with that new attribute. The fact is that it's a ...

A little bit of Go!

I changed a goroutine to make use a service which gets awaken whenever a signal happens. This is used because the signal is sent only in very complicated cases and I wanted to remove the tests from the bottom goroutine (many of the parameters are from other structures not available here.)

Here I wanted to get the basics to show how it's done.

var (
  someMutex      sync.Mutex
  someCond       *sync.Cond
  someState      bool
)

func init() {
  someCond = sync.NewCond(&someMutex)
  go someLoop()
}

func sendSignal() {
  someMutex.Lock()
  defer someMutex.Unlock()

  ...

C++: Why should I breakup my expressions?

One Line Expressions

There has been and I'm sure there still are many talks about whether to write it all on one line or break up expressions on many lines.

One Line Pros

The only pro I can think of for having everything on the same line is conciseness. For example:

*dst++ = *src++;

This is a very clear statement for most every C/C++ programmer.

I have to agree with this fact and I at times use such lines of code too...

One Line Cons

Now, I write very large libraries and programs (thousands when not ten of thousands or even hundreds of thousand lines of code....) and it ...

Meta-programming, promoting a type without changing the sign

Today I found that one of my functions would overflow. Here is a simplified version of it:

template<typename T>
T scale(T value, T position, T count)
{
    return (value * position) / count;
}

As we can see, all variables are of type T. So if the multiplication overflows, the function returns an invalid result. To fix this, I wanted to promote all the variables to the latest possible integer (keeping the sign) or double (in my case the support for floating points was just for the type float so no need for long double.)

As a result I wrote a structure using meta-programming as ...