cosmos-sdk/x/params
Emmanuel T Odeke abe3e0c6da
perf: all: remove unnecessary allocations from strings.Builder.WriteString(fmt.Sprintf(...)) (#13230)
This change removes a code pattern that I noticed while on a late night
audit of cosmovisor in which
strings.Builder.WriteString(fmt.Sprintf(...))
calls were being made, yet that's counterproductive to using fmt.Fprintf
which will check whether the writer implements .WriteString and then
avoids the need to firstly build a string using fmt.Sprintf.

The performance wins from this change transcend all dimensions as
exhibited below:

```shell
$ benchstat before.txt after.txt
name            old time/op    new time/op    delta
DetailString-8    5.48µs ±23%    4.40µs ±11%  -19.79%  (p=0.000 n=20+17)

name            old alloc/op   new alloc/op   delta
DetailString-8    2.63kB ± 0%    2.11kB ± 0%  -19.76%  (p=0.000 n=20+20)

name            old allocs/op  new allocs/op  delta
DetailString-8      63.0 ± 0%      50.0 ± 0%  -20.63%  (p=0.000 n=20+20)
```

Fixes #13229
2022-09-09 21:57:46 +00:00
..
client refactor: migrate to `cosmos/gogoproto` (#13070) 2022-09-08 17:27:48 +00:00
keeper chore: math lib update and type fixes (#12791) 2022-08-02 11:16:40 +00:00
simulation refactor: remove `simapp` from `x/staking` module (#13101) 2022-08-31 14:50:22 +00:00
testutil refactor: replace app wiring yaml config by go (#12757) 2022-07-29 16:39:44 -03:00
types perf: all: remove unnecessary allocations from strings.Builder.WriteString(fmt.Sprintf(...)) (#13230) 2022-09-09 21:57:46 +00:00
README.md chore: collapse module spec and readme (#13143) 2022-09-05 12:26:40 +00:00
doc.go x/params docs general audit & cleanup (#8295) 2021-01-15 13:30:17 +00:00
module.go feat: Remove `RandomizedParams` from the `AppModuleSimulation` interface (#12846) 2022-08-10 14:34:45 +02:00
proposal_handler.go refactor: move legacy gov to v1beta1 (#10748) 2021-12-13 18:48:44 +00:00
proposal_handler_test.go chore: use `math.Int` instead of `math.Int` and apply linting (#12702) 2022-07-26 00:24:47 +02:00

README.md

x/params (Deprecated)

Note: The Params module has been depreacted in favour of each module housing its own parameters.

Abstract

Package params provides a globally available parameter store.

There are two main types, Keeper and Subspace. Subspace is an isolated namespace for a paramstore, where keys are prefixed by preconfigured spacename. Keeper has a permission to access all existing spaces.

Subspace can be used by the individual keepers, which need a private parameter store that the other keepers cannot modify. The params Keeper can be used to add a route to x/gov router in order to modify any parameter in case a proposal passes.

The following contents explains how to use params module for master and user modules.

Contents

Keeper

In the app initialization stage, subspaces can be allocated for other modules' keeper using Keeper.Subspace and are stored in Keeper.spaces. Then, those modules can have a reference to their specific parameter store through Keeper.GetSubspace.

Example:

type ExampleKeeper struct {
	paramSpace paramtypes.Subspace
}

func (k ExampleKeeper) SetParams(ctx sdk.Context, params types.Params) {
	k.paramSpace.SetParamSet(ctx, &params)
}

Subspace

Subspace is a prefixed subspace of the parameter store. Each module which uses the parameter store will take a Subspace to isolate permission to access.

Key

Parameter keys are human readable alphanumeric strings. A parameter for the key "ExampleParameter" is stored under []byte("SubspaceName" + "/" + "ExampleParameter"), where "SubspaceName" is the name of the subspace.

Subkeys are secondary parameter keys those are used along with a primary parameter key. Subkeys can be used for grouping or dynamic parameter key generation during runtime.

KeyTable

All of the parameter keys that will be used should be registered at the compile time. KeyTable is essentially a map[string]attribute, where the string is a parameter key.

Currently, attribute consists of a reflect.Type, which indicates the parameter type to check that provided key and value are compatible and registered, as well as a function ValueValidatorFn to validate values.

Only primary keys have to be registered on the KeyTable. Subkeys inherit the attribute of the primary key.

ParamSet

Modules often define parameters as a proto message. The generated struct can implement ParamSet interface to be used with the following methods:

  • KeyTable.RegisterParamSet(): registers all parameters in the struct
  • Subspace.{Get, Set}ParamSet(): Get to & Set from the struct

The implementor should be a pointer in order to use GetParamSet().