Git Product home page Git Product logo

loc's Introduction

Documentation Go workflow CircleCI codecov Go Report Card GitHub tag (latest SemVer)

loc

It's a fast, alloc-free and convinient version of runtime.Caller.

Performance benefits are available when using the nikandfor_loc_unsafe build tag. This relies on the internal runtime implementation, which means that older versions of the package may not compile with future versions of Go. Without the tag, it is safe to use with any version of Go.

It was born from tlog.

What is similar

Caller

pc := loc.Caller(1)
ok := pc != 0
name, file, line := pc.NameFileLine()
e := pc.FuncEntry()

// is similar to

pc, file, line, ok := runtime.Caller(1) 
f := runtime.FuncForPC(pc)
name := f.Name()
e := f.Entry()

Callers

pcs := loc.Callers(1, 3)
// or
var pcsbuf [3]loc.PC
pcs := loc.CallersFill(1, pcsbuf[:])

for _, pc := range pcs {
    name, file, file := pc.NameFileLine()
}

// is similar to
var pcbuf [3]uintptr
n := runtime.Callers(2, pcbuf[:])

frames := runtime.CallersFrames(pcbuf[:n])
for {
    frame, more := frames.Next()
    
    _, _, _ = frame.Function, frame.File, frame.Line

    if !more {
        break
    }
}

What is different

Normalized path

loc returns cropped filepath.

github.com/nikandfor/loc/func_test.go

# vs

/home/nik/nikandfor/loc/github.com/nikandfor/loc/func_test.go

Performance

In loc the full cycle (get pc than name, file and line) takes 360=200+160 (200+0 when you repeat) ns whereas runtime takes 690 (640 without func name) + 2 allocs per each frame.

It's up to 3.5x improve.

BenchmarkLocationCaller-8              	 5844801	       201.8 ns/op	       0 B/op	       0 allocs/op
BenchmarkLocationNameFileLine-8        	 7313388	       156.5 ns/op	       0 B/op	       0 allocs/op
BenchmarkRuntimeCallerNameFileLine-8   	 1709940	       689.1 ns/op	     216 B/op	       2 allocs/op
BenchmarkRuntimeCallerFileLine-8       	 1917613	       642.1 ns/op	     216 B/op	       2 allocs/op

loc's People

Contributors

nikandfor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.