Skip to content

Commit

Permalink
Change benchmark examples
Browse files Browse the repository at this point in the history
  • Loading branch information
oissevalt committed Mar 17, 2024
1 parent 79862c5 commit db62759
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Opzione

_Opzione_ (Italian "option") is a Go library for optionals, with tracking of nested pointers.
_Opzione_ (Italian "option") is a Go library for optionals, with tracking of nested pointers. You can add it to your project using `go get`:

```shell
go get github.com/oissevalt/opzione
```

The package provides two basic optional types (containers), `Simple` and `Chained`.

Expand Down Expand Up @@ -59,7 +63,7 @@ func main() {
}
```

Note that the use of reflection can introduce additional operation time and memory usage, but best effort has been made to minimize such impact. According to benchmark (`opzione_test.go`), a sequence of operation on nested pointers took less than 400ns on average.
Note that the use of reflection can introduce additional operation time and memory usage, but best effort has been made to minimize such impact. According to benchmark (`opzione_test.go`), a sequence of operation on nested pointers took less than 300ns on average.

For value types, `Chained` is expected to behave the same as `Simple`.

Expand Down
33 changes: 15 additions & 18 deletions opzione_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,27 @@ import (
)

func BenchmarkOptionalReflection(b *testing.B) {
for i := 0; i < b.N; i++ {
value1 := 12
value2 := 24
number := 10
pointer := &number

somePointer := ChainedSome[*int](&value1)
nonePointer := ChainedNone[*int]()
for i := 0; i < b.N; i++ {
optional := ChainedSome(&pointer)

value, err := somePointer.Value()
value, err = nonePointer.Value()
somePointer.Swap(&value2)
nonePointer.Swap(&value1)
value, err = nonePointer.Value()
value, err = somePointer.Take()
_ = optional.IsNone()
_, _ = optional.Value()

intptr := &value2
nested := &intptr
ptrptrOption := ChainedSome[***int](&nested)
pointer = nil
_ = optional.IsNone()
_, _ = optional.Value()

valueptr, err := ptrptrOption.Value()
p2 := &number
_ = optional.Swap(&p2)

intptr = nil
valueptr, err = ptrptrOption.Value()
optional.With(func(i **int) {
_ = **i + 1
})

_, _, _ = value, err, valueptr
pointer = &number
}
}

Expand Down

0 comments on commit db62759

Please sign in to comment.