Coder Social home page Coder Social logo

fastcgo's Introduction

fastcgo

Fast Cgo calls using an assembly trampoline.

Supported

Window MacOS Linux
x86_64
ARM64

✅ = Working and tested.
❓ = May work, unknown, no tests.
❌ = Not currently supported.

Warning

This is very unsafe and lets you call C using the current's goroutine thread's system stack internal to Go. It monopolizes the thread to itself and completly bypass the regular scheduler premptions. It also disrupts the GC while blocked inside C code. Use at your own risk.

Why?

Two reason:

  • Workaround for a scheduling issue when the call lasts longer than 20 microseconds which is causing me visible stutter when calling glfwSwapBuffers() with VSync enabled.
  • Bring down the Cgo overhead from 50ns to 3ns (see below).

Benchmark

With go test -run=- -bench=. ./bench:

cpu: Intel(R) Core(TM) i7-7700K CPU @ 4.20GHz
BenchmarkGO-8           1000000000               0.4139 ns/op
BenchmarkCGO-8          23076256                 51.72 ns/op
BenchmarkFastCGO-8      414632949                2.885 ns/op

Example

package main

/*
#include <stdio.h>
void example(int x) {
	printf("Hello %d", x);
}
*/
import "C"
import "github.com/nitrix/fastcgo"

func main() {
	fastcgo.UnsafeCall1(C.example, 42)
}

API

func UnsafeCall1(fn unsafe.Pointer, arg0 uintptr)
func UnsafeCall2(fn unsafe.Pointer, arg0 uintptr, arg1 uintptr)
func UnsafeCall3(fn unsafe.Pointer, arg0 uintptr, arg1 uintptr, arg2 uintptr)
func UnsafeCall4(fn unsafe.Pointer, arg0 uintptr, arg1 uintptr, arg2 uintptr, arg3 uintptr)

func UnsafeCall1Return1(fn unsafe.Pointer, arg0 uintptr) uintptr
func UnsafeCall2Return1(fn unsafe.Pointer, arg0 uintptr, arg1 uintptr) uintptr
func UnsafeCall3Return1(fn unsafe.Pointer, arg0 uintptr, arg1 uintptr, arg2 uintptr) uintptr
func UnsafeCall4Return1(fn unsafe.Pointer, arg0 uintptr, arg1 uintptr, arg2 uintptr, arg3 uintptr) uintptr

License

This is free and unencumbered software released into the public domain. See the UNLICENSE file for more details.

fastcgo's People

Contributors

nitrix avatar

Watchers

 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.