Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/posener/fcontext
Go Context with (pseudo) constant access-time
https://github.com/posener/fcontext
context go golang performance values
Last synced: 20 days ago
JSON representation
Go Context with (pseudo) constant access-time
- Host: GitHub
- URL: https://github.com/posener/fcontext
- Owner: posener
- License: apache-2.0
- Created: 2019-05-25T14:01:48.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-04-10T08:06:08.000Z (over 4 years ago)
- Last Synced: 2024-06-20T17:48:09.994Z (5 months ago)
- Topics: context, go, golang, performance, values
- Language: Go
- Homepage:
- Size: 44.9 KB
- Stars: 10
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fcontext
[![codecov](https://codecov.io/gh/posener/fcontext/branch/master/graph/badge.svg)](https://codecov.io/gh/posener/fcontext)
[![GoDoc](https://img.shields.io/badge/pkg.go.dev-doc-blue)](http://pkg.go.dev/github.com/posener/fcontext)Package fcontext provides a fully compatible (pseudo) constant
value access-time alternative to the standard library context
package.The standard library context provides values access-time which
is linear with the amount of values that are stored in the
it. This implementation provides a constant access time in the
most common context use case and linear access time for the
less common use cases (This is why the term 'pseudo' is used).
Please see the benchmarks below for details.
Other parts of the context implementation left untouched.## Concepts
The main assumption that is made in this implementation is that
context values tree is mostly grows tall and barely grows wide.
This means that the way that the context will mostly be used is
by adding more values to the existing context:```go
ctx = context.WithValue(ctx, 1, 1)
ctx = context.WithValue(ctx, 2, 2)
ctx = context.WithValue(ctx, 3, 3)
```And not creating new branches of the existing context:
```go
ctx1 := context.WithValue(ctx, 1, 1)
ctx2 := context.WithValue(ctx, 2, 2)
ctx3 := context.WithValue(ctx, 3, 3)
```The last form might be more familiar in the following code:
```go
func main() {
ctx := context.WithValue(context.Background(), 2, 2)
f(ctx)
f(ctx)
// ...
}func f(ctx context.Context) {
ctx = context.WithValue(2, 2)
// ...
}
```This implementation will work either way, but will improve the
performance of the first pattern significantly.## Benchmarks
Run the benchmarks with `make bench`. Notice that micro benchmarks
do not necessarily represent real world scenarios.If you are using a Go version prior to 1.14, Another consideration
is that using this context will result in an extra goroutine when
it is converted to a standard cancellable context
[Go issue](https://github.com/golang/go/issues/28728),
[Fix](https://go-review.googlesource.com/c/go/+/196521).Results (On personal machine):
**Access**: Constant access time regardless to the number of stored
values. Compared to the standard library, on the average case, it
performs 10%!(NOVERB)better for 10 values, 4 times better for 100 values
and 35 times better for 1000 values.**Store**: About 6 times slower and takes about 4 more memory than
the standard library context. (Can take up to 8 times if the
context is only grown shallowly).## Usage
This library is fully compatible with the standard library context.
```diff
import (
- "context"
+ context "github.com/posener/fcontext"
)
```---
Readme created from Go doc with [goreadme](https://github.com/posener/goreadme)