Live data from Hacker News

Faster Sorting with Go Generics

eli.thegreenplace.net

1–10 of 62 posts

Re: Faster Sorting with Go Generics

#2
Are there any good write ups on Go Generics vs other languages for those of us who are familiar with Go but do not write it on a day to day basis? I pick up Go every few months to try new things cause its infinitely easy to setup a web server in Go since it is built-in.

Re: Faster Sorting with Go Generics

#3

Are there any good write ups on Go Generics vs other languages for those of us who are familiar with Go but do not write it on a day to day basis? I pick up Go every few months to try new things cause its infinitely easy to setup a web server in Go since it is built-in.

The FAQ has some good information: https://go.dev/doc/faq#generics_comparison and other sections around it

Re: Faster Sorting with Go Generics

#4
> The first thing to note is that there is no dynamic dispatch to the Less method. Each loop iteration invokes cmpstring directly.

Except about 95% of the arrays I sort aren't comparable with just <. It's less clear how the unmentioned sort.Slice() will improve.

Re: Faster Sorting with Go Generics

#5

> The first thing to note is that there is no dynamic dispatch to the Less method. Each loop iteration invokes cmpstring directly. Except about 95% of the arrays I sort aren't comparable with just <. It's less clear how the unmentioned sort.Slice() will improve.

You'll still have a dynamic Less() equivalent - unless the sorting function gets inlined, which sounds unlikely. But sort.Slice() relies on reflection to do the swapping, which performs even worse than a call to a virtual Swap(). That would now be an ordinary index swap within the sort function. So you'll probably still see major speedups with bubbleSortFunc or some equivalent.

Re: Faster Sorting with Go Generics

#10

Are there any good write ups on Go Generics vs other languages for those of us who are familiar with Go but do not write it on a day to day basis? I pick up Go every few months to try new things cause its infinitely easy to setup a web server in Go since it is built-in.

I encourage you to explore why generics exist in the first place by exploring topics such as Parametric Polymorphism, Higher Kinded Types, & Higher Kinded Polymorphism.

The truth will set you free.

Post reply on HN