Skip to content

Instantly share code, notes, and snippets.

View jimblandy's full-sized avatar

Jim Blandy jimblandy

View GitHub Profile

I get around 7.25 hours of sleep a night on average, and for a long time I have wanted to get eight hours instead. But despite having this as a goal, when it's time for me to go to bed, I often find myself casting about for some sort of activity to do instead: "let me just check YouTube" "maybe a bit of reading before bed would be nice" "I wonder if there's anything really important on the NYT web site", and so on. None of these activities are urgent, and none of them couldn't perfectly well be put off until morning. This often leads to my staying up much later than I'd planned.

There's something in me that feels like going to bed is irritating, a waste of time, a missed opportunity to spend a few moments enjoying myself. Or perhaps it feels like a capitulation, giving up on this day and putting off fun things until tomorrow. I know these feelings are counterproductive, but it seems like they usually win out.

My question is not about strategies for going to bed on time. Rather, my question is abou

I'm writing a book about Rust. One phenomenon I've noticed while working on the book is that, after I've been working on a particular passage for a while and I've read it over many times, it becomes hard for me to really "see" the text of the passage clearly, and follow its flow the way a new reader would. The process of revision becomes difficult because it's harder to actually pay attention to the text that I've read so many times before. Subjectively, it's almost as if something in my mind has become saturated, and can't absorb any more.

From a cognitive science or psychological point of view, what's going on here? Is there anything I can do to help me work on my text more effectively?

What you're describing is a well-documented cognitive phenomenon known as textual habituation or perceptual fluency. Essentially, the brain becomes so familiar with the text that it stops processing it deeply. Instead of actively reading each word, your mind starts skimming or filling in gaps based on memory. This mak

@jimblandy
jimblandy / ChatGPT-Naga-IR.md
Last active February 13, 2025 00:10
ChatGPT's advice on Naga IR design

I work on a Rust crate called Naga that translates GPU shaders from one language (say, the WebGPU shading language, also called WGSL) to another (say, Vulkan's SPIR-V). Naga is divided into front ends that parse shader modules into Naga's own IR, a middle end that does validation and analysis in language-independent code, and back ends that generate shader modules from IR.

At first, we had a principle that the IR should only represent concepts needed to preserve the original shader code's meaning, and concerns that were specific to some front end should be isolated in that front end. For example, WGSL promises that expressions containing only compile-time constants are fully evaluated at compile time, so that users can write general expressions for array sizes, alignment attributes on structure members, and other values that need to be known at compile time. At first, Naga's IR did not permit general expressions to occur in such places, requiring the front end to determine their final value itself befo

@jimblandy
jimblandy / 200-asynchronous.md
Last active November 15, 2024 10:04
Asynchronous Programming

Asynchronous Programming

In the last chapter, we explained how to use threads in Rust to spread compute-intensive work across multiple processor cores, putting the entire machine to work. But threads are more than just a way to get things done faster: they also make a programming language more expressive. The clearest way to write a program that carries out multiple activities independently, but concurrently, is by creating a thread to handle each one.

diff --git a/gfx/wgpu_bindings/Cargo.toml b/gfx/wgpu_bindings/Cargo.toml
--- a/gfx/wgpu_bindings/Cargo.toml
+++ b/gfx/wgpu_bindings/Cargo.toml
@@ -17,7 +17,7 @@ default = []
[dependencies.wgc]
package = "wgpu-core"
git = "https://github.com/gfx-rs/wgpu"
-rev = "3fda684eb9e69c78b16312a3e927e3ea82e853d1"
+rev = "d3e09dd63ad54e2982ef744a6d74df468e232ff0"
# TODO: remove the replay feature on the next update containing https://github.com/gfx-rs/wgpu/pull/5182
1...2...3...4...5...6...7...8...
*** * *
*** * *
. It's unfortunate that when we feel a
stone *** * *
*** * *
*** * *
* *** * *
*** * *
.
@jimblandy
jimblandy / shmem.md
Last active December 24, 2024 18:05
Explanation of Firefox's `mozilla::ipc::Shmem`

ipc::Shmem and friends

The mozilla::ipc::Shmem type represents a block of memory that IPDL messages can transfer between processes without having to copy the contents. IPDL messages can pass and Shmem arguments, as in these examples from PWebGPU.ipdl:

async BufferReturnShmem(RawId selfId, Shmem shmem);
async BufferMap(RawId selfId, WGPUHostMap hostMap, uint64_t offset, uint64_t size) returns (Shmem sm);

Given a Shmem s, you can call s.Size<T> to get the length of the shared memory block considered as an array of T values, and s.get<T> to get a T* pointing to the first element.

A Shmem is usually first constructed with no memory allocated to it, using the default constructor. For example, the[Device::CreateBuffer][cbi] method in Firefox's WebGPU implementation declares a Shmem like this:

@jimblandy
jimblandy / sqrt-2-irrational.md
Last active October 14, 2021 17:59
Proof that the square root of two is irrational

Prep questions:

  • Suppose x is even. If we multiply it by something, is the product always even, always odd, or does it depend?
  • Suppose x is even. Is x^2 always even, always odd, or does it depend?
  • Suppose x is odd. Is x^2 always even, always odd, or does it depend?
  • Suppose p/q is a fraction in lowest terms. Can both the top and bottom be even? Can they both be odd? Can one be even, and one odd?
  • What happens when we square a fraction?
  • What happens when we square a product, say, pq?

Rules:

  • "x is even" means "For some integer k, x = 2k."
@jimblandy
jimblandy / customizations.el
Created July 22, 2021 21:29
My Rust configuration for Emacs. Not tested or really prepared for publication, just offered as-is.
;; Customizations. I think you need to integrate these into the existing calls to `custom-set-variables`
;; and `custom-set-faces`.
(custom-set-variables
'(lsp-keymap-prefix "C-S-l")
'(lsp-rust-all-features t)
'(lsp-rust-analyzer-diagnostics-disabled ["unresolved-proc-macro"])
'(lsp-rust-analyzer-display-chaining-hints t)
'(lsp-rust-analyzer-display-parameter-hints t)
'(lsp-rust-analyzer-server-command '("rust-analyzer-linux"))
@jimblandy
jimblandy / dump-type.py
Last active April 21, 2024 15:09
GDB Python script to define a command for inspecting types.
# Put this in a file BLAH/dump-type.py and then put this in your ~/.gdbinit file
# (or type it at the GDB prompt):
#
# source BLAH/dump-type.py
#
# Then you can say:
#
# (gdb) help jimb-dump-type
#
# and you should get a help message.