flak rss random

regexp/o

Perl has a regex option /o, which pretends to optimize your code, but actually introduces bugs. Go has been missing out. Until now.

We need regexp of course, but also text/template for interpolation. Add in runtime to get the pc, and we have all the elements for a cache of only once regex.

func Compile(s string, vals any) (*regexp.Regexp, error) {
        cacheMtx.Lock()
        defer cacheMtx.Unlock()
        pc, _, _, _ := runtime.Caller(1)
        k := key{s: s, pc: pc}
        re := cache[k]
        if re != nil {
                return re, nil
        }
        t := template.New("regex")
        t, err := t.Parse(s)
        if err != nil {
                return nil, err
        }
        var sb strings.Builder
        err = t.Execute(&sb, vals)
        if err != nil {
                return nil, err
        }
        re, err = regexp.Compile(sb.String())
        if err != nil {
                return nil, err
        }
        cache[k] = re
        return re, nil
}

type key struct {
        s  string
        pc uintptr
}

var cacheMtx sync.Mutex
var cache = make(map[key]*regexp.Regexp)

And a test program.

func main() {
        fmt.Printf("starting\n")
        inputs := []string{"Apple", "bananas", "42"}
        for i := 0; i < 3; i++ {
                re, err := regexpo.Compile("{{ call .Letters }}", map[string]any{
                        "Letters": func() string {
                                time.Sleep(1 * time.Second)
                                fmt.Printf("substitute\n")
                                return "[a-z]"
                        },
                })
                if err != nil {
                        fmt.Printf("failure: %s\n", err)
                        return
                }
                ok := re.MatchString(inputs[i])
                fmt.Printf("match: %v\n", ok)
        }
        fmt.Printf("done.\n")
}

The substitution is only done once.

Posted 04 Aug 2025 20:18 by tedu Updated: 04 Aug 2025 20:18
Tagged: go programming

writing a little gosh

I had the idea to write a little shell in go. Called gosh, of course. There’s a few people playing with the same theme, but nothing exactly the same.

more...

Posted 30 Jun 2025 16:33 by tedu Updated: 30 Jun 2025 16:33
Tagged: go programming

forbidden secrets of ancient X11 scaling technology revealed

People keep telling me that X11 doesn’t support DPI scaling, or fractional scaling, or multiple monitors, or something. There’s nothing you can do to make it work. I find this surprising. Why doesn’t it work? I figure the best way to find out is try the impossible and see how far we get.

more...

Posted 24 Jun 2025 17:59 by tedu Updated: 24 Jun 2025 17:59
Tagged: programming x11

vulgar gestures

I like to go on the internet and click on links, but some of the links are bad, so then I swipe right to make it go away. The problem is that when I’m running chrome on OpenBSD, the swipe gesture doesn’t seem to work like it does on other platforms. We’re not going to fix it, but we are going to make it work. (Although, I hear the younglings say they swipe right when they like something. Explains a lot, actually.)

more...

Posted 20 Jun 2025 15:37 by tedu Updated: 20 Jun 2025 15:37
Tagged: openbsd programming

slog is aptly named

I used to use the go log package, then I switched to the slog package, and it’s been a bumpy ride.

more...

Posted 13 Jun 2025 08:10 by tedu Updated: 13 Jun 2025 08:10
Tagged: go programming

sometimes the dependencies are useful

I ripped out a dependency and then I found out what it did. I wrote an RSS parser for a very simple project, and then figured, how hard could it be to use in a real feed reader? Well, not very hard, but it was somewhat time consuming, and offers another perspective on using other people’s code.

more...

Posted 26 May 2025 19:47 by tedu Updated: 26 May 2025 19:47
Tagged: programming project

another tale of go.mod bloat

It’s been one year since our previous adventure, so it’s time for another round of guess why that dependency shows up in the tarball. This time we’re looking at honk, an ActivityPub server that’s supposed to be idiosyncratic with minimal dependencies, so you can keep all your attention focused where it’s needed.

more...

Posted 22 May 2025 07:27 by tedu Updated: 22 May 2025 07:27
Tagged: go programming

too much go misdirection

Poking through layers of indirection in go trying to recover some efficiency.

more...

Posted 19 May 2025 14:45 by tedu Updated: 20 May 2025 23:06
Tagged: go programming

fan service

ASUS laptops generally have a feature that lets the user toggle the fan speed. Fn-F5 on some models, Fn-F on others. The direct effect is to limit the fan speed, from whisper mode to megablast, and indirectly control performance. But it doesn’t work in OpenBSD, so I needed to write an ASUS ACPI WMI driver.

more...

Posted 11 May 2025 01:57 by tedu Updated: 11 May 2025 01:57
Tagged: computers openbsd programming

checking the wifi

As I move around, I roam between wifi networks, but sometimes lose the connection. Then I click a link and watch in vain as it fails to load. So I’d like an easy way to check which, if any, wifi network I’m connected to, such as by putting it in my dwm status bar. I could run ifconfig and parse the output, but that’s excessively wasteful. I need to get the info myself.

more...

Posted 30 Apr 2025 08:00 by tedu Updated: 30 Apr 2025 18:38
Tagged: openbsd programming
V
V