r/programming • • 11d ago

What Go 1.27’s generic methods made possible for ORMs. Compile-checked columns with no generated code

https://skudra.net/blog/field-pointers-as-column-names
102 Upvotes

19 comments sorted by

8

u/tef 11d ago

i feel it's worth noting that u.Field will also be a runtime error, but it's still a bit of a neat trick

3

u/ZealousidealStorm200 11d ago

Yeah, that's one of the cases the compiler doesn't catch, It's mentioned in the "where it still fails" section. The vet tool does catch it though https://skudra.net/tooling/antvet , so, ideally, a pre-commit hook or a CI step would catch it

1

u/tef 11d ago

ah, i only saw &u.Field

tbh, if you've committed to a pre-commit hook or a ci step, then a go generate doesn't feel too much to ask of the user, which could generate a struct with right fields

1

u/ZealousidealStorm200 10d ago

That's a fair point, it's the tradeoff ent and jet make. What drove me away is that codegen is required there, nothing compiles until it runs, so the generated code always has to stay in sync with your models, and generation has to happen before every commit or CI run.

my experience, small changes introduce drift from the generated code. If a model or schema change doesn't break the existing generated code and everything still compiles, then it's easy for someone to just... not regenerate. That adds up over time

antvet is optional, the code builds and runs without it. It catches a narrow set of mistakes, and most column and type errors are still caught by the compiler. Basically, a tradeoff

1

u/tef 10d ago

yeah, i tend to use reflection where i can instead, but, well, there's limits like "function arguments do not have names", and i also find myself wondering if a tiny bit of codegen would work

6

u/Lotaor_Wieth 11d ago

Does this still rely on reflection at runtime to map the rows back into structs, or is the whole path resolved at compile time now? Always wondered how much overhead generic methods actually save at runtime

5

u/ZealousidealStorm200 10d ago

It does use reflection at run time, but only once for each struct type (when you create the probe). ant learn the structs its layout, which field goes with which column, and where each field sits in memory, and that's stored in an internal slice and a map. After that, when a row is read, it writes each column straight into the right spot in your struct with no reflections for each row (because the mapping is known)

Generics don't remove reflections by themselves, but they do give typed results, like, we can know that the result will be []User instead of interface{}. Keeping those mappings in memory allows creating way less temporary objects. So reading rows costs about the same as a plain database/sql call.

4

u/txdv 11d ago

not everyone is happy about ORMs, but I guess you enable people and let them make decisions

4

u/ZealousidealStorm200 11d ago

Yeah, well I also searched around for a while. The company I work at switched from .NET to Go for newer projects, but DB stayed mostly the same, PG with procedures for all the business logic.

We wanted to switch over to an ORM so there's less "separation" between API programmers and DB programmers, but I couldn't find any ORM that matched all our requirements and wants, and that could also cleanly integrate into an existing project.

I think ORMs are okayish, but clearly have their downsides and flaws too. I think the golden middle ground is using ORM for simpler business functions, and using stored procedures for more complex and performance critical situations.

1

u/txdv 11d ago

im ok with them if they dont do expand one to many relations into arrays/lists as subqueries

1

u/standing_artisan 10d ago

Generics are not 100% free, they come with some amount of cost on the runtime of your program, yes not the magnitude of reflection but still not 100% free because of they hybrid approach on implementing generics. Based on this context. I still don't understand why would I ever use a gorm/ant or whatever and not write simple sql queryes and at MAX use a query builder where I need to create those dynamically.

1

u/ZealousidealStorm200 10d ago

Generics do have a cost, but in ant they're mostly in building the query, the bigger overhead is allocations and change tracking. And with change tracking off, it's about 8% over plain database/sql on a 1000 row read.

Plain SQL with a simple query builder is a totally fine approach, sqlc too. ant is meant for when you want the features of an ORM, with columns checked by the compiler (without a generate step), relation loading in batches, and change tracking. If those aren't needed, then you don't need ant :)

-16

u/Beaverman 11d ago

This is LLM written.

20

u/ZealousidealStorm200 11d ago

Author here, I wrote the post myself. The disclaimer at the bottom is about the library; yes, large parts of it were made with AI tooling based on an initial proof of concept I made. I keep it in check with wide test coverage, comparisons against other libs, and a list of architecture decisions.

The post is meant to communicate the technique that became possible with the new Go generic methods, for ant and for other ORMs too.

1

u/Old_County5271 11d ago

Did the compile time speed suffer? Did it reach rust or c levels?

1

u/robert_7524 5d ago

Did you measure clean and incremental builds separately? A Rust/C comparison needs a comparable workload and build mode.

1

u/ketralnis 11d ago

Why do you say so? I’m not getting that vibe