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

refactor: Use merge_sort instead of qsort for sorting. #2638

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

iphydf
Copy link
Member

@iphydf iphydf commented Feb 3, 2024

This change is Reviewable

Copy link

codecov bot commented Feb 3, 2024

Codecov Report

Attention: Patch coverage is 98.68421% with 2 lines in your changes missing coverage. Please review.

Project coverage is 73.16%. Comparing base (a3d1b85) to head (6970387).
Report is 21 commits behind head on master.

Files with missing lines Patch % Lines
toxcore/DHT.c 97.36% 1 Missing ⚠️
toxcore/group.c 95.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2638      +/-   ##
==========================================
+ Coverage   73.04%   73.16%   +0.11%     
==========================================
  Files         149      149              
  Lines       30516    30594      +78     
==========================================
+ Hits        22290    22383      +93     
+ Misses       8226     8211      -15     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@iphydf iphydf force-pushed the merge-sort branch 2 times, most recently from 5580dfc to d875693 Compare February 3, 2024 19:40
@JFreegman
Copy link
Member

What are the advantages of merge sort over qsort in this instance?

@iphydf iphydf marked this pull request as ready for review February 3, 2024 21:19
@iphydf
Copy link
Member Author

iphydf commented Feb 4, 2024

What are the advantages of merge sort over qsort in this instance?

https://github.com/TokTok/c-toxcore/pull/2638/files#diff-421ac668286fc646ef81a78b41a2b05bb4c1dbf1f02f3ed69ced2929a8ba4e10R141-R145

I guess I can explain more in that comment if needed. This PR is part of my attempt to make toxcore C code rely less on struct layouts and low level system properties and be semantically simpler. I know C coders like to bit-twiddle everything, but I think it's worth trying to avoid that as much as possible. qsort operates with memcpy, lots of casting from and to char*, moving around in abstract memory with pointer arithmetic, etc. It's semantically complex and requires complicated abstract domains that understand memory layouts and their implications.

@iphydf
Copy link
Member Author

iphydf commented Feb 5, 2024

As for why merge sort instead of any other algorithm: it's an easy one to implement and has deterministic time complexity. If we have a lot of mostly-sorted arrays, we should probably just sorted-insert elements rather than adding a bunch of unsorted elements and sorting afterwards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor Refactoring production code, eg. renaming a variable, not affecting semantics
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants