Rob Pike [Fri, 24 May 2013 22:49:26 +0000 (15:49 -0700)]
fmt.Printf: introduce notation for random access to arguments.
This text is added to doc.go:
Explicit argument indexes:
In Printf, Sprintf, and Fprintf, the default behavior is for each
formatting verb to format successive arguments passed in the call.
However, the notation [n] immediately before the verb indicates that the
nth one-indexed argument is to be formatted instead. The same notation
before a '*' for a width or precision selects the argument index holding
the value. After processing a bracketed expression [n], arguments n+1,
n+2, etc. will be processed unless otherwise directed.
For example,
fmt.Sprintf("%[2]d %[1]d\n", 11, 22)
will yield "22, 11", while
fmt.Sprintf("%[3]*[2].*[1]f", 12.0, 2, 6),
equivalent to
fmt.Sprintf("%6.2f", 12.0),
will yield " 12.00". Because an explicit index affects subsequent verbs,
this notation can be used to print the same values multiple times
by resetting the index for the first argument to be repeated:
fmt.Sprintf("%d %d %#[1]x %#x", 16, 17)
will yield "16 17 0x10 0x11".
The notation chosen differs from that in C, but I believe it's easier to read
and to remember (we're indexing the arguments), and compatibility with
C's printf was never a strong goal anyway.
While we're here, change the word "field" to "arg" or "argument" in the
code; it was being misused and was confusing.
David du Colombier [Fri, 24 May 2013 20:55:19 +0000 (13:55 -0700)]
build: fix make.rc on Plan 9
Set $status as null to prevent rc from exiting
on the last --no-banner argument checking when
used with rc -e. It allows all.rc to not exit
before executing run.rc
Dmitriy Vyukov [Fri, 24 May 2013 14:35:48 +0000 (18:35 +0400)]
log/syslog: fix deadlock in test
The problem was that server handlers block on done<-,
the goroutine that reads from done blocks on count<-,
and the main goroutine that is supposed to read from count
waits for server handlers to exit.
Fixes #5547.
Dominik Honnef [Thu, 23 May 2013 17:19:03 +0000 (10:19 -0700)]
misc/emacs: Do not modify kill ring when programmatically deleting text
Operations like gofmt and go-remove-unused-imports delete entire
lines of text. Previously this put them on the kill-ring,
negatively affecting user experience.
Daniel Morsing [Thu, 23 May 2013 16:29:19 +0000 (18:29 +0200)]
io: Prioritize WriterTos over ReaderFroms in Copy.
This only affects calls where both ReaderFrom and WriterTo are implemented. WriterTo can issue one large write, while ReaderFrom must Read until EOF, potentially reallocating when out of memory. With one large Write, the Writer only needs to allocate once.
This also helps in ioutil.Discard since we can avoid copying memory when the Reader implements WriterTo.
Dmitriy Vyukov [Wed, 22 May 2013 19:04:46 +0000 (23:04 +0400)]
runtime: properly synchronize GC and finalizer goroutine
This is needed for preemptive scheduler, because the goroutine
can be preempted at surprising points.
Dmitriy Vyukov [Wed, 22 May 2013 18:57:47 +0000 (22:57 +0400)]
runtime: detect deadlocks in programs using cgo
When cgo is used, runtime creates an additional M to handle callbacks on threads not created by Go.
This effectively disabled deadlock detection, which is a right thing, because Go program can be blocked
and only serve callbacks on external threads.
This also disables deadlock detection under race detector, because it happens to use cgo.
With this change the additional M is created lazily on first cgo call. So deadlock detector
works for programs that import "C", "net" or "net/http/pprof" but do not use them in fact.
Also fixes deadlock detector under race detector.
It should be fine to create the M later, because C code can not call into Go before first cgo call,
because C code does not know when Go initialization has completed. So a Go program need to call into C
first either to create an external thread, or notify a thread created in global ctor that Go
initialization has completed.
Fixes #4973.
Fixes #5475.
Dmitriy Vyukov [Wed, 22 May 2013 18:22:57 +0000 (22:22 +0400)]
runtime: fix and speedup malloc stats
Currently per-sizeclass stats are lost for destroyed MCache's. This patch fixes this.
Also, only update mstats.heap_alloc on heap operations, because that's the only
stat that needs to be promptly updated. Everything else needs to be up-to-date only in ReadMemStats().
Dmitriy Vyukov [Wed, 22 May 2013 09:29:17 +0000 (13:29 +0400)]
runtime: simplify MCache
The nlistmin/size thresholds are copied from tcmalloc,
but are unnecesary for Go malloc. We do not do explicit
frees into MCache. For sparse cases when we do (mainly hashmap),
simpler logic will do.
Brad Fitzpatrick [Tue, 21 May 2013 22:51:49 +0000 (15:51 -0700)]
bufio: reuse Writer buffers after Flush
A bufio.Writer.Flush marks the usual end of a Writer's
life. Recycle its internal buffer on those explicit flushes,
but not on normal, as-needed internal flushes.
benchmark old ns/op new ns/op delta
BenchmarkWriterEmpty 1959 727 -62.89%
benchmark old allocs new allocs delta
BenchmarkWriterEmpty 2 1 -50.00%
benchmark old bytes new bytes delta
BenchmarkWriterEmpty 4215 83 -98.03%
Adam Langley [Tue, 21 May 2013 14:47:31 +0000 (10:47 -0400)]
crypto/tls: don't send NPN extension if NextProtos is not set.
This isn't clearly a bug on Go's part, but it triggers a bug in Firefox
which means that crypto/tls and net/http cannot be wired up together
unless NextProtos includes "http/1.1". When net/http sets up the
tls.Config, it does this and so works fine. But anyone setting up the
tls.Config themselves will hit the Firefox bug.
Fixes #5445.
R=golang-dev, bradfitz, r
CC=golang-dev
https://golang.org/cl/9539045
Robert Griesemer [Mon, 20 May 2013 21:01:07 +0000 (14:01 -0700)]
spec: removed old or invalid TODOs
Several old TODOs are either resolved now (e.g. when is a return
needed), or are from a time the language wasn't frozen (^ for uints
only). Consolidated the others.
R=golang-dev, r
CC=golang-dev
https://golang.org/cl/9599044
Adam Langley [Mon, 20 May 2013 18:20:26 +0000 (14:20 -0400)]
crypto/x509: provide better error messages for X.509 verify failures.
Failures caused by errors like invalid signatures or missing hash
functions cause rather generic, unhelpful error messages because no
trust chain can be constructed: "x509: certificate signed by unknown
authority."
With this change, authority errors may contain the reason why an
arbitary candidate step in the chain was rejected. For example, in the
event of a missing hash function the error looks like:
x509: certificate signed by unknown authority (possibly because of
"crypto/x509: cannot verify signature: algorithm unimplemented" while
trying to verify candidate authority certificate 'Thawte SGC CA')
Fixes 5058.
R=golang-dev, r
CC=golang-dev
https://golang.org/cl/9104051
Keith Randall [Fri, 17 May 2013 19:53:49 +0000 (12:53 -0700)]
runtime: faster x86 memmove (a.k.a. built-in copy())
REP instructions have a high startup cost, so we handle small
sizes with some straightline code. The REP MOVSx instructions
are really fast for large sizes. The cutover is approximately
1K. We implement up to 128/256 because that is the maximum
SSE register load (loading all data into registers before any
stores lets us ignore copy direction).
Carl Shapiro [Thu, 16 May 2013 17:42:39 +0000 (10:42 -0700)]
runtime: fix scanning of not started goroutines
The stack scanner for not started goroutines ignored the arguments
area when its size was unknown. With this change, the distance
between the stack pointer and the stack base will be used instead.
Adam Langley [Thu, 16 May 2013 16:29:23 +0000 (12:29 -0400)]
crypto/tls: fix flakey test.
A test added in b37d2fdcc4d9 didn't work with some values of GOMAXPROCS
because the defer statements were in the wrong order: the Pipe could be
closed before the TLS Client was.
Dmitriy Vyukov [Wed, 15 May 2013 19:50:32 +0000 (23:50 +0400)]
runtime: fix GC scanning of slices
If a slice points to an array embedded in a struct,
the whole struct can be incorrectly scanned as the slice buffer.
Fixes #5443.
Dmitriy Vyukov [Wed, 15 May 2013 17:22:32 +0000 (21:22 +0400)]
runtime: add simple malloc benchmarks
Allocs of size 16 can bypass atomic set of the allocated bit, while allocs of size 8 can not.
Allocs with and w/o type info hit different paths inside of malloc.
Current results on linux/amd64:
BenchmarkMalloc8 50000000 43.6 ns/op
BenchmarkMalloc16 50000000 46.7 ns/op
BenchmarkMallocTypeInfo8 50000000 61.3 ns/op
BenchmarkMallocTypeInfo16 50000000 63.5 ns/op
Dmitriy Vyukov [Wed, 15 May 2013 14:35:05 +0000 (18:35 +0400)]
runtime: transfer whole span from MCentral to MCache
Finer-grained transfers were relevant with per-M caches,
with per-P caches they are not relevant and harmful for performance.
For few small size classes where it makes difference,
it's fine to grab the whole span (4K).
benchmark old ns/op new ns/op delta
BenchmarkMalloc 42 40 -4.45%
Adam Langley [Wed, 15 May 2013 14:27:34 +0000 (10:27 -0400)]
crypto/rsa: check for minimal PKCS#1 v1.5 padding.
The PKCS#1 spec requires that the PS padding in an RSA message be at
least 8 bytes long. We were not previously checking this. This isn't
important in the most common situation (session key encryption), but
the impact is unclear in other cases.
Adam Langley [Wed, 15 May 2013 14:25:54 +0000 (10:25 -0400)]
crypto/tls: ignore empty TLS records.
OpenSSL can be configured to send empty records in order to randomise
the CBC IV. This is an early version of 1/n-1 record splitting (that Go
does) and is quite reasonable, but it results in tls.Conn.Read
returning (0, nil).
This change ignores up to 100 consecutive, empty records to avoid
returning (0, nil) to callers.
Adam Langley [Wed, 15 May 2013 14:03:22 +0000 (10:03 -0400)]
math/big: save some copies in binaryGCD.
This patch resulted from a bit of quick optimisation in response to a
golang-nuts post. It looks like one could save a couple other copies in
this function, but this addresses the inner loop and is fairly simple.