Git Product home page Git Product logo

phony's Introduction

Phony

Go Report Card GoDoc

Phony is a Pony-inspired proof-of-concept implementation of shared-memory actor-model concurrency in the Go programming language. Actors automatically manage goroutines and use asynchronous causal messaging (with backpressure) for communcation. This makes it easy to write programs that are free from deadlocks, goroutine leaks, and many of the for loops over select statements that show up in boilerplate code. The down side is that the code needs to be written in an asynchronous style, which is not idiomatic to Go, so it can take some getting used to.

Benchmarks

goos: linux
goarch: amd64
pkg: github.com/Arceliar/phony
BenchmarkLoopActor-4                	15617646	        71.1 ns/op	      16 B/op	       1 allocs/op
BenchmarkLoopChannel-4              	14870767	        73.0 ns/op	       0 B/op	       0 allocs/op
BenchmarkSendActor-4                	 3268095	       377 ns/op	      32 B/op	       2 allocs/op
BenchmarkSendChannel-4              	 2598151	       442 ns/op	       0 B/op	       0 allocs/op
BenchmarkRequestResponseActor-4     	 2256913	       527 ns/op	      48 B/op	       3 allocs/op
BenchmarkRequestResponseChannel-4   	 1257068	       869 ns/op	       0 B/op	       0 allocs/op
BenchmarkBlock-4                    	  780747	      1586 ns/op	     144 B/op	       3 allocs/op
PASS
ok  	github.com/Arceliar/phony	12.677s

These are microbenchmarks, but they seem to indicate that Actor messaging and goroutine+channel operations have comparable cost. I suspect that the difference is negligible in most applications.

Implementation Details

The code base is short, under 100 source lines of code as of writing, so reading the code is probably the best way to see what it does, but that doesn't necessarily explain why certain design decisions were made. To elaborate on a few things:

  • Phony only depends on packages from the standard library:

    • runtime for some scheduler manipulation (through Goexit() and Gosched()).
    • sync/atomic to implement the Inbox's message queues.
    • unsafe to use atomic's unsafe.Pointer operations, which the paranoid should audit themselves for correctness.
  • Attempts were make to make embedding and composition work:

    • Actor is an interface satisfied by the Inbox struct.
    • The zero value of an Inbox is a fully initialized and ready-to-use Actor
    • This means any struct that anonymously embeds an Inbox is an Actor
    • structs that don't want to export the Actor interface can embed it as a field instead.
  • Inbox was implemented with scalability in mind:

    • The Inbox is basically an atomically updated single-consumer multiple-producer linked list.
    • Pushing a message is wait-free -- no locks, spinlocks, or CompareAndSwap loops.
    • Popping messages is wait-free in the normal case, with a busy loop (LoadPointer) if popping the last message lost a race with a push.
    • When backpressure is required, it's implemented by sending two extra messages (one to the receiver of the original message, and one to the sender).
  • The implementation aims to be as lightweight as reasonably possible:

    • On x86_64, an empty Inbox is 24 bytes, and messages overhead is 16 bytes, or half that on x86.
    • An Actor with an empty Inbox has no goroutine.
    • An Actor that has stopped due to backpressure also has no goroutine.
    • This means that idle Actors can be collected as garbage when they're no longer reachable, just like any other struct.

phony's People

Contributors

arceliar 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.