Skip to content

Commit

Permalink
chores: add nolint explanation (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
quagmt authored Nov 19, 2024
1 parent f71729f commit 2e1d37c
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 71 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/fuzz.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Fuzz

on:
pull_request:
paths:
- "**.go"
- go.mod
- go.sum
- ".github/workflows/fuzz.yaml"

jobs:
fuzz:
runs-on: ubuntu-latest
steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod

- name: Run fuzz tests
run: make fuzz-all 20
20 changes: 2 additions & 18 deletions .github/workflows/ci.yaml → .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
name: CI
name: Lint and Test

on:
pull_request:
paths:
- "**.go"
- go.mod
- go.sum
- ".github/workflows/ci.yaml"
- ".github/workflows/test.yaml"
- ".golangci.yaml"

jobs:
Expand Down Expand Up @@ -54,19 +54,3 @@ jobs:
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}

fuzz:
runs-on: ubuntu-latest
steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Setup Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod

- name: Run fuzz tests
run: make fuzz-all 20
9 changes: 9 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
run:
timeout: 5m

linters-settings:
nolintlint:
# Enable to require an explanation of nonzero length after each nolint directive.
# Default: false
require-explanation: true
# Enable to require nolint directives to mention the specific linter being suppressed.
# Default: false
require-specific: true

