Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

orderbook: Refactor package structure for simplicity and efficiency #1465

Merged
merged 18 commits into from
May 14, 2024

Conversation

shazbert
Copy link
Collaborator

@shazbert shazbert commented Feb 2, 2024

PR Description

Update orderbook package. This is an initial update before I implement options books complimentary to #1082.

  • Renames orderbook.Item to orderbook.Tranche for improved clarity.
  • Renames orderbook.Items to orderbook.Tranches.
  • Removes the linked list implementation and associated handling due to complexity and overhead.
  • Renames linked_list.go to Tranches.go and removes linked list-specific manipulation functions.
  • Eliminates node.go and associated tests as they are no longer necessary.
  • Removes the byOBPrice type, as it can be inlined in functions using sort.Slice.
  • Eliminates the unsafe package due to redundancy and potential risks associated with its usage.

Initial benchmarks on my machine suggest a slight performance boost (when deleting it has performance hit 😭, but everything else is pretty schnappy) resulting from the changes introduced in this pull request.

Type of change

Please delete options that are not relevant and add an x in [] as item is complete.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How has this been tested

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration and
also consider improving test coverage whilst working on a certain feature or package.

  • go test ./... -race
  • golangci-lint run
  • Test X

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation and regenerated documentation via the documentation tool
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally and on Github Actions with my changes
  • Any dependent changes have been merged and published in downstream modules

@shazbert shazbert added the review me This pull request is ready for review label Feb 2, 2024
@shazbert shazbert requested a review from a team February 2, 2024 00:21
@shazbert shazbert self-assigned this Feb 2, 2024
Copy link

codecov bot commented Feb 2, 2024

Codecov Report

Attention: Patch coverage is 88.82603% with 79 lines in your changes are missing coverage. Please review.

Project coverage is 37.77%. Comparing base (b2d7bee) to head (d2c76cf).

Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1465      +/-   ##
==========================================
+ Coverage   36.00%   37.77%   +1.76%     
==========================================
  Files         412      409       -3     
  Lines      177779   147712   -30067     
==========================================
- Hits        64017    55794    -8223     
+ Misses     105902    84052   -21850     
- Partials     7860     7866       +6     
Files Coverage Δ
exchanges/binance/binance_websocket.go 60.34% <100.00%> (+2.40%) ⬆️
exchanges/binance/binance_wrapper.go 41.27% <100.00%> (+2.74%) ⬆️
exchanges/binanceus/binanceus_websocket.go 47.26% <100.00%> (+3.31%) ⬆️
exchanges/binanceus/binanceus_wrapper.go 44.89% <100.00%> (+3.99%) ⬆️
exchanges/bitfinex/bitfinex_websocket.go 36.30% <100.00%> (+3.57%) ⬆️
exchanges/bitmex/bitmex_websocket.go 24.41% <100.00%> (+2.05%) ⬆️
exchanges/btcmarkets/btcmarkets_websocket.go 43.38% <100.00%> (+3.75%) ⬆️
exchanges/btcmarkets/btcmarkets_wrapper.go 35.87% <100.00%> (+3.24%) ⬆️
exchanges/btse/btse_websocket.go 38.92% <100.00%> (+2.45%) ⬆️
exchanges/btse/btse_wrapper.go 48.29% <100.00%> (+3.74%) ⬆️
... and 47 more

... and 331 files with indirect coverage changes

@shazbert shazbert changed the title orderbook: Update naming convention and improve performance. orderbook: Update naming convention and rm complexity. Feb 4, 2024
Copy link
Collaborator

@gloriousCode gloriousCode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate the simplification, even though many functions appear to remain very similar 🎉
I'll definitely be doing more rounds of testing for this important code. I've mostly just questioned benchmarking for now :D

exchanges/orderbook/orderbook_test.go Outdated Show resolved Hide resolved
exchanges/orderbook/tranches_test.go Show resolved Hide resolved
exchanges/orderbook/orderbook_test.go Show resolved Hide resolved
exchanges/orderbook/orderbook_test.go Show resolved Hide resolved
@@ -0,0 +1,669 @@
package orderbook
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please Make This A Lower Case File, Thanks! Unless There is A New Thing?

exchanges/orderbook/Tranches.go Outdated Show resolved Hide resolved
@shazbert shazbert requested a review from gloriousCode February 6, 2024 01:41
Copy link
Collaborator

@gloriousCode gloriousCode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After making the master branch benchmarks equivalent to your updates, the main downside I'm seeing is sorting performance, which isn't used very much thankfully.

Overall, I find the simplification of manipulating a slice over having a linked list to be an improvement, even if some of the benchmarks for me aren't drastic. I have found no problems with orderbook checksum calculation or anything during testing.

