Monday, December 16, 2024

Inlined vs Non-inlined Code

In various programming languages, it is possible to inline code. For example, with C++, one can use design patterns or keywords like extern, static, or inline to suggest to the compiler that code should be inlined. Non-inlined code can generate extra, unnecessary function calls, as well as calls to the global offset table (GOT) or procedure linkage table (PLT).

Inlining can reduce runtime overhead and eliminate function call indirection, though it may increase code size due to duplication, since it must be copied across all translation units where it is used.

Consider the following non-inlined code:

int g;

int foo() { 
    return g; 
    }

int bar() {
    g = 1;
    foo(); 
    return g;
}

If we compile the code like so with gcc -O3 -Wall -fPIC -m32, we can observe in the assembly that indeed, this code was not inlined. There are still explicit function calls, and extra calls to the GOT and PLT.

However, it's important to note that at higher optimization levels like -O2 or -O3, the compiler may inline small functions automatically—even without the inline keyword.

foo():
        call    __x86.get_pc_thunk.ax
        add     eax, OFFSET FLAT:_GLOBAL_OFFSET_TABLE_
        mov     eax, DWORD PTR g@GOT[eax]
        mov     eax, DWORD PTR [eax]
        ret
bar():
        push    esi
        push    ebx
        call    __x86.get_pc_thunk.bx
        add     ebx, OFFSET FLAT:_GLOBAL_OFFSET_TABLE_
        sub     esp, 4
        mov     esi, DWORD PTR g@GOT[ebx]
        mov     DWORD PTR [esi], 1
        call    foo()@PLT
        mov     eax, DWORD PTR [esi]
        add     esp, 4
        pop     ebx
        pop     esi
        ret
g:
        .zero   4
__x86.get_pc_thunk.ax:
        mov     eax, DWORD PTR [esp]
        ret
__x86.get_pc_thunk.bx:
        mov     ebx, DWORD PTR [esp]
        ret

Now consider the use of the inline keyword.

int g;

inline int foo() { 
    return g; 
    }

int bar() {
    g = 1;
    foo(); 
    return g;
}

In this example, the aforementioned code significantly reduces the amount of instructions generated by GCC—the function foo has essentially been merged into bar.

If this were a small program that made repeated calls to these functions, this optimization could reduce overhead and increase performance.

bar():
        call    __x86.get_pc_thunk.ax
        add     eax, OFFSET FLAT:_GLOBAL_OFFSET_TABLE_
        mov     eax, DWORD PTR g@GOT[eax]
        mov     DWORD PTR [eax], 1
        mov     eax, 1
        ret
g:
        .zero   4
__x86.get_pc_thunk.ax:
        mov     eax, DWORD PTR [esp]
        ret

The inline keyword is a hint to the compiler. The compiler may not always follow such suggestions—it is context dependent. Other functions may be inlined and optimized by default even without such hints.

But if you're experimenting with inline code, caveats may apply[1][2][3].

Sunday, December 15, 2024

Lists

Lists are appealing because they give structure to otherwise unwieldy information.

Information security people frequently repeat the adage that defenders "think in lists" and hackers think in graphs. But a graph is just a list of lists. And it seems obvious that this useful observation extends far beyond the domain of computer security.

The reason lists and graphs are powerful is because they provide us a fair idea of what concepts are all about. Lists are tools, just like metaphors. They can let us quickly organize data and view ideas from various vantage points, which is both useful and efficient.

The brain has a natural tendency to think in lists. The brain has only a small bag of tricks, like pattern matching and repetition, and it can augment the feedback loop of consciousness in various ways—experimenting with the information it receives from the world in tandem with repeated experimentation.

An example is the history of paleontology, where every few hundred years, someone remarked, "I think some of the stuff on land was once under water. Look at these fossils." That idea was repeated over and over again, with occasional variations over time.

Today, an entire field of testable, verifiable knowledge exists, built on the foundation of what began as a mundane observation.

The paleontology parable, however, does not encompass all forms of great thinking. There are also instances of genius where unique discoveries emerge, defying the predictability of list-based heuristics.

Here are some Wikipedia lists (and timelines) that I find interesting, fun, and/or useful:

Tuesday, December 10, 2024

Currently Reading: Ben Franklin's Autobiography

Today, while updating Windows virtual machines for Patch Tuesday, I found myself re-reading Ben Franklin's autobiography.

Two fun excerpts:

