Go to file
Emmanuel T Odeke 5399e72f32
perf: store/cachekv: avoid a map lookup if unnecessary, clear maps fast (#10486)
We can shave off some milliseconds, but also cut down some Megabytes of
RAM consumed by only requesting from the cache if needed, but also using
the map clearing idiom which is recognized by the compiler to make fast
code.

Noticed in profiles from Tharsis' Ethermint per https://github.com/tharsis/ethermint/issues/710

- Before
* Memory profiles
```shell
   19.50MB    19.50MB    134:	store.cache = make(map[string]*cValue)
   18.50MB    18.50MB    135:	store.deleted = make(map[string]struct{})
   15.50MB    15.50MB    136:	store.unsortedCache = make(map[string]struct{})
```

* CPU profiles
```go
         .          .    118:	// TODO: Consider allowing usage of Batch, which would allow the write to
         .          .    119:	// at least happen atomically.
     150ms      150ms    120:	for _, key := range keys {
     220ms      3.64s    121:		cacheValue := store.cache[key]
         .          .    122:
         .          .    123:		switch {
         .      250ms    124:		case store.isDeleted(key):
         .          .    125:			store.parent.Delete([]byte(key))
     210ms      210ms    126:		case cacheValue.value == nil:
         .          .    127:			// Skip, it already doesn't exist in parent.
         .          .    128:		default:
     240ms     27.94s    129:			store.parent.Set([]byte(key), cacheValue.value)
         .          .    130:		}
         .          .    131:	}

...

      10ms       60ms    134:	store.cache = make(map[string]*cValue)
         .       40ms    135:	store.deleted = make(map[string]struct{})
         .       50ms    136:	store.unsortedCache = make(map[string]struct{})
         .      110ms    137:	store.sortedCache = dbm.NewMemDB()
```

- After
* Memory profiles
```shell
         .          .    130:	// Clear the cache using the map clearing idiom
         .          .    131:	// and not allocating fresh objects.
         .          .    132:	// Please see https://bencher.orijtech.com/perfclinic/mapclearing/
         .          .    133:	for key := range store.cache {
         .          .    134:		delete(store.cache, key)
         .          .    135:	}
         .          .    136:	for key := range store.deleted {
         .          .    137:		delete(store.deleted, key)
         .          .    138:	}
         .          .    139:	for key := range store.unsortedCache {
         .          .    140:		delete(store.unsortedCache, key)
         .          .    141:	}
```

* CPU profiles
```shell
         .          .    111:	// TODO: Consider allowing usage of Batch, which would allow the write to
         .          .    112:	// at least happen atomically.
     110ms      110ms    113:	for _, key := range keys {
         .      210ms    114:		if store.isDeleted(key) {
         .          .    115:			// We use []byte(key) instead of conv.UnsafeStrToBytes because we cannot
         .          .    116:			// be sure if the underlying store might do a save with the byteslice or
         .          .    117:			// not. Once we get confirmation that .Delete is guaranteed not to
         .          .    118:			// save the byteslice, then we can assume only a read-only copy is sufficient.
         .          .    119:			store.parent.Delete([]byte(key))
         .          .    120:			continue
         .          .    121:		}
         .          .    122:
      50ms      2.45s    123:		cacheValue := store.cache[key]
     910ms      920ms    124:		if cacheValue.value != nil {
         .          .    125:			// It already exists in the parent, hence delete it.
     120ms     29.56s    126:			store.parent.Set([]byte(key), cacheValue.value)
         .          .    127:		}
         .          .    128:	}
         .          .    129:
         .          .    130:	// Clear the cache using the map clearing idiom
         .          .    131:	// and not allocating fresh objects.
         .          .    132:	// Please see https://bencher.orijtech.com/perfclinic/mapclearing/
         .      210ms    133:	for key := range store.cache {
         .          .    134:		delete(store.cache, key)
         .          .    135:	}
         .       10ms    136:	for key := range store.deleted {
         .          .    137:		delete(store.deleted, key)
         .          .    138:	}
         .      170ms    139:	for key := range store.unsortedCache {
         .          .    140:		delete(store.unsortedCache, key)
         .          .    141:	}
         .      260ms    142:	store.sortedCache = dbm.NewMemDB()
         .       10ms    143:}

```

Fixes #10487
Updates https://github.com/tharsis/ethermint/issues/710
2021-11-08 23:49:13 +00:00
.github chore: golangci-linter github action setup (#10475) 2021-11-08 10:24:22 +00:00
baseapp feat: ADR-038 Part 2: StreamingService interface, file writing implementation, and configuration (#8664) 2021-10-24 21:37:37 +00:00
client chore: Add "Since:" on proto doc comments (#10434) 2021-10-27 14:13:35 +00:00
codec refactor: move from io/ioutil to io and os package (#10341) 2021-10-13 07:38:22 +00:00
container refactor(container)!: move logging and visualization options to DebugOption (#10302) 2021-10-14 16:49:50 +00:00
contrib style: lint go and markdown (#10060) 2021-10-30 13:43:04 +00:00
cosmovisor style: lint go and markdown (#10060) 2021-10-30 13:43:04 +00:00
crypto style: lint go and markdown (#10060) 2021-10-30 13:43:04 +00:00
db style: lint go and markdown (#10060) 2021-10-30 13:43:04 +00:00
docs chore: fix typo in adr-014-proportional-slashing.md (#10492) 2021-11-08 10:13:14 +00:00
internal feat: ADR-040: Implement KV Store with decoupled storage and SMT (#9892) 2021-10-19 11:58:06 +00:00
proto/cosmos feat: implement nft module msg server (#10074) 2021-10-27 14:43:47 +00:00
scripts style: lint go and markdown (#10060) 2021-10-30 13:43:04 +00:00
server style: lint go and markdown (#10060) 2021-10-30 13:43:04 +00:00
simapp feat(mint): Implement custom inflation function. (#10441) 2021-11-08 10:56:10 +00:00
snapshots style: lint go and markdown (#10060) 2021-10-30 13:43:04 +00:00
std Add ADR 031 BaseApp and codec infrastructure (#7519) 2020-10-15 13:07:59 +00:00
store perf: store/cachekv: avoid a map lookup if unnecessary, clear maps fast (#10486) 2021-11-08 23:49:13 +00:00
telemetry refactor!: Keyring migration (#9695) 2021-09-20 12:02:15 +00:00
tests test: fix mockgen version (#9127) 2021-05-25 09:18:59 +00:00
testutil feat: Add Table-Store (aka ORM) package - `AutoUInt64Table` and `PrimaryKeyTable` (#10415) 2021-10-27 12:49:37 +00:00
third_party/proto feat: Introduce Cosmos Scalars (#9933) 2021-09-21 09:46:29 +00:00
types style: lint go and markdown (#10060) 2021-10-30 13:43:04 +00:00
version refactor: Implementing sigs.k8s.io YAML to remove .proto yaml annotations (#9780) 2021-09-24 14:37:34 +00:00
x feat(mint): Implement custom inflation function. (#10441) 2021-11-08 10:56:10 +00:00
.build.sh fix library file path (#8301) 2021-01-12 10:08:42 +00:00
.clang-format Fix clang-format to specific version (#7350) 2020-10-27 10:04:34 +00:00
.codecov.yml CI: Fix codecov flag project config (#7994) 2020-11-20 09:13:25 -05:00
.deepsource.toml chore: refactor code quality issues (#8932) 2021-03-22 09:30:27 +00:00
.dockerignore Add basic Dockerfile to build all binaries and export gaiad 2018-04-10 12:39:47 +02:00
.gitattributes Merge PR #6426: Migrate API Server 2020-06-15 13:39:09 -04:00
.gitignore docs: fix cosmovisor quickstart (#9730) 2021-07-26 07:51:36 +00:00
.golangci.yml fix(CI): add golangci-lint to dependencies (#10112) 2021-09-14 11:05:30 +02:00
.goreleaser.yml ci: automate release post tag push (#6230) 2020-05-25 20:02:08 +02:00
.markdownlint.json chore: add markdownlint to lint commands (#9353) 2021-05-27 15:31:04 +00:00
.markdownlintignore chore: add markdownlint to lint commands (#9353) 2021-05-27 15:31:04 +00:00
.mergify.yml chore: update mergify for v0.44 backports (#10146) 2021-09-15 12:43:59 +02:00
CHANGELOG.md perf: store/cachekv: avoid a map lookup if unnecessary, clear maps fast (#10486) 2021-11-08 23:49:13 +00:00
CODE_OF_CONDUCT.md Update CODE_OF_CONDUCT.md (#7381) 2020-09-28 11:28:30 +02:00
CODING_GUIDELINES.md style: lint go and markdown (#10060) 2021-10-30 13:43:04 +00:00
CONTRIBUTING.md style: lint go and markdown (#10060) 2021-10-30 13:43:04 +00:00
Dockerfile build: Update Dockerfile (#9839) 2021-08-03 09:46:16 -04:00
LICENSE docs: 10180 Fix SDK (#10237) 2021-09-28 19:33:58 +02:00
Makefile chore: golangci-linter github action setup (#10475) 2021-11-08 10:24:22 +00:00
README.md chore: add meeting list and working groups to contributing doc (#10315) 2021-10-13 13:01:29 +00:00
RELEASE_PROCESS.md style: lint go and markdown (#10060) 2021-10-30 13:43:04 +00:00
SECURITY.md docs: update contributing guidelines (#10223) 2021-10-01 11:36:22 +00:00
buf.yaml refactor: Bring back deprecated proto fields to `v1beta1` (#9534) 2021-06-23 13:03:33 +00:00
docker-compose.yml perf: Change localnet CIDR from /16 (65k addresses) to /25 (128 addresses) to reduce docker network conflicts (#9667) 2021-07-12 08:51:36 +00:00
go.mod build(deps): Bump github.com/rs/zerolog from 1.25.0 to 1.26.0 (#10483) 2021-11-02 18:50:20 +01:00
go.sum build(deps): Bump github.com/rs/zerolog from 1.25.0 to 1.26.0 (#10483) 2021-11-02 18:50:20 +01:00

README.md

Cosmos SDK

banner

The Cosmos SDK is a framework for building blockchain applications. Tendermint Core (BFT Consensus) and the Cosmos SDK are written in the Golang programming language. Cosmos SDK is used to build Gaia, the first implementation of the Cosmos Hub.

WARNING: The Cosmos SDK has mostly stabilized, but we are still making some breaking changes.

Note: Requires Go 1.17+

Quick Start

To learn how the Cosmos SDK works from a high-level perspective, see the Cosmos SDK High-Level Intro.

If you want to get started quickly and learn how to build on top of Cosmos SDK, visit Cosmos SDK Tutorials. You can also fork the tutorial's repository to get started building your own Cosmos SDK application.

For more information, see the Cosmos SDK Documentation.

Contributing

See CONTRIBUTING.md for details how to contribute and participate in our dev calls. If you want to follow the updates or learn more about the latest design then join our Discord.

Tools and Frameworks

The Cosmos ecosystem is vast. We will only make a few notable mentions here.

  • Tools: notable frameworks and modules.
  • CosmJS: the Swiss Army knife to power JavaScript based client solutions.

Cosmos Hub Mainnet

The Cosmos Hub application, gaia, has moved to its own cosmos/gaia repository. Go there to join the Cosmos Hub mainnet and more.

Inter-Blockchain Communication (IBC)

The IBC module for the Cosmos SDK has moved to its own cosmos/ibc-go repository. Go there to build and integrate with the IBC module.

Starport

Starport is the all-in-one platform to build, launch, and maintain any crypto application on a sovereign and secured blockchain. If you are building a new app or a new module, use Starport to get started and speed up development.

Disambiguation

This Cosmos SDK project is not related to the React-Cosmos project (yet). Many thanks to Evan Coury and Ovidiu (@skidding) for this Github organization name. As per our agreement, this disambiguation notice will stay here.