linters:
enable:
- nolintlint
Expand Down
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ func main() {
fmt.Println(a.Div(d)) // 123.456 / 4123547.1234567890123456789 = 0.0000299392722585176

// Rounding
fmt.Println(c.RoundBank(3)) // banker's rounding: 1.2345 -> 1.234
fmt.Println(c.RoundBank(3)) // banker's rounding: 1.2345 -> 1.234
fmt.Println(c.RoundAwayFromZero(2)) // round away from zero: 1.2345 -> 1.24
fmt.Println(c.RoundHAZ(3)) // half away from zero: 1.2345 -> 1.235
fmt.Println(c.RoundHTZ(3)) // half towards zero: 1.2345 -> 1.234
fmt.Println(c.Trunc(2)) // truncate: 1.2345 -> 1.23
fmt.Println(c.Floor()) // floor: 1.2345 -> 1
fmt.Println(c.Ceil()) // ceil: 1.2345 -> 2
fmt.Println(c.RoundHAZ(3)) // half away from zero: 1.2345 -> 1.235
fmt.Println(c.RoundHTZ(3)) // half towards zero: 1.2345 -> 1.234
fmt.Println(c.Trunc(2)) // truncate: 1.2345 -> 1.23
fmt.Println(c.Floor()) // floor: 1.2345 -> 1
fmt.Println(c.Ceil()) // ceil: 1.2345 -> 2

// Display
fmt.Println(a.String()) // 123.456
Expand Down Expand Up @@ -108,10 +108,10 @@ func main() {
a, _ := udecimal.NewFromFloat64(1.345) // a = 1.345

// Rounding
fmt.Println(a.RoundBank(2)) // banker's rounding: 1.345 -> 1.34
fmt.Println(a.RoundBank(2)) // banker's rounding: 1.345 -> 1.34
fmt.Println(a.RoundAwayFromZero(2)) // round away from zero: 1.345 -> 1.35
fmt.Println(a.RoundHAZ(2)) // half away from zero: 1.45 -> 1.35
fmt.Println(a.RoundHTZ(2)) // half towards zero: 1.45 -> 1.34
fmt.Println(a.RoundHAZ(2)) // half away from zero: 1.45 -> 1.35
fmt.Println(a.RoundHTZ(2)) // half towards zero: 1.45 -> 1.34
}
```

Expand Down
39 changes: 7 additions & 32 deletions benchmarks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,6 @@ This section provides benchmarks for the `udecimal` library in comparision with

For full benchmark details, checkout [benchmark-udec.txt](./bench-ss.txt) and [benchstat.txt](./benchstat.txt)

## How to run benchmarks

### 1. Clone the repository and go to the benchmarks directory

```bash
git clone https://github.com/quagmt/udecimal.git
cd udecimal/benchmarks
```

### 2. Run benchmarks

Run full benchmarks with benchstat comparison (it will take some time)

```bash
make bench-ss
make bench-udec
make stats
```

Or run benchmarks for `udecimal` only

```bash
make bench-udec
```

## Benchmark Results

<i>**NOTE**: The results are for references and can be varied depending on the hardware</i>
Expand Down Expand Up @@ -63,15 +38,15 @@ BenchmarkPow/1.01.Pow(10)-32
BenchmarkPow/1.01.Pow(100)-32 994129 1137 ns/op 817 B/op 13 allocs/op
# Parsing string
BenchmarkParse/1234567890123456789.1234567890123456879-32 32111433 38.21 ns/op 0 B/op 0 allocs/op
BenchmarkParse/1234567890-32 98585916 12.58 ns/op 0 B/op 0 allocs/op
BenchmarkParse/0.1234567890123456879-32 44339668 26.45 ns/op 0 B/op 0 allocs/op
BenchmarkParseFallBack/123456789123456789123456.1234567890123456-32 2805122 473.3 ns/op 192 B/op 6 allocs/op
BenchmarkParseFallBack/111222333444555666777888999.1234567890123456789-32 2442004 500.8 ns/op 216 B/op 6 allocs/op
BenchmarkParse/1234567890123456789.1234567890123456879-32 32111433 38.21 ns/op 0 B/op 0 allocs/op
BenchmarkParse/1234567890-32 98585916 12.58 ns/op 0 B/op 0 allocs/op
BenchmarkParse/0.1234567890123456879-32 44339668 26.45 ns/op 0 B/op 0 allocs/op
BenchmarkParseFallBack/123456789123456789123456.1234567890123456-32 2805122 473.3 ns/op 192 B/op 6 allocs/op
BenchmarkParseFallBack/111222333444555666777888999.1234567890123456789-32 2442004 500.8 ns/op 216 B/op 6 allocs/op
BenchmarkString/1234567890123456789.1234567890123456879-32 14577884 76.50 ns/op 48 B/op 1 allocs/op
BenchmarkString/0.1234567890123456879-32 41109242 40.02 ns/op 24 B/op 1 allocs/op
BenchmarkStringFallBack/123456789123456789123456.1234567890123456-32 4147044 256.2 ns/op 208 B/op 4 allocs/op
BenchmarkStringFallBack/111222333444555666777888999.1234567890123456789-32 3808071 313.3 ns/op 208 B/op 4 allocs/op
BenchmarkStringFallBack/123456789123456789123456.1234567890123456-32 4147044 256.2 ns/op 208 B/op 4 allocs/op
BenchmarkStringFallBack/111222333444555666777888999.1234567890123456789-32 3808071 313.3 ns/op 208 B/op 4 allocs/op
# Marshal/Unmarshal
BenchmarkMarshalJSON/1234567890123456789.1234567890123456879-32 13965998 77.22 ns/op 48 B/op 1 allocs/op
Expand Down
6 changes: 3 additions & 3 deletions bint.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func parseBint(s []byte) (bool, bint, uint8, error) {
return false, bint{}, 0, errInvalidFormat(s)
}

//nolint:gosec
//nolint:gosec // prec <= maxPrec (19) and can be safely converted to uint8
return neg, bintFromBigInt(dValue), uint8(prec), nil
}

Expand Down Expand Up @@ -244,7 +244,7 @@ func parseSmallToU128(s []byte) (u128, uint8, error) {
return u128{}, 0, ErrInvalidFormat
}

//nolint:gosec
//nolint:gosec // len(s) <= maxDigitU64 and len(s)-i-1 >= 0, can be safely converted to uint8
prec = uint8(len(s) - i - 1)

// prevent "123." or "-123."
Expand Down Expand Up @@ -293,7 +293,7 @@ func parseLargeToU128(s []byte) (u128, uint8, error) {
}

// now 0 < pos < l-1
//nolint:gosec
//nolint:gosec // l < maxStrLen, so 0 < l-pos-1 < 256, can be safely converted to uint8
prec := uint8(l - pos - 1)
if prec > defaultPrec {
return u128{}, 0, ErrPrecOutOfRange
Expand Down
12 changes: 6 additions & 6 deletions decimal.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func NewFromInt64(coef int64, prec uint8) (Decimal, error) {
return Decimal{}, ErrPrecOutOfRange
}

//nolint:gosec
//nolint:gosec // coef is positive, so it's safe to convert to uint64
return newDecimal(neg, bintFromU64(uint64(coef)), prec), nil
}

Expand Down Expand Up @@ -1354,7 +1354,7 @@ func (d Decimal) PowInt(e int) Decimal {
neg = false
}

//nolint:gosec
//nolint:gosec // powPrecision <= defaultPrec, so it's safe to convert to uint8
return newDecimal(neg, bintFromBigInt(qBig), uint8(powPrecision))
}

Expand Down Expand Up @@ -1422,7 +1422,7 @@ func (d Decimal) PowInt32(e int32) (Decimal, error) {
neg = false
}

//nolint:gosec
//nolint:gosec // powPrecision <= defaultPrec, so it's safe to convert to uint8
return newDecimal(neg, bintFromBigInt(qBig), uint8(powPrecision)), nil
}

Expand Down Expand Up @@ -1484,7 +1484,7 @@ func (d Decimal) tryPowIntU128(e int) (Decimal, error) {
return Decimal{}, errOverflow
}

//nolint:gosec
//nolint:gosec // exponent <= defaultPrec, so it's safe to convert to uint8
return newDecimal(neg, bintFromU128(u128{hi: result.hi, lo: result.lo}), uint8(exponent)), nil
}

Expand Down Expand Up @@ -1600,8 +1600,8 @@ func (d Decimal) sqrtU128() (Decimal, error) {
return Decimal{}, errOverflow
}

//nolint:gosec
bitLen := uint(coef.bitLen()) // bitLen < 256
//nolint:gosec // 0 <= coef.bitLen() < 256, so it's safe to convert to uint
bitLen := uint(coef.bitLen())

// initial guess = 2^((bitLen + 1) / 2) ≥ √coef
x := one128.Lsh((bitLen + 1) / 2)
Expand Down
2 changes: 1 addition & 1 deletion u128.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ func (u u128) QuoRem(v u128) (q, r u128, err error) {
// generate a "trial quotient," guaranteed to be within 1 of the actual
// quotient, then adjust.

//nolint:gosec
//nolint:gosec // 0 <= bits.LeadingZeros64(v.hi) <= 63, so it's safe to convert to uint
n := uint(bits.LeadingZeros64(v.hi))
v1 := v.Lsh(n)
u1 := u.Rsh(1)
Expand Down
4 changes: 2 additions & 2 deletions u256.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (u u256) div256by128(v u128) (u128, u128) {
// normalize v
n := bits.LeadingZeros64(v.hi)

//nolint:gosec
//nolint:gosec // 0 <= n <= 63, so it's safe to convert to uint
v = v.Lsh(uint(n))

// shift u to the left by n bits (n < 64)
Expand Down Expand Up @@ -235,7 +235,7 @@ func (u u256) div256by128(v u128) (u128, u128) {
a[i+1], a[i] = rem.hi, rem.lo
}

//nolint:gosec
//nolint:gosec // 0 <= n <= 63, so it's safe to convert to uint
r := u128{hi: a[1], lo: a[0]}.Rsh(uint(n))

return u128{hi: q[1], lo: q[0]}, r
Expand Down

0 comments on commit 2e1d37c

Please sign in to comment.