Skip to content

Commit e8018fb

Browse files
committed
runtime: rewrite map size test
I don't know why the memstats code is flaky. TBR=bradfitz CC=golang-dev https://golang.org/cl/12160043
1 parent 27032fd commit e8018fb

File tree

2 files changed

+3
-41
lines changed

2 files changed

+3
-41
lines changed

src/pkg/runtime/hashmap.c

+3
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,9 @@ runtime·makemap_c(MapType *typ, int64 hint)
11121112
Type *key;
11131113

11141114
key = typ->key;
1115+
1116+
if(sizeof(Hmap) > 48)
1117+
runtime·panicstring("hmap too large");
11151118

11161119
if(hint < 0 || (int32)hint != hint)
11171120
runtime·panicstring("makemap: size out of range");

src/pkg/runtime/map_test.go

-41
Original file line numberDiff line numberDiff line change
@@ -371,44 +371,3 @@ func testMapLookups(t *testing.T, m map[string]string) {
371371
}
372372
}
373373
}
374-
375-
func TestMapSize(t *testing.T) {
376-
if runtime.GOMAXPROCS(-1) != 1 {
377-
t.Skip("gomaxprocs > 1 - not accurate")
378-
}
379-
var m map[struct{}]struct{}
380-
size := bytesPerRun(100, func() {
381-
m = make(map[struct{}]struct{})
382-
})
383-
if size > 48 {
384-
t.Errorf("size = %v; want <= 48", size)
385-
}
386-
}
387-
388-
// like testing.AllocsPerRun, but for bytes of memory, not number of allocations.
389-
func bytesPerRun(runs int, f func()) (avg float64) {
390-
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
391-
392-
// Warm up the function
393-
f()
394-
395-
// Measure the starting statistics
396-
var memstats runtime.MemStats
397-
runtime.ReadMemStats(&memstats)
398-
sum := 0 - memstats.Alloc
399-
400-
// Run the function the specified number of times
401-
for i := 0; i < runs; i++ {
402-
f()
403-
}
404-
405-
// Read the final statistics
406-
runtime.ReadMemStats(&memstats)
407-
sum += memstats.Alloc
408-
409-
// Average the mallocs over the runs (not counting the warm-up).
410-
// We are forced to return a float64 because the API is silly, but do
411-
// the division as integers so we can ask if AllocsPerRun()==1
412-
// instead of AllocsPerRun()<2.
413-
return float64(sum / uint64(runs))
414-
}

0 commit comments

Comments
 (0)