benchmark master time this branch time master bytes this branch bytes master allocs this branch allocs
BenchmarkUpdateByID-10 18.34 ns/op 18.92 ns/op 0 B/op 0 B/op 0 allocs/op 0 allocs/op
BenchmarkLoad-10 72.87 ns/op 23.01 ns/op 0 B/op 0 B/op 0 allocs/op 0 allocs/op
BenchmarkUpdateInsertByPrice_Amend-10 37.13 ns/op 5.698 ns/op 0 B/op 0 B/op 0 allocs/op 0 allocs/op
BenchmarkUpdateInsertByPrice_Insert_Delete-10 58.17 ns/op 53.03 ns/op 0 B/op 0 B/op 0 allocs/op 0 allocs/op
BenchmarkRetrieve-10 111.2 ns/op 106.3 ns/op 480 B/op 480 B/op 1 allocs/op 1 allocs/op
BenchmarkUpdateInsertByID_bids-10 49.06 ns/op 51.26 ns/op 0 B/op 0 B/op 0 allocs/op 0 allocs/op
BenchmarkDeleteByID-10 207.6 ns/op 151.2 ns/op 0 B/op 480 B/op 0 allocs/op 1 allocs/op
BenchmarkUpdateInsertByID_asks-10 38.11 ns/op 38.66 ns/op 0 B/op 0 B/op 0 allocs/op 0 allocs/op
BenchmarkReverse-10 2845 ns/op 2826 ns/op 0 B/op 0 B/op 0 allocs/op 0 allocs/op
BenchmarkSortAsksDecending-10 4081 ns/op 4269 ns/op 24 B/op 152 B/op 1 allocs/op 3 allocs/op
BenchmarkSortBidsAscending-10 4707 ns/op 4181 ns/op 41 B/op 152 B/op 2 allocs/op 3 allocs/op
BenchmarkSortAsksStandard-10 68287 ns/op 95506 ns/op 39 B/op 172 B/op 1 allocs/op 3 allocs/op
BenchmarkSortBidsStandard-10 86231 ns/op 94179 ns/op 58 B/op 172 B/op 2 allocs/op 3 allocs/op
BenchmarkSortAsksAscending-10 4137 ns/op 4302 ns/op 24 B/op 152 B/op 1 allocs/op 3 allocs/op
BenchmarkSortBidsDescending-10 4710 ns/op 4237 ns/op 41 B/op 152 B/op 2 allocs/op 3 allocs/op

exchanges/orderbook/depth.go Outdated Show resolved Hide resolved
exchanges/kraken/kraken_test.go Show resolved Hide resolved
@shazbert shazbert requested a review from gloriousCode March 18, 2024 00:03
Copy link
Collaborator

@gloriousCode gloriousCode left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK! Thanks Shazbert

@gloriousCode gloriousCode added the gcrc GloriousCode Review Complete label May 7, 2024
@thrasher- thrasher- changed the title orderbook: Update naming convention and rm complexity. orderbook: Refactor package structure for simplicity and efficiency May 14, 2024
Copy link
Collaborator

@thrasher- thrasher- left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK, running OKX/Kraken and Bitfinex websocket stream, no checksum issues. Tests happy. Benchmarks for sec/op of old vs new on my system at the cost of slightly more allocs. Nice stuff!

goos: windows
goarch: amd64
pkg: github.com/thrasher-corp/gocryptotrader/exchanges/orderbook
cpu: AMD Ryzen Threadripper 3970X 32-Core Processor
                                     │    old.txt    │               new.txt               │
                                     │    sec/op     │   sec/op     vs base                │
Load-64                                 62.00n ±  0%   16.23n ± 1%  -73.81% (p=0.000 n=10)
Reverse-64                              2.945µ ±  2%   2.960µ ± 2%        ~ (p=1.000 n=10)
SortAsksAscending-64                   21.724µ ±  4%   3.792µ ± 1%  -82.55% (p=0.000 n=10)
SortAsksDecending-64                   21.655µ ± 19%   3.770µ ± 1%  -82.59% (p=0.000 n=10)
SortAsksStandard-64                     117.8µ ±  2%   110.2µ ± 1%   -6.47% (p=0.000 n=10)
SortBidsAscending-64                   23.376µ ± 10%   3.789µ ± 1%  -83.79% (p=0.000 n=10)
SortBidsDescending-64                  23.498µ ±  4%   3.765µ ± 0%  -83.98% (p=0.000 n=10)
SortBidsStandard-64                     132.5µ ±  1%   107.6µ ± 1%  -18.82% (p=0.000 n=10)
UpdateByID-64                           30.54n ±  0%   24.00n ± 1%  -21.44% (p=0.000 n=10)
UpdateInsertByPrice_Amend-64           10.065n ±  1%   6.857n ± 1%  -31.87% (p=0.000 n=10)
UpdateInsertByPrice_Insert_Delete-64    30.32n ±  3%   46.98n ± 1%  +54.96% (p=0.000 n=10)
geomean                                 2.232µ         990.2n       -55.64%

                                   │   old.txt    │               new.txt                │
                                     │  allocs/op   │ allocs/op   vs base                  │
Load-64                                0.000 ± 0%     0.000 ± 0%        ~ (p=1.000 n=10) ¹
Reverse-64                             0.000 ± 0%     0.000 ± 0%        ~ (p=1.000 n=10) ¹
SortAsksAscending-64                   2.000 ± 0%     3.000 ± 0%  +50.00% (p=0.000 n=10)
SortAsksDecending-64                   2.000 ± 0%     3.000 ± 0%  +50.00% (p=0.000 n=10)
SortAsksStandard-64                    2.000 ± 0%     3.000 ± 0%  +50.00% (p=0.000 n=10)
SortBidsAscending-64                   3.000 ± 0%     3.000 ± 0%        ~ (p=1.000 n=10) ¹
SortBidsDescending-64                  3.000 ± 0%     3.000 ± 0%        ~ (p=1.000 n=10) ¹
SortBidsStandard-64                    3.000 ± 0%     3.000 ± 0%        ~ (p=1.000 n=10) ¹
UpdateByID-64                          0.000 ± 0%     0.000 ± 0%        ~ (p=1.000 n=10) ¹
UpdateInsertByPrice_Amend-64           0.000 ± 0%     0.000 ± 0%        ~ (p=1.000 n=10) ¹
UpdateInsertByPrice_Insert_Delete-64   0.000 ± 0%     0.000 ± 0%        ~ (p=1.000 n=10) ¹
geomean                                           ²               +11.69%                ²

@thrasher- thrasher- merged commit 4cd4fb0 into thrasher-corp:master May 14, 2024
8 of 13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
gcrc GloriousCode Review Complete review me This pull request is ready for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants