pydis – A redis clone in Python 3 to disprove some falsehoods about performance

I developed pydis in two days to disprove some of the falsehoods about performance and optimisation regarding software and interpreted languages in particular.

Unfortunately many programmers, due to their lack of experience, of some knowledge of computer architecture(s), or of an in-depth understanding of the task they are given, spend countless hours by making life harder for themselves in the name of marginal performance gains, often trading many other conveniences (such as type safety, garbage collection, etc) too.

pydis is < 250 lines of idiomatic Python code, providing a subset of redis’ functionality for which there are official benchmarks, and yet it’s ~60% as fast as Redis measured in number operations per second.

Remember, man-hours are more expensive that cpu-time.

https://github.com/boramalper/pydis

Continue reading

A Better Approach to DFA Minimization

DFA minimization is often described as follows:

  1. First eliminate any unreachable states (easy).
  2. Then create a table of all possible pairs of states \((p, q)\), initially unmarked. (E.g. a two-dimensional array of booleans, initially set to false.) We mark pairs \((p, q)\) as and when we discover that \(p\) and \(q\) cannot be equivalent.
    1. Start by marking all pairs \((p, q)\) where \(p \in F\) and \(q \not\in F\), or vice versa.
    2. Look for unmarked pairs \((p, q)\) such that for some \(u \in \Sigma\), the pair \((δ(p, u), δ(q, u))\) is marked. Then mark \((p, q)\).
    3. Repeat step 2.2 until no such unmarked pairs remain. If \((p, q)\) is still unmarked, can collapse \(p\) and \(q\) to a single state.

Source: Mary Cryan, Informatics 2A: Processing Formal and Natural Languages, Lecture 5, 2018.

Based on my very limited research, aforementioned algorithm seems to be the most common way of teaching undergrads how to minimise a given DFA, but I believe that it suffers from reasons soon listed below, and I will instead propose a better way of executing the same algorithm that does not (make the student) suffer from the same problems.

Continue reading

Marketing is a State of Mind, not an Afterthought

Marketing, due to its historical luggage, is gravely misunderstood by many people as something to be done to increase the sales of a product (i.e. promoting), whereas it should instead be understood as a strategical process, which is never done but continuing as long as the producer exists. Whether you are an entrepreneur, a solo free-software developer, or an indie game-dev, as long as you are a producer, you -presumably- must be working for your products to be used/consumed/bought by people (unless -of course- you are an artist, then you might want to abstain from the consideration of your audience). It is clear then, that marketing is crucial simply if you do not want your work to get wasted. You do need marketing.

Philip Kotler
Philip Kotler

If this post manages to wet your appetite, you should absolutely check Marketing: An Introduction by Gary Armstrong, Philip Kotler et al. It is a 640 pages-long book, and as it indicates, there is no way for anyone to even briefly summarise -yet alone cover– marketing thoroughly. What you are reading is (a) written by a student, and (b) is a tiny fraction of a tiny fraction: think of it as a stimulus.

I’ll mention 3 different (simplified) eras each industry goes (or already went) through, so that you can understand where most of the misconceptions about marketing came from, and then I will explain how the modern approach to marketing makes more sense in today’s context – assuming that the industry you are working in is already matured; this will make more sense once you read the post.

Continue reading

Dropping root Privileges Permanently on Linux in C

If you would like to drop root privileges permanently on Linux using C:

#define _GNU_SOURCE  // for secure_getenv()


