From cb8ce9d9e543ccf16a2f0a04e47a3691af10d5a6 Mon Sep 17 00:00:00 2001 From: Jacob Asper Date: Fri, 16 Feb 2024 09:41:48 -0500 Subject: [PATCH 1/8] time complexity for push --- library/alloc/src/vec/mod.rs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 08e3cdedc666b..7bdf92053f151 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -1912,6 +1912,13 @@ impl Vec { /// vec.push(3); /// assert_eq!(vec, [1, 2, 3]); /// ``` + /// + /// # Time complexity + /// + /// Takes amortized *O*(1) time. If the vector's length would exceed its capacity after + /// the push,*O*(*capacity*) space is allocated, doubling the capacity and + /// taking *O*(*capacity*) time. This expensive operation is offset by the + /// *capacity* *O*(1) insertions it allows. #[cfg(not(no_global_oom_handling))] #[inline] #[stable(feature = "rust1", since = "1.0.0")] From bb6dca0fc89d335e106151ee334fc18916f63067 Mon Sep 17 00:00:00 2001 From: Jacob Asper Date: Fri, 16 Feb 2024 09:58:34 -0500 Subject: [PATCH 2/8] time complexity for push_within_capacity --- library/alloc/src/vec/mod.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 7bdf92053f151..d6e2bd324004c 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -1966,6 +1966,10 @@ impl Vec { /// } /// assert_eq!(from_iter_fallible(0..100), Ok(Vec::from_iter(0..100))); /// ``` + /// + /// # Time complexity + /// + /// Takes *O*(1) time. #[inline] #[unstable(feature = "vec_push_within_capacity", issue = "100486")] pub fn push_within_capacity(&mut self, value: T) -> Result<(), T> { From 0a5d6841e8b14b2a4430ed675d2621fdd6747d04 Mon Sep 17 00:00:00 2001 From: Jacob Asper Date: Fri, 16 Feb 2024 09:59:37 -0500 Subject: [PATCH 3/8] time complexity for pop --- library/alloc/src/vec/mod.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index d6e2bd324004c..27efffb72ac04 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -1999,6 +1999,10 @@ impl Vec { /// assert_eq!(vec.pop(), Some(3)); /// assert_eq!(vec, [1, 2]); /// ``` + /// + /// # Time complexity + /// + /// Takes *O*(1) time. #[inline] #[stable(feature = "rust1", since = "1.0.0")] pub fn pop(&mut self) -> Option { From d2f825f26127f71cd853c602818452c8be876ea8 Mon Sep 17 00:00:00 2001 From: Jacob Asper Date: Sun, 18 Feb 2024 05:14:17 -0500 Subject: [PATCH 4/8] time complexity for insert --- library/alloc/src/vec/mod.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 27efffb72ac04..711b4a8a8ef35 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -1490,6 +1490,12 @@ impl Vec { /// vec.insert(4, 5); /// assert_eq!(vec, [1, 4, 2, 3, 5]); /// ``` + /// + /// # Time complexity + /// + /// Takes *O*(`len`) time. All items after the insertion index must be + /// shifted to the right. In the worst case, all elements are shifted when + /// the insertion index is 0. #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] pub fn insert(&mut self, index: usize, element: T) { From ef1a58484286454af6815d7e8a2f52c10b1449f2 Mon Sep 17 00:00:00 2001 From: Jacob Asper Date: Sun, 18 Feb 2024 05:29:56 -0500 Subject: [PATCH 5/8] intradoc link for vec --- library/alloc/src/vec/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index 711b4a8a8ef35..e9a57c0a47b6d 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -1493,7 +1493,7 @@ impl Vec { /// /// # Time complexity /// - /// Takes *O*(`len`) time. All items after the insertion index must be + /// Takes *O*([`Vec::len`]) time. All items after the insertion index must be /// shifted to the right. In the worst case, all elements are shifted when /// the insertion index is 0. #[cfg(not(no_global_oom_handling))] From a9cfeb34ddd4b859042f8d8f02ae96a0dbf8574b Mon Sep 17 00:00:00 2001 From: Jacob Asper Date: Sun, 18 Feb 2024 06:02:05 -0500 Subject: [PATCH 6/8] fix typo in push documentation --- library/alloc/src/vec/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index e9a57c0a47b6d..b4eee69e31f77 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -1922,7 +1922,7 @@ impl Vec { /// # Time complexity /// /// Takes amortized *O*(1) time. If the vector's length would exceed its capacity after - /// the push,*O*(*capacity*) space is allocated, doubling the capacity and + /// the push, *O*(*capacity*) space is allocated, doubling the capacity and /// taking *O*(*capacity*) time. This expensive operation is offset by the /// *capacity* *O*(1) insertions it allows. #[cfg(not(no_global_oom_handling))] From bc52e5d4de4a41f41e9849e564c955ce3d1fe513 Mon Sep 17 00:00:00 2001 From: Jacob Asper Date: Sun, 18 Feb 2024 17:55:52 -0500 Subject: [PATCH 7/8] Fix error in push docs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Copying is O(n)—not the memory allocation --- library/alloc/src/vec/mod.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index b4eee69e31f77..bd36b55d782fe 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -1921,10 +1921,11 @@ impl Vec { /// /// # Time complexity /// - /// Takes amortized *O*(1) time. If the vector's length would exceed its capacity after - /// the push, *O*(*capacity*) space is allocated, doubling the capacity and - /// taking *O*(*capacity*) time. This expensive operation is offset by the - /// *capacity* *O*(1) insertions it allows. + /// Takes amortized *O*(1) time. If the vector's length would exceed its + /// capacity after the push, the capacity is doubled by allocating + /// *O*(*capacity*) space, then *O*(*capacity*) time to copy the vector's + /// elements. This expensive operation is offset by the *capacity* *O*(1) + /// insertions it allows. #[cfg(not(no_global_oom_handling))] #[inline] #[stable(feature = "rust1", since = "1.0.0")] From 74151cbbf04477b646c7fcfd1db60f9c79c06081 Mon Sep 17 00:00:00 2001 From: Jacob Asper Date: Sun, 25 Feb 2024 02:41:54 -0500 Subject: [PATCH 8/8] Make push docs more vague --- library/alloc/src/vec/mod.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/library/alloc/src/vec/mod.rs b/library/alloc/src/vec/mod.rs index bd36b55d782fe..aaac911175d7f 100644 --- a/library/alloc/src/vec/mod.rs +++ b/library/alloc/src/vec/mod.rs @@ -1922,10 +1922,9 @@ impl Vec { /// # Time complexity /// /// Takes amortized *O*(1) time. If the vector's length would exceed its - /// capacity after the push, the capacity is doubled by allocating - /// *O*(*capacity*) space, then *O*(*capacity*) time to copy the vector's - /// elements. This expensive operation is offset by the *capacity* *O*(1) - /// insertions it allows. + /// capacity after the push, *O*(*capacity*) time is taken to copy the + /// vector's elements to a larger allocation. This expensive operation is + /// offset by the *capacity* *O*(1) insertions it allows. #[cfg(not(no_global_oom_handling))] #[inline] #[stable(feature = "rust1", since = "1.0.0")]