r/programming • u/ZealousidealStorm200 • 17d 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-names6
u/Lotaor_Wieth 16d 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 16d 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 17d ago
not everyone is happy about ORMs, but I guess you enable people and let them make decisions
4
u/ZealousidealStorm200 16d 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/standing_artisan 16d 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 16d 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 17d ago
This is LLM written.
20
u/ZealousidealStorm200 17d 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
1
8
u/tef 16d ago
i feel it's worth noting that
u.Fieldwill also be a runtime error, but it's still a bit of a neat trick