From 796ac3e053540409aabeb4e9eae1d806292d0848 Mon Sep 17 00:00:00 2001 From: Yu Zhang Date: Thu, 6 Aug 2026 14:26:28 +0800 Subject: [PATCH] perf(deque,queue,priority_queue): annotate consuming parameters with #owned Mark parameters whose reference is stored on every non-panic path as ownership-transferring, so last-use callers move the reference instead of paying an incref/decref pair per call: - Deque: push_back, push_front, insert, set - Queue: push (forwards into the owned Deque::push_back) - PriorityQueue: push, plus the meld/merges node plumbing Interleaved A/B benchmark (native, String elements): Deque::push_back -5~6%; generated C shows 3 RC ops per push reduced to 1. --- deque/deque.mbt | 4 ++++ priority_queue/priority_queue.mbt | 3 +++ queue/queue.mbt | 1 + 3 files changed, 8 insertions(+) diff --git a/deque/deque.mbt b/deque/deque.mbt index af931d3e6..06f7dcefa 100644 --- a/deque/deque.mbt +++ b/deque/deque.mbt @@ -410,6 +410,7 @@ pub fn[A] Deque::append(self : Deque[A], other : Deque[A]) -> Unit { /// ) /// } /// ``` +#owned(value) pub fn[A] Deque::insert(self : Deque[A], index : Int, value : A) -> Unit { guard index >= 0 && index <= self.length() else { abort( @@ -631,6 +632,7 @@ pub fn[A] Deque::back(self : Deque[A]) -> A? { /// @test.assert_eq(dv.front(), Some(0)) /// } /// ``` +#owned(value) pub fn[A] Deque::push_front(self : Deque[A], value : A) -> Unit { if self.len == self.buf.length() { self.realloc() @@ -654,6 +656,7 @@ pub fn[A] Deque::push_front(self : Deque[A], value : A) -> Unit { /// @test.assert_eq(dv.back(), Some(6)) /// } /// ``` +#owned(value) pub fn[A] Deque::push_back(self : Deque[A], value : A) -> Unit { if self.len == self.buf.length() { self.realloc() @@ -836,6 +839,7 @@ pub fn[A] Deque::at(self : Deque[A], index : Int) -> A { /// } /// ``` #alias("_[_]=_") +#owned(value) pub fn[A] Deque::set(self : Deque[A], index : Int, value : A) -> Unit { if index < 0 || index >= self.len { index_out_of_bounds(self.len, index) diff --git a/priority_queue/priority_queue.mbt b/priority_queue/priority_queue.mbt index c7b9b871d..5f4873572 100644 --- a/priority_queue/priority_queue.mbt +++ b/priority_queue/priority_queue.mbt @@ -154,6 +154,7 @@ pub fn[K : Compare] PriorityQueue::from_iter( } ///| +#owned(x, y) fn[A : Compare] meld(x : Node[A], y : Node[A]) -> Node[A] { if x.content > y.content { y.sibling = x.child @@ -167,6 +168,7 @@ fn[A : Compare] meld(x : Node[A], y : Node[A]) -> Node[A] { } ///| +#owned(x) fn[A : Compare] merges(x : Node[A]?) -> Node[A]? { let (x, acc) = match x { None => return None @@ -251,6 +253,7 @@ pub fn[A : Compare] PriorityQueue::pop(self : PriorityQueue[A]) -> A? { /// @test.assert_eq(queue.length(), 1) /// } /// ``` +#owned(value) pub fn[A : Compare] PriorityQueue::push( self : PriorityQueue[A], value : A, diff --git a/queue/queue.mbt b/queue/queue.mbt index bc880c5c7..c1c59b499 100644 --- a/queue/queue.mbt +++ b/queue/queue.mbt @@ -103,6 +103,7 @@ pub fn[A] Queue::is_empty(self : Queue[A]) -> Bool { /// queue.push(1) /// } /// ``` +#owned(x) pub fn[A] Queue::push(self : Queue[A], x : A) -> Unit { self.inner.push_back(x) }