At my first admission into this printing-house I took to working at press, imagining I felt a want of the bodily exercise I had been us'd to in America, where presswork is mix'd with composing. I drank only water; the other workmen, near fifty in number, were great guzzlers of beer. On occasion, I carried up and down stairs a large form of types in each hand, when others carried but one in both hands. They wondered to see, from this and several instances, that the Water-American, as they called me, was stronger than themselves, who drank strong beer! We had an alehouse boy who attended always in the house to supply the workmen. My companion at the press drank every day a pint before breakfast, a pint at breakfast with his bread and cheese, a pint between breakfast and dinner, a pint at dinner, a pint in the afternoon about six o'clock, and another when he had done his day's work. I thought it a detestable custom; but it was necessary, he suppos'd, to drink strong beer, that he might be strong to labour. I endeavoured to convince him that the bodily strength afforded by beer could only be in proportion to the grain or flour of the barley dissolved in the water of which it was made; that there was more flour in a pennyworth of bread; and therefore, if he would eat that with a pint of water, it would give him more strength than a quart of beer. He drank on, however, and had four or five shillings to pay out of his wages every Saturday night for that muddling liquor; an expense I was free from. And thus these poor devils keep themselves always under.

In the excerpt below, Ben Franklin recounts teaching others at the printing house how to swim—as well as leaping into the river in front of visitors at the college and coffee shop, surprising onlookers:

At Watts's printing-house I contracted an acquaintance with an ingenious young man, one Wygate, who, having wealthy relations, had been better educated than most printers; was a tolerable Latinist, spoke French, and lov'd reading. I taught him and a friend of his to swim at twice going into the river, and they soon became good swimmers. They introduc'd me to some gentlemen from the country, who went to Chelsea by water to see the College and Don Saltero's curiosities. In our return, at the request of the company, whose curiosity Wygate had excited, I stripped and leaped into the river, and swam from near Chelsea to Blackfriar's, performing on the way many feats of activity, both upon and under water, that surpris'd and pleas'd those to whom they were novelties.

Thursday, December 05, 2024

A lot of Nothing

A list of things I’m grateful for: good friends, great family, continued sobriety, health, books, the internet, walks, music, not being in debt—but also discipline, silence, solitude, challenges, persistence, patience, and the ability to practice a multitude of things each and every day.

I’m grateful for the freedom and peace that comes with being able to live life on my own terms, without being enslaved by emergent dynamics, irrational external expectations, or the emotional labor of navigating complex, irrational, controlling scenarios.

I’m thankful for the clarity that comes from a disciplined approach to thought, where reason and evidence guide decisions, allowing for a deeper understanding of nature and a steady path through uncertainty.

Alright. Maybe I'm just joking about that last paragraph. What I actually meant to say is that I'm grateful for my irrationality and foolishness. I mean, they're a major factor in why I don't just give up. I never learned how to.

Wednesday, December 04, 2024

Horizontal vs Vertical Distinction

The definition of metaphor is: “a figure of speech in which a word or phrase is applied to an object or action to which it is not literally applicable.”

On my other blog, I shared a thought about metaphors:

Metaphors are cool because they let you reason about a domain of knowledge by using tools that were originally intended for a different domain.

Metaphors are useful! A related but distinctly different concept from a metaphor is the concept of metonymy.

The primary difference is that a metaphor draws a similarity between objects—that is, the relation is vertical.

On the other hand, a metonym draws a link between objects that are contiguous but not similar—that is, the relation is horizontal.

The sentence “When I eat raw habanero peppers, they set my mouth on fire” is a metaphor, while the sentence "A new Oval Office has been elected" is a metonym.

In the first sentence, "peppers" and "fire" draw a similarity to one another, e.g., they are different things that can both be described as “hot.”

In the second sentence, "Oval Office" is used as a substitution for "president." The Oval Office and the president are two different things, but they are contiguous; e.g., the Oval Office is the working space of the president.

Variations of horizontal-vertical distinctions are also useful in other domains. In biology, they are often used to describe the way genes or diseases are passed on. For example, when a mother passes a disease to her child during pregnancy, childbirth, or breastfeeding, this is known as vertical transmission. In contrast, if someone catches a cold, travels, and then spreads it to others, this is an example of horizontal transmission.

Saturday, November 30, 2024

Repetitions are Sequences

When doing a task like working out, a common pattern is to perform something like 100 reps, then 90 reps, then 80, and so on, until you’ve completely counted down to zero. But this pattern can also be expressed arithmetically.

