{"id":15684269,"url":"https://github.com/posener/fcontext","last_synced_at":"2025-10-19T08:05:03.295Z","repository":{"id":57481463,"uuid":"188573095","full_name":"posener/fcontext","owner":"posener","description":"Go Context with (pseudo) constant access-time","archived":false,"fork":false,"pushed_at":"2020-04-10T08:06:08.000Z","size":46,"stargazers_count":10,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-31T11:35:34.199Z","etag":null,"topics":["context","go","golang","performance","values"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/posener.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-05-25T14:01:48.000Z","updated_at":"2020-11-03T04:40:28.000Z","dependencies_parsed_at":"2022-09-26T17:50:30.058Z","dependency_job_id":null,"html_url":"https://github.com/posener/fcontext","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/posener%2Ffcontext","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/posener%2Ffcontext/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/posener%2Ffcontext/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/posener%2Ffcontext/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/posener","download_url":"https://codeload.github.com/posener/fcontext/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252910588,"owners_count":21823864,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["context","go","golang","performance","values"],"created_at":"2024-10-03T17:13:47.073Z","updated_at":"2025-10-19T08:05:03.245Z","avatar_url":"https://github.com/posener.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# fcontext\n\n[![codecov](https://codecov.io/gh/posener/fcontext/branch/master/graph/badge.svg)](https://codecov.io/gh/posener/fcontext)\n[![GoDoc](https://img.shields.io/badge/pkg.go.dev-doc-blue)](http://pkg.go.dev/github.com/posener/fcontext)\n\nPackage fcontext provides a fully compatible (pseudo) constant\nvalue access-time alternative to the standard library context\npackage.\n\nThe standard library context provides values access-time which\nis linear with the amount of values that are stored in the\nit. This implementation provides a constant access time in the\nmost common context use case and linear access time for the\nless common use cases (This is why the term 'pseudo' is used).\nPlease see the benchmarks below for details.\nOther parts of the context implementation left untouched.\n\n## Concepts\n\nThe main assumption that is made in this implementation is that\ncontext values tree is mostly grows tall and barely grows wide.\nThis means that the way that the context will mostly be used is\nby adding more values to the existing context:\n\n```go\nctx = context.WithValue(ctx, 1, 1)\nctx = context.WithValue(ctx, 2, 2)\nctx = context.WithValue(ctx, 3, 3)\n```\n\nAnd not creating new branches of the existing context:\n\n```go\nctx1 := context.WithValue(ctx, 1, 1)\nctx2 := context.WithValue(ctx, 2, 2)\nctx3 := context.WithValue(ctx, 3, 3)\n```\n\nThe last form might be more familiar in the following code:\n\n```go\nfunc main() {\n\tctx := context.WithValue(context.Background(), 2, 2)\n\tf(ctx)\n\tf(ctx)\n\t// ...\n}\n\nfunc f(ctx context.Context) {\n\tctx = context.WithValue(2, 2)\n\t// ...\n}\n```\n\nThis implementation will work either way, but will improve the\nperformance of the first pattern significantly.\n\n## Benchmarks\n\nRun the benchmarks with `make bench`. Notice that micro benchmarks\ndo not necessarily represent real world scenarios.\n\nIf you are using a Go version prior to 1.14, Another consideration\nis that using this context will result in an extra goroutine when\nit is converted to a standard cancellable context\n[Go issue](https://github.com/golang/go/issues/28728),\n[Fix](https://go-review.googlesource.com/c/go/+/196521).\n\nResults (On personal machine):\n\n**Access**: Constant access time regardless to the number of stored\nvalues. Compared to the standard library, on the average case, it\nperforms 10%!(NOVERB)better for 10 values, 4 times better for 100 values\nand 35 times better for 1000 values.\n\n**Store**: About 6 times slower and takes about 4 more memory than\nthe standard library context. (Can take up to 8 times if the\ncontext is only grown shallowly).\n\n## Usage\n\nThis library is fully compatible with the standard library context.\n\n```diff\n import (\n-\t\"context\"\n+ \tcontext \"github.com/posener/fcontext\"\n )\n```\n\n---\nReadme created from Go doc with [goreadme](https://github.com/posener/goreadme)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fposener%2Ffcontext","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fposener%2Ffcontext","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fposener%2Ffcontext/lists"}