Monday, December 30, 2024

Prevent Chrome's Translation Feature from Reformatting Code Blocks

Chrome Browser annoyingly reformats code blocks occasionally when using the translate feature. For example, if you translate a Chinese web page to English—if there are code blocks, it may also parse and reformat them, making them harder to read. Here's a workaround via Chrome DevTools Console. Run this before translating a web page:

// browser console hack to prevent chrome from mucking  
// with code blocks whenever a page is translated, 
// e.g. translating a chinese page to english

document.querySelectorAll('pre, code').forEach(block => {
  block.style.whiteSpace = 'pre-wrap';
  block.setAttribute('translate', 'no');
});

Monday, December 16, 2024

Early Timekeeping

Why are there sixty seconds in a minute, sixty minutes in an hour, yet twenty-four hours in a day? The answer is because modern timekeeping derives from the base-60 number system.

It is believed that Sumerians of Mesopotamia used their phalanges to count. They counted increments of 12 with one hand's four fingers, each of which has three bones, and tracked them with the other hand's five fingers: 12, 24, 36, 48, 60.

Early civilization calendars were often lunisolar, based on the phases of the moon—roughly aware of the sun's yearly 365-day orbit. Though they were somewhat imprecise, many resembled the 12-month calendar we know today. Ancient calendars, however, would often have extra days or months periodically added for alignment purposes.

The Sumerians had no tradition for referring to the length of time we call a "week," nor did they identify months. They simply observed months and years. Later, the Babylonians would put forth the notion of the "week," as well as move to use a solar, rather than lunisolar, calendar.

A history of calendars: https://en.wikipedia.org/wiki/History_of_calendars

God as Atheist

In Orthodoxy, G.K. Chesterton turns familiar ideas upside down and urges us to see truth in paradox.

Chesterton says that, for example, artists like and enjoy their own limitations, precisely because they are defined by those limitations. Constraints themselves are a kind of source from which creativity springs.

But one of the most interesting sections of Orthodoxy is when Chesterton begins to remark more directly about religion.

Chesterton notes that Christianity is one of the only religions in which the idea of "God" is not an omnipotent force—on the contrary, God is represented in a trinity—including one branch in which God himself is human in the form of Christ. He describes the crucifixion of Christ, pointing out that Christianity is paradoxically one of the only religions in which God himself briefly becomes an atheist.

All moral reform must start in the active not the passive will.

Here again we reach the same substantial conclusion. In so far as we desire the definite reconstructions and the dangerous revolutions which have distinguished European civilisation, we shall not discourage the thought of possible ruin; we shall rather encourage it. If we want, like the Eastern saints, merely to contemplate how right things are, of course we shall only say that they must go right. But if we particularly want to make them go right, we must insist that they may go wrong.

Lastly, this truth is yet again true in the case of the common modern attempts to diminish or to explain away the divinity of Christ. The thing may be true or not; that I shall deal with before I end. But if the divinity is true it is certainly terribly revolutionary. That a good man may have his back to the wall is no more than we knew already; but that God could have his back to the wall is a boast for all insurgents for ever. Christianity is the only religion on earth that has felt that omnipotence made God incomplete. Christianity alone has felt that God, to be wholly God, must have been a rebel as well as a king. Alone of all creeds, Christianity has added courage to the virtues of the Creator. For the only courage worth calling courage must necessarily mean that the soul passes a breaking point — and does not break. In this indeed I approach a matter more dark and awful than it is easy to discuss; and I apologise in advance if any of my phrases fall wrong or seem irreverent touching a matter which the greatest saints and thinkers have justly feared to approach. But in that terrific tale of the Passion there is a distinct emotional suggestion that the author of all things (in some unthinkable way) went not only through agony, but through doubt. It is written, "Thou shalt not tempt the Lord thy God." No; but the Lord thy God may tempt Himself; and it seems as if this was what happened in Gcthsemane. In a garden Satan tempted man: and in a garden God tempted God. He passed in some superhuman manner through our human horror of pessimism. When the world shook and the sun was wiped out of heaven, it was not at the crucifixion, but at the cry from the cross: the cry which confessed that God was forsaken of God. And now let the revolutionists choose a creed from all the creeds and a god from all the gods of the world, carefully weighing all the gods of inevitable recurrence and of unalterable power. They will not find another god who has himself been in revolt. Nay, (the matter grows too difficult for human speech) but let the atheists themselves choose a god. They will find only one divinity who ever uttered their isolation; only one religion in which God seemed for an instant to be an atheist.

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.

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 ...