int drop_root_privileges(void) {  // returns 0 on success and -1 on failure
	gid_t gid;
	uid_t uid;

	// no need to "drop" the privileges that you don't have in the first place!
	if (getuid() != 0) {
		return 0;
	}

	// when your program is invoked with sudo, getuid() will return 0 and you
	// won't be able to drop your privileges
	if ((uid = getuid()) == 0) {
		const char *sudo_uid = secure_getenv("SUDO_UID");
		if (sudo_uid == NULL) {
			printf("environment variable `SUDO_UID` not found\n");
			return -1;
		}
		errno = 0;
		uid = (uid_t) strtoll(sudo_uid, NULL, 10);
		if (errno != 0) {
			perror("under-/over-flow in converting `SUDO_UID` to integer");
			return -1;
		}
	}

	// again, in case your program is invoked using sudo
	if ((gid = getgid()) == 0) {
		const char *sudo_gid = secure_getenv("SUDO_GID");
		if (sudo_gid == NULL) {
			printf("environment variable `SUDO_GID` not found\n");
			return -1;
		}
		errno = 0;
		gid = (gid_t) strtoll(sudo_gid, NULL, 10);
		if (errno != 0) {
			perror("under-/over-flow in converting `SUDO_GID` to integer");
			return -1;
		}
	}
	
	if (setgid(gid) != 0) {
		perror("setgid");
		return -1;
	}
	if (setuid(uid) != 0) {
		perror("setgid");
		return -1;	
	}

	// change your directory to somewhere else, just in case if you are in a
	// root-owned one (e.g. /root)
	if (chdir("/") != 0) {
		perror("chdir");
		return -1;
	}

	// check if we successfully dropped the root privileges
	if (setuid(0) == 0 || seteuid(0) == 0) {
		printf("could not drop root privileges!\n");
		return -1;
	}

	return 0;
}

I hope this would save you some time so that you won’t spend an hour like me, trying to find a proper solution!

The code is provided “as is”, without warranty of any kind, express or implied.

Sources

c – Dropping root privileges – Stack Overflow

man sudo (Sudo Manual)

Closed-Form Expression to Calculate n-th Fibonacci Number

A more interesting way to find nth Fibonacci number.

Fibonacci sequence is a prime example in teaching recursion to newcomers, and a great opportunity to brag if your language supports Tail Call Optimization, but it often goes unnoticed that there is a closed-form expression which lets us find the nth Fibonacci number with great ease and in much faster way. This article will present the expression, and explain -step by step- its derivation using high-school mathematics.

Continue reading

Commandments for University/College Applicants

After messing up my university applications, I have decided to create a list for fellow UWC Dilijan students to get rid of the glooming feeling of regret, but I guess the list might be equally useful for other UWCers, as well as IB students and others who are planning to study abroad.

Continue reading

The featured caricature of the article in The Economist

Western Media Bias: Analysis of an Article in The Economist

I have happened to buy “The Economist” for my two-days-long layover in İstanbul to kill some spare time, and read an article titled The prince’s time machine, which is about the “shift from the Islamic to the Gregorian calendar” in Saudi Arabia. The article was so full of bias that I couldn’t resist myself but to write a response.

Continue reading

Some Advice to Those Who Will Serve Time in a UWC

or Things I Wish I Had Known When I Started UWC Dilijan

It has been one and a half year since I have started studying in Dilijan and it has been only one semester that I can confidently say that I have been mentally and emotionally stable. There are many things that you realize in your third term when everything is kind of done and that makes you realize what have you done wrong in the past, or what you could have done better. Take this prose as a honest reflection and a sincere advice to those who will serve time in a UWC.

Continue reading

Let us not waste our time in idle discourse! (Pause. Vehemently.) Let us do something, while we have the chance! It is not every day that we are needed. Not indeed that we personally are needed. Others would meet the case equally well, if not better. To all mankind they were addressed, those cries for help still ringing in our ears! But at this place, at this moment of time, all mankind is us, whether we like it or not. Let us make the most of it, before it is too late! Let us represent worthily for once the foul brood to which a cruel fate consigned us! What do you say? (Estragon says nothing.) It is true that when with folded arms we weigh the pros and cons we are no less a credit to our species. The tiger bounds to the help of his congeners without the least reflection, or else he slinks away into the depths of the thickets. But that is not the question. What are we doing here, that is the question. And we are blessed in this, that we happen to know the answer. Yes, in this immense confusion one thing alone is clear. We are waiting for Godot to come—