4 ms·
> You have the overhead of boxing the value, but the actual type checking is just a single comparison and jump. This is way too much in a tight loop. I want m
by sacado2 7y ago
> You have the overhead of boxing the value, but the actual type checking is just a single comparison and jump.
This is way too much in a tight loop. I want my structure to fit in the cache and I want to have as little tests as possible.
- foldr 7y agoMost of the time it won't actually be "too much". The comparison itself has very little overhead due to branch prediction. But yes, if you're writing performance critical code (which you generally won't be), then you'll need to use code generation to avoid boxing. This kind of discussion is pointless without some kind of real world reference point. The vast majority of Go projects will never run into a situation where the overhead of using interface{} is a performance bottleneck. If you're working on a project where you consistently have to think about the cache performance of your code, then C++ or Rust would probably be better choices. Go isn't meant to be all things to all people.
- sacado2 7y agoIf you want a real-world example: I struggle with my SAT solver, gophersat[1]. I have different kinds of constraints, mainly propositional clauses (a or b or c) and pseudo-boolean constraints (2a + b + c >= 2). Long story short, a propositional clause is a special kind of pseudo-boolean constraint, so I can process my clauses as if they were regular pseudo-boolean constraints, but this is slower than using dedicated code. Lots of problems only deal with propositional clauses, so they take longer to solve than they should, because I cannot say "this is a problem with only propositional clauses", vs "this is a problem with all kinds of clauses". I could use interfaces, and tried, but this is way slower, to the point it doesn't make sense to use them. And I can't use C++ or rust for this problem: there are already tons of very good c++ SAT solvers, but my goal was to provide a SAT solver I can use in my go programs without resorting to cgo black magic. [1]https://github.com/crillab/gophersat https://github.com/crillab/gophersat
- foldr 7y agoNeat project. Yes, fair enough, I think that is a good example of the kind of code that Go is not particularly suited to (if performance is critical).
- jerf 7y ago"This is way too much in a tight loop. I want my structure to fit in the cache and I want to have as little tests as possible." If that is a hard requirement, then the answer is just that Go isn't a suitable language. AIUI the current generics proposal in Go, it won't fix that problem for you either. Where I disagree with the programming community at large is how often that's actually a hard requirement. A lot of programmers grossly overestimate their performance requirements on the microscale [1], and so concerns about "premature optimization" remain alive and well decades later. However, there are undeniably still a non-trivial number of cases where that really is a hard requirement, in which case the answer is, don't use Go. (Preferably, figure that out before you start. As a fairly heavy Go user, I actually kinda cringe when reading one of the several "We sped up Go by 10x on our particular big thing and got to barely-acceptable performance" articles, rather than consider it something to be celebrated. I mean, cool that you sped it up that much and all, but honestly the correct answer was to notice in the design phase that you were doing something you probably shouldn't have done in Go, modulo concerns about easy prototyping and such.) [1]: I qualify with "microscale" because I also think a lot of programmers underestimate the performance requirements on the macro scale. But that's actually a very different issue. There's a lot of very sluggish and slow code out there in the wild that is architecturally wrong; at every individual step the code is reasonably optimized and fast, the problem is having the wrong steps.