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

Merged
merged 1 commit into from
Dec 5, 2024

Conversation

iphydf
Copy link
Member

@iphydf iphydf commented Feb 3, 2024

This change is Reviewable

@iphydf iphydf added this to the v0.2.19 milestone Feb 3, 2024
@iphydf iphydf force-pushed the merge-sort branch 4 times, most recently from 6358d6c to ce3a61b Compare February 3, 2024 18:32
Copy link

codecov bot commented Feb 3, 2024

Codecov Report

Attention: Patch coverage is 93.78531% with 11 lines in your changes missing coverage. Please review.

Project coverage is 73.13%. Comparing base (956cd08) to head (a749ee7).
Report is 4 commits behind head on master.

Files with missing lines Patch % Lines
toxcore/DHT.c 92.10% 3 Missing ⚠️
toxcore/group.c 85.00% 3 Missing ⚠️
toxcore/sort.c 95.38% 3 Missing ⚠️
toxcore/onion_client.c 92.59% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2638      +/-   ##
==========================================
- Coverage   73.21%   73.13%   -0.09%     
==========================================
  Files         149      150       +1     
  Lines       30595    30702     +107     
==========================================
+ Hits        22401    22454      +53     
- Misses       8194     8248      +54     

☔ 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.

@iphydf iphydf force-pushed the merge-sort branch 6 times, most recently from a17e42d to 37be3ea Compare February 10, 2024 23:58
@iphydf iphydf modified the milestones: v0.2.19, v0.2.20 Feb 11, 2024
@iphydf iphydf modified the milestones: v0.2.20, v0.2.21 Nov 6, 2024
@github-actions github-actions bot added the refactor Refactoring production code, eg. renaming a variable, not affecting semantics label Nov 8, 2024
@iphydf iphydf force-pushed the merge-sort branch 12 times, most recently from a932f6e to b770dec Compare December 5, 2024 11:56
Copy link
Member

@Green-Sky Green-Sky left a comment

Choose a reason for hiding this comment

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

with qsort gone, are we still using anything from stdlib.h ?

@iphydf
Copy link
Member Author

iphydf commented Dec 5, 2024

with qsort gone, are we still using anything from stdlib.h ?

Only in group.c. I've removed it from the other 2.

Copy link
Member

@Green-Sky Green-Sky left a comment

Choose a reason for hiding this comment

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

and onion_announce.c ?

@iphydf
Copy link
Member Author

iphydf commented Dec 5, 2024

and onion_announce.c ?

Done.

@iphydf iphydf merged commit 6460c25 into TokTok:master Dec 5, 2024
62 checks passed
@iphydf iphydf deleted the merge-sort branch December 5, 2024 19:11
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
Development

Successfully merging this pull request may close these issues.

3 participants