News for hackers. Stuff that matters.
Re:Practices for writing high-performance Go by adrianN on Monday May 06, 2019 @11:50AM (#19840489)
Concurrency is a bit easier in Go than in C++.
Re:Practices for writing high-performance Go by shereadsthenews on Monday May 06, 2019 @12:20PM (#19840845)
Disagree. I can write a server in C++ that, with careful thought and a bit of planning, will be able to exploit the resources of the kind of 88-core dual-socket machines that are mainstream today. No amount of planning will allow me to write a Go program that does the same thing on the same machine. Small amounts of concurrency might seem easy in Go but lots of concurrency is hard.
Re:Practices for writing high-performance Go by jerf on Monday May 06, 2019 @02:51PM (#19842535)
"I can write a server in C++ that, with careful thought and a bit of planning, will be able to exploit the resources of the kind of 88-core dual-socket machines that are mainstream today."

This is downvoted grey as I write this, and as someone who has been called a "Go shill" on occasion... it's exactly right. Go is a decent language for writing code in a fairly straightforward manner and getting pretty good performance out of it, but if you need to squeeze every bit of performance out of your hardware, it's a bad choice. (I can give you choices that are worse by an order of magnitude, or even more in some cases, but it's still a bad choice.) You have a fairly smooth optimization ride up to 1.5-2x slower than C for most use cases, a few pathological edge cases where it's grossly worse (many clustered around these "every drop of performance" problems!) and a few where it'll reach parity, and then you're going to hit a brick wall.

An 88 core system is probably not impossible to sensibly use with Go, but you're going to be more constrained. I'd imagine it can probably serve web requests really well, but it's more likely to hit pathological cases if you hammer certain global resources.

Arguably, precisely part of the point of Go was that C++ makes you pay for that level of performance in code complexity and cognitive overhead all the time, even when you don't remotely need it. (I can't prove this, but I'd guess the median "cloud service" is grotesquely overprovisioned on the smallest AWS instance. The "cloud services" that leap to mind are things like the AWS auth servers or Netflix content servers or the Google crawling or indexing servers, but while those are huge and important, they're also in many dimensions the exceptions. A good chunk of the popularity of "serverless" is probably a result of this.) When you do need every bit of performance, though, the list of viable options is short.

Re:Practices for writing high-performance Go by chessturk on Monday May 06, 2019 @07:33PM (#19844991)
As a fellow gopher, I agree, Go isn't trying to compete with C/C++ on performance, it's trying to give you safety and ease of use of high level language.

I think the point about concurrency typifies this, channels aren't as fast as mutexs and semaphores, but it makes sharing code with co workers easier to reason about.

If you're in a domain where performance is still king, Go isn't trying to find a place there.

Re:Practices for writing high-performance Go by weberc2 on Tuesday May 07, 2019 @09:14AM (#19849012)
Presumably he was downvoted for responding to “concurrency in Go is easier” with “Disagree. I can get every bit of performance out of an 88 core CPU with C++”. It’s not a coherent counter argument; no one claimed Go was as efficient as C++, only that concurrency is easier.
Re:Practices for writing high-performance Go by fouc on Tuesday May 07, 2019 @06:04AM (#19847962)
How would Rust compare?
Re:Practices for writing high-performance Go by galangalalgol on Tuesday May 07, 2019 @06:49AM (#19848199)
The benchmark game puts c, rust, and c++, in that order, roughly on par in performance, with go being about 2-3x slower. No idea if that's accurate. Sampling bias means people who like performance are optimizing the languages used for performance, and people who just like to get something working quit after the first benchmark in go or python is finished. https://benchmarksgame-team.pages.debian.net/benchmarksgame/...
Re:Practices for writing high-performance Go by v_lisivka on Tuesday May 07, 2019 @10:01AM (#19849384)
If you discard regexredux, then Rust is faster than C and C++ at average: see average bar at "How many times slower graph" [1].

regexredux program is outlier in Rust, because replacement of a regex in string is slower in regex crate, because author of regex crate chose to implement safer, but slower algorithm. To fix this, regex crate must be updated or replaced. I spent two weekends on this.

[1]: https://benchmarksgame-team.pages.debian.net/benchmarksgame/...

Re:Practices for writing high-performance Go by igouy on Wednesday May 08, 2019 @02:01AM (#19856242)
> To fix this, regex crate must be updated or replaced.

I somehow doubt pcre2 is being changed to make the tiny toy C programs run better.

> I spent two weekends on this.

So shouldn't we assume the program performance simply reflects all-those-hours you've spent working on it?

Re:Practices for writing high-performance Go by v_lisivka on Wednesday May 08, 2019 @05:21AM (#19857179)
Look at the program:

  fn find_replaced_sequence_length(sequence: String) -> usize {
    // Replace the following patterns, one at a time:
    let substs = vec![
        ("tHa[Nt]", "<4>"),
        ("aND|caN|Ha[DS]|WaS", "<3>"),
        ("a[NSt]|BY", "<2>"),
        ("<[^>]*>", "|"),
        ("\\|[^|][^|]*\\|", "-"),
    ];
  
    // Perform the replacements in the sequence:
    substs
        .iter()
        .fold(sequence, |s, (re, replacement)| {
            regex(re)
                .replace_all(&s, NoExpand(replacement)).into_owned()
        }).len()
  }

It measures performance of RE engine. I can switch from regex crate to PCRE2, and program performance will match C.
Re:Practices for writing high-performance Go by igouy on Wednesday May 08, 2019 @11:12AM (#19859555)
> I can switch from regex crate to PCRE2, and program performance will match C.

Perhaps it would; those measurements have not been made.

What does that have to do with re-writing libraries to make tiny toy programs run better?

What does that have to do with program performance being a proxy for programmer effort?

Re:Practices for writing high-performance Go by igouy on Tuesday May 07, 2019 @12:07PM (#19850777)
mandelbrot is outlier in Rust, because… :-)
Re:Practices for writing high-performance Go by v_lisivka on Tuesday May 07, 2019 @04:10PM (#19852997)
Because hardware acceleration is used in C and C++ versions. I will fix this soon.
Re:Practices for writing high-performance Go by igouy on Friday May 10, 2019 @01:59PM (#19880449)
> Sampling bias means people who like performance are optimizing the languages used for performance …

Also, there might be a something to prove "bias" :-)

Re:Practices for writing high-performance Go by galangalalgol on Tuesday May 07, 2019 @07:04AM (#19848286)
Also, Dropbox rewrote the core stuff from go to rust when they needed more performance. So that is one example/anecdote.
Re:Practices for writing high-performance Go by v_lisivka on Tuesday May 07, 2019 @09:36AM (#19849152)
Concurrency in Rust is extremely easy, because it safe by design. Just import rayon crate and change iter() to par_iter() [1]. Compiler will point out to problems, e.g. it will not allow to send a type, which cannot be used concurrently, until it will be wrapped by Arc (atomic reference counter).

[1]: https://docs.rs/rayon/1.0.3/rayon/

Re:Practices for writing high-performance Go by Thaxll on Monday May 06, 2019 @01:01PM (#19841365)
You're talking a about a very specific scenario, out of the box Go is off course easier than C++ for concurrency. Who by the way runs applications on a 88 core server, why would you so they instead of splitting that into smaller chunks.
Re:Practices for writing high-performance Go by derefr on Monday May 06, 2019 @01:25PM (#19841656)
> Who by the way runs applications on a 88 core server, why would you so they instead of splitting that into smaller chunks.

Graph analysis and/or routing, for one. Distributed Dijkstra is pretty impractical (the optimal lower bound on the number of messages required equals the number of edges in the graph!)

For example, I don't work for Google, but I can pretty much guarantee you that an individual Google Maps routing query runs on a single, extremely-high-memory (but not NUMA!) node, which has likely been optimized for speed using plain-old threading on a high-core-count CPU, with the upper bound on the number of useful cores being the system architecture's per-socket memory bandwidth.

Re:Practices for writing high-performance Go by kasey_junk on Monday May 06, 2019 @05:41PM (#19844197)
Where do you get high core count computers that aren’t numa?
Re:Practices for writing high-performance Go by Thaxll on Monday May 06, 2019 @03:02PM (#19842617)
I highly doubt Google runs any workload on Borg with CPU count > 24. I don't work there, so I'm just assuming but it goes against all modern scaling / deployments patterns.
Re:Practices for writing high-performance Go by shereadsthenews on Monday May 06, 2019 @05:31PM (#19844099)
There are various workloads that occupy entire machines, they are all very carefully written by dedicated performance experts, in C++.
Re:Practices for writing high-performance Go by foobiekr on Monday May 06, 2019 @02:20PM (#19842223)
I would be very, very curious if this is actually true. I agree with the intuition behind the statement but I am actually curious how it is done.
Re:Practices for writing high-performance Go by jefftk on Monday May 06, 2019 @10:25PM (#19845874)
I think you're ignoring caching/preprocessing in your analysis.
Re:Practices for writing high-performance Go by derefr on Tuesday May 07, 2019 @10:02AM (#19849388)
I am, yeah; but it’s an infinite regress—if these aren’t the specs of the machine that answers your query, then they’re the specs of the machine that does the spec-work to build the cache.
Re:Practices for writing high-performance Go by erik_seaberg on Tuesday May 07, 2019 @12:48PM (#19851163)
Or the machine that designs the machine that answers your query.
Re:Practices for writing high-performance Go by nickpsecurity on Tuesday May 07, 2019 @11:20AM (#19850307)
Anyone doing scale up in the HPC space. It's a massive market. This link has a picture in it showing the industries that buy that stuff:

https://www.nextplatform.com/2017/06/22/casing-hpc-market-ha...

Although they cost a ton, there were at least two reasons to like NUMA machines:

1. You program them much like multithreaded machines instead of message passing like MPI. You do need to account for locality. There's OS, library, and documentation support for handling that, though. Porting a multithreaded library to NUMA is much smaller problem than clustering it.

2. A massive amount of memory with lower-latency access than on clusters. If your data is in memory, it's much faster than if it's being moved in and out of memory. Then, when it is moved, it's moved faster. Low-latency reduces the damage of many smaller copies, too.

For such reasons, I always wanted a SGI or Cray machine. Modern, multi-core machines with plenty of RAM are good enough for most of my purposes, though. I do plan to do some model-checking of software in the near future. The amount of RAM used grows exponentially or something like that with the size of the program. Small ones already use GB of RAM in the analyses. Some are getting parallelized a bit. Obviously, 88 cores with 100+GB of RAM could be pretty useful if handling programs twice or three times as large. :)

Btw, there are also languages designed specifically to take advantage of parallelism in many HPC situations. Like Go, they were intended to let you describe the algorithms in a high-level way with the compiler synthesizing efficient implementations for everything from multi-cores to NUMA to clusters. That's the theory. The best one from my prior research was Chapel. Then, there's simpler ones for stuff like data parallel with Cilk being an example.

https://chapel-lang.org/

https://en.wikipedia.org/wiki/Cilk

ParaSail was a recent one with interesting design. I'm not sure what its current status is in terms of usability.

https://www.embedded.com/design/programming-languages-and-to...

Re:Practices for writing high-performance Go by shereadsthenews on Monday May 06, 2019 @01:16PM (#19841564)
You're making a different argument, that one does not need so much vertical scalability, and I might agree, but that doesn't negate the fact that Go has limits of vertical scalability that C++ doesn't face.
Re:Practices for writing high-performance Go by weberc2 on Tuesday May 07, 2019 @09:17AM (#19849035)
The original claim was that concurrency is easier in Go, not that Go has a higher performance ceiling. You are the one making the different argument. :)
Re:Practices for writing high-performance Go by lenkite on Monday May 06, 2019 @04:09PM (#19843268)
Could you describe how you would go about designing this and what libraries/frameworks would you use ? AFAIK co-routines is only coming with C++ 20, so I guess you would still use std::thread right ?
Re:Practices for writing high-performance Go by alexhutcheson on Monday May 06, 2019 @05:56PM (#19844327)
std::thread has only been around since C++11, so most existing HPC codebases probably either use pthreads[1] directly, or an organization- or project-specific library wrapping pthreads. Some used boost::Thread, which was effectively the predecessor of std::thread.

If you want to saturate every core on a very parallel problem like "handle as many packets as you can", a very rough first approach would be to spin up 88 threads (one per core). Within each thread, you could use either something like boost::Fiber[2] (similar to goroutines, except mapped N:1 to OS threads, rather than M:N) to avoid blocking the threads on IO. This paper[3] has a good overview of different concurrency approaches.

If you're doing something that's not embarrassingly parallel[4], then often there is a well-researched approach for your specific domain. The same general ideas apply to keeping your cores busy, but you're often more bounded by communication between threads, memory bandwidth, etc.

[1] https://en.wikipedia.org/wiki/POSIX_Threads

[2] https://www.boost.org/doc/libs/1_70_0/libs/fiber/doc/html/fi...

[3] http://www.sosp.org/2001/papers/welsh.pdf

[4] https://en.wikipedia.org/wiki/Embarrassingly_parallel

Re:Practices for writing high-performance Go by cdelsolar on Monday May 06, 2019 @12:35PM (#19841028)
Where's the evidence that Go can't handle large amounts of concurrency?
Re:Practices for writing high-performance Go by shereadsthenews on Monday May 06, 2019 @12:51PM (#19841228)
All over my profiler. Go can deal with something embarrassingly parallel, same as any other language, but try getting it to scale up to high core counts with small RPCs is impossible, for the reasons the OP mentioned: you can't avoid its global allocator and you don't control the scheduler either. You will spend all of your CPU time in runtime.findrunnable and runtime.mallocgc.
Re:Practices for writing high-performance Go by stcredzero on Monday May 06, 2019 @01:18PM (#19841580)
You will spend all of your CPU time in runtime.findrunnable

Profile your channel latency, then design around that. "Small RPCs?" With an 88 core server, should you be making that many remote procedure calls? Find the 20% most intensive code and take that out of the hands of the scheduler, as much as possible.

and runtime.mallocgc

Even with sync.Pool? Can you design those parts of the system to mostly use the stack?

Re:Practices for writing high-performance Go by kasey_junk on Monday May 06, 2019 @05:57PM (#19844336)
Not for nothing but channels become a bottleneck pretty early when you start doing highly concurrent golang.

You can very carefully design around it using only channels but it quickly starts making more sense to just use a different concurrency abstraction.

Re:Practices for writing high-performance Go by chessturk on Monday May 06, 2019 @10:11PM (#19845792)
My experience has been channels are a really good way to make highly concurrent code readable for people who hate concurrency (ymmv).

They aren't as efficient, but using them as a semaphore (i.e. only signaling) can facilitate fairly-efficient, easier-to-read shared memory. But I'm kind of proving the whole...

> You can very carefully design around it only channels

...part of your post.

Re:Practices for writing high-performance Go by weberc2 on Monday May 06, 2019 @01:33PM (#19841743)
I guarantee you the "little bit of planning" you're doing in C++ is far greater than the equivalent planning in Go to avoid the global allocator in your goroutines (i.e., allocate on the stack, use object pools, etc). I'm sure you still won't get to the same performance ceiling that C++ allows, but it doesn't mean "Go is bad at concurrency".
Re:Practices for writing high-performance Go by chessturk on Monday May 06, 2019 @10:35PM (#19845918)
The philosophy of Go, as far as I can tell, is opinionated simplicity. In this way, it is fantastic at concurrency.

It gives you a simplified fork in goroutines, a way to safely pass by value or signal between these through channels, and structures like select to psuedo randomly deal with race conditions.

That stuff is all awesome!

But when you are talking about performance, opinionated simplicity is a bad philosophy. You are going to want the finer grained control the poster is asking for, and which Go deliberately doesn't allow for. C, C++, and Rust will always have a speed advantage because they lack the overhead of the GC and were designed with allowing the developer minute control of the system, which is the space to make such optimizations.

That said, opininated simplicity means every Go codebase I drop into looks roughly the same. That is not true in the above langauges, precisely because they allow for more control.

To me, Go's ideal use case is the domains of Java, Python, NodeJS, and even Elixir -- where it has a performance edge in many cases and where its consistant style really shine.

All that to say, Go is bad at performance optimized concurrency, as designed.

Re:Practices for writing high-performance Go by weberc2 on Tuesday May 07, 2019 @09:25AM (#19849089)
Your post doesn’t address mine. I gave specific advice for addressing the GP’s issue. It’s much easier to move your allocs out of the hot path in Go than it is to do virtually anything in C++. I’m not claiming Go will compete with C++ on performance, only that his argument isn’t a good reason to write off Go. (I’ve written a lot of both languages).
Re:Practices for writing high-performance Go by networkimprov on Monday May 06, 2019 @01:21PM (#19841612)
Is there a Github issue for this problem? If not, would you consider filing one?

https://github.com/golang/go/issues

It should probably explain why one cannot "avoid its global allocator" by using pools and/or stack objects.

Re:Practices for writing high-performance Go by shereadsthenews on Monday May 06, 2019 @01:39PM (#19841819)
Stack is great, then you will spend more time in runtime.morestack because the Go authors have wisely decided that we are all too dumb to request specific stack sizes and we must rely on copying and doubling stacks larger than 2KiB even if we know in advance that 2KiB is not enough. This compares poorly with native threads where the stacks are dynamically allocated one page at a time without copying and we can specify the stack size _limit_ per thread if we need to. This is another place where the Go authors just arbitrarily decided that users cannot be trusted.

sync.Pool is fun but it has known scalability limits that are yet to be addressed in a released runtime (see https://go-review.googlesource.com/c/go/+/166960/ for example). You also can't use sync.Pool for anything that you need to be non-ephemeral, like CPU-local counters, because the GC can just blow them away at any time and the finalizers run at arbitrary future times or possibly never.

Re:Practices for writing high-performance Go by lepage on Tuesday May 07, 2019 @09:03AM (#19848958)
The runtime.morestack issue is actually being tracked btw [0]. I'm also getting the impression that you want Go to be another C++, but I don't understand why.

All of your listed issues have additional details and discussions around them. It not "we don't add this stuff because you are too dumb to use it". It's more "we don't know how to add this currently so it would properly interact with other features and be non-confusing". There are some exceptions to this rule (time.Time issue comes to mind) but overall they tend to listen to the developer base. Also, keep in mind that there are also relatively few Core Go developers, so they have to be extremely careful about what they gonna add in the language. It's going to be them who is going to support it in years to come.

As for C++ - yes, nothing beats it in highly optimized cases. And there is nothing wrong with that. The problem is not only writing C++ for those highly optimized cases is an extremely hard task by itself. The problem is finding people who can actually do it with the resulting code which is going to be supportable in the future (I'm not talking about UB, races or memory leaks).

[0]: https://github.com/golang/go/issues/18138

Edit: wording

Re:Practices for writing high-performance Go by networkimprov on Monday May 06, 2019 @02:52PM (#19842540)
My point is that you should file a Github issue if there isn't one.
Re:Practices for writing high-performance Go by icebraining on Tuesday May 07, 2019 @08:20AM (#19848725)
Why "should"? Nobody owes the writing of issues.
Re:Practices for writing high-performance Go by networkimprov on Tuesday May 07, 2019 @02:23PM (#19852064)
If you depend on an open source project, and can articulate a problem with it that can be fixed, then I do feel you should file an issue to contribute your insight.