Source file test/fixedbugs/issue75764.go

     1  // run
     2  
     3  // Copyright 2026 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  // TODO: disable test for ppc64le/dynlink? See cmd/compile/internal/reader/noder.go:addTailCall.
     8  
     9  package main
    10  
    11  import (
    12  	"fmt"
    13  	"runtime"
    14  	"time"
    15  )
    16  
    17  type I interface {
    18  	foo() time.Duration // foo returns its running time
    19  }
    20  
    21  type base struct {
    22  }
    23  
    24  func (b *base) foo() time.Duration {
    25  	t := time.Now()
    26  	var pcs [10]uintptr
    27  	runtime.Callers(1, pcs[:])
    28  	return time.Since(t)
    29  }
    30  
    31  type wrap struct {
    32  	I
    33  	data int
    34  }
    35  
    36  // best runs f a bunch of times, picks the shortest returned duration.
    37  func best(f func() time.Duration) time.Duration {
    38  	m := f()
    39  	for range 9 {
    40  		m = min(m, f())
    41  	}
    42  	return m
    43  }
    44  
    45  func main() {
    46  	if runtime.GOARCH == "wasm" {
    47  		// TODO: upgrade wasm to do indirect tail calls
    48  		return
    49  	}
    50  	var i I = &base{}
    51  	for x := range 1000 {
    52  		i = &wrap{I: i, data: x}
    53  	}
    54  	short := best(i.foo)
    55  	for x := range 9000 {
    56  		i = &wrap{I: i, data: x}
    57  	}
    58  	long := best(i.foo)
    59  
    60  	ratio := long.Seconds() / short.Seconds()
    61  
    62  	// Running time should be independent of the number of wrappers.
    63  	// Prior to the fix for 75764, it was linear in the number of wrappers.
    64  	// Pre-fix, we get ratios typically in the 7.0-10.0 range.
    65  	// Post-fix, it is in the 1.0-1.5 range.
    66  	allowed := 5.0
    67  	if ratio >= allowed {
    68  		fmt.Printf("short: %v\nlong: %v\nratio: %v\nallowed: %v\n", short, long, ratio, allowed)
    69  	}
    70  }
    71  

View as plain text