Skip to content

Commit

Permalink
Remove sqlc queries internalized on driver/executor structs (#455)
Browse files Browse the repository at this point in the history
A small refactor to remove the embedded sqlc queries that's internalized
on the structs for the drivers and executors. I originally did this
because I thought it was a small memory optimization, but it turns out
that it doesn't matter because `Queries` is an empty struct. It uses
zero bytes of memory, and multiple sequential initializations end up
pointing to the same memory location.

Here's a test script to prove that this is true:

    package main

    import (
        "fmt"
        "unsafe"

        "github.com/riverqueue/river/riverdriver/riverpgxv5/internal/dbsqlc"
    )

    func main() {
        var (
            a = dbsqlc.New()
            b = dbsqlc.New()
        )

        fmt.Printf("size of a = %v\n", unsafe.Sizeof(*a))
        fmt.Printf("addr of a = %p\n", a)

        fmt.Printf("size of b = %v\n", unsafe.Sizeof(*b))
        fmt.Printf("addr of b = %p\n", b)

        anotherFunction()
    }

    // Even in a different function, the same struct is reused
    func anotherFunction() {
        c := dbsqlc.New()

        fmt.Printf("size of c = %v\n", unsafe.Sizeof(*c))
        fmt.Printf("addr of c = %p\n", c)
    }

Run output:

    size of a = 0
    addr of a = 0x1031710c0
    size of b = 0
    addr of b = 0x1031710c0
    size of c = 0
    addr of c = 0x1031710c0
  • Loading branch information
brandur authored Jul 15, 2024
1 parent 18fbd7d commit 0f5e131
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 100 deletions.
Loading

0 comments on commit 0f5e131

Please sign in to comment.