rust-lightning icon indicating copy to clipboard operation
rust-lightning copied to clipboard

Store `Feature` flags in line rather than on the heap by default (without increasing their size)

Open TheBlueMatt opened this issue 8 months ago • 5 comments

In a running LDK instance we generally have a ton of Features objects flying around, including ones per channel/peer in the NetworkGraph, ones per channel/peer in Channel/ChannelManager, and ones inside of BOLT 11/12 Invoices.

Thus, its useful to avoid unecessary allocations in them to reduce heap fragmentation.

Luckily, Features generally don't have more than 15 bytes worth of flags, and because Vec has a NonNull pointer we can actually wrap a vec in a two-variant enum with zero additional space penalty.

While this patch leaves actually deserializing Features without allocating as a future exercise, immediately releasing the allocation is much better than holding it, and Features constructed through repeated set_* calls benefit from avoiding the Vec entirely.

TheBlueMatt avatar Apr 10 '25 22:04 TheBlueMatt

👋 Thanks for assigning @tnull as a reviewer! I'll wait for their review and will help manage the review process. Once they submit their review, I'll check if a second reviewer would be helpful.

ldk-reviews-bot avatar Apr 10 '25 22:04 ldk-reviews-bot

🔔 1st Reminder

Hey @valentinewallace! This PR has been waiting for your review. Please take a look when you have a chance. If you're unable to review, please let us know so we can find another reviewer.

ldk-reviews-bot avatar Apr 14 '25 00:04 ldk-reviews-bot

Seems CI is stuck here?

tnull avatar Apr 22 '25 17:04 tnull

Went ahead and squashed to see if CI will run again.

TheBlueMatt avatar Apr 24 '25 16:04 TheBlueMatt

Also made clippy happy:

$ git diff-tree -U2 a07c020d4 4a841aac3diff --git a/lightning-types/src/features.rs b/lightning-types/src/features.rs
index 804083d65..d97347b47 100644
--- a/lightning-types/src/features.rs
+++ b/lightning-types/src/features.rs
@@ -816,5 +816,5 @@ impl DerefMut for FeatureFlags {
 impl PartialOrd for FeatureFlags {
 	fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
-		self.deref().partial_cmp(other.deref())
+		Some(self.cmp(other))
 	}
 }

TheBlueMatt avatar Apr 26 '25 01:04 TheBlueMatt

Codecov Report

Attention: Patch coverage is 90.09901% with 10 lines in your changes missing coverage. Please review.

Project coverage is 90.99%. Comparing base (85185d8) to head (f491278). Report is 98 commits behind head on main.

Files with missing lines Patch % Lines
lightning-types/src/features.rs 90.09% 6 Missing and 4 partials :warning:
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3730      +/-   ##
==========================================
+ Coverage   89.11%   90.99%   +1.87%     
==========================================
  Files         156      158       +2     
  Lines      123514   138442   +14928     
  Branches   123514   138442   +14928     
==========================================
+ Hits       110070   125970   +15900     
+ Misses      10762     9853     -909     
+ Partials     2682     2619      -63     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov[bot] avatar May 05 '25 21:05 codecov[bot]

🔔 1st Reminder

Hey @tnull! This PR has been waiting for your review. Please take a look when you have a chance. If you're unable to review, please let us know so we can find another reviewer.

ldk-reviews-bot avatar May 07 '25 13:05 ldk-reviews-bot

🔔 2nd Reminder

Hey @tnull! This PR has been waiting for your review. Please take a look when you have a chance. If you're unable to review, please let us know so we can find another reviewer.

ldk-reviews-bot avatar May 10 '25 00:05 ldk-reviews-bot

🔔 3rd Reminder

Hey @tnull! This PR has been waiting for your review. Please take a look when you have a chance. If you're unable to review, please let us know so we can find another reviewer.

ldk-reviews-bot avatar May 12 '25 00:05 ldk-reviews-bot

Squashed without further changes.

TheBlueMatt avatar May 15 '25 13:05 TheBlueMatt