We say that there are 11 terms in this sequence: 100, 90, 80, 70, 60, 50, 40, 30, 20, 10, and 0. Alternatively, we could count the terms by solving:

\[ 0 = 100 - 10 (n - 1) \]

Now, let S represent the sum of all the terms in our sequence, N represent the number of terms, and \( t_0 \) and \( t_1 \) represent the first and last terms of the sequence.

\[ S_n = \frac{n}{2} \cdot (t_1 + t_n) \]

If we're beginning at 100 and counting all the way down to zero, we plug those values into our equation to get the total sum of 550.

\[ S_n = \frac{11}{2} \cdot (100 + 0) = 550 \]

Thursday, November 21, 2024

Absence as Category Error

When you switch on a lightbulb, your eyes perceive photons, and some neurons in your brain activate. If you switch off the light, then so-called ‘off’ neurons activate.

Photoreceptors include rods, which are responsible for the detection of dim light, and cones, which function in bright light and are responsible for the ability to distinguish colours based on their unique spectral sensitivities. These cells each have a ciliary process, known as an outer segment, that consists of stacks of membranous discs where the proteins involved in light sensing and signalling are located. The rods and cones connect to bipolar cells. There are also neurons responsible for modifying visual signals, such as amacrine cells, which connect rod bipolar cells to cone bipolar cells, and horizontal cells, which mediate feedback inhibition to the photoreceptors. The cone bipolar cells connect to ganglion cells, which integrate the signals from the upstream neurons. The ganglion cell axons assemble to form the optic nerve for transmission of visual signals to the brain.[1]

You don’t actually “stop seeing” when you’re in the dark. No; the mind physically represents nothingness in a pattern of neurons. In the case of literal darkness (as opposed to cognitive dimness), photoreceptors include a special adaptation that allows us to see, even when there appears to be very little light.

Similarly, in physics, there is darkness—the void of space. But is it entirely correct to claim that it is empty? Scientists posit that it is populated with vacuum energy. Vacuum energy is a theoretical background energy throughout the universe, as modeled by the uncertainty principle.

The uncertainty principle can be visualized in this way: imagine a field where virtual particles are constantly popping in and out of existence, imperceptible to the eyes but verifiable by deduction and lab experiments. The implication of this is that the universe is, in a sense, 'charged.' This is a tenet of quantum field theory.

This theory additionally predicts a significant amount of hypothetical energy throughout the universe. But it seems to bear little cosmological consequence, as the energy density that we actually observe is much smaller than the hypothesized model.

But if we assume the standard model is correct, this knowledge gap is known as the cosmological constant problem. It is one of several ideas put forth to account for hidden (dark) states of matter and energy implied by the universe's accelerating expansion. The thing I'm pointing at here is that there's a gap--something is there, and even if it isn't "dark energy" -- then it is, at the very least, a gap in our understanding of the universe.

The point I’m trying to draw, however, is that when we attempt to observe or discuss "nothing," we inevitably encounter "something"—or we find that "nothing" itself is a direct or indirect reference to "something." I argue that it’s impossible to truly discuss "nothing." In a genuine vacuum, a place where absolutely nothing exists, there wouldn't be any fields to measure. There would be no spacetime to speculate about.

In that vein, asking "Why is there something rather than nothing?" is a category error. If we were to ask, "Why is it snowing?" one could at least try to formulate an answer: "Due to cold temperatures, water in the atmosphere froze and fell to the ground as ice crystals." In this scenario, we are asking about a specific feature of the map. Alternatively, we could claim it was because a giant snowman god in the sky caused it to snow. On the other hand, the question "Why is there something rather than nothing?" is unknowable, because we are no longer asking about a specific abstraction of the map—instead, you're asking what created the map itself.

Similarly, the phrase “nothing exists” is a kind of inverse category error—a claim that nothing is real. However, labels like "absence" or "nothing" often function as references or pointers to other things. 

If we must say it, the phrase "nothing exists" is not a self-contradicting statement but a humorous or horrifying statement of fact. Nothing exists.

"Nothing could ruin this moment." "Nothing can dim this light." "Nothing is too great a challenge." "Nothing lasts forever."


Using Python To Access archive.today, July 2025

It seems like a lot of the previous software wrappers to interact with archive.today (and archive.is, archive.ph, etc) via the command-line ...