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

Arc: remove unused allocation from Weak::new() #50357

Merged
merged 1 commit into from
Jun 29, 2018

Conversation

seanmonstar
Copy link
Contributor

It seems non-obvious that calling Weak::new() actually allocates space for the entire size of T, even though you can never access that data from such a constructed weak pointer. Besides that, if someone were to create many Weak:new()s, they could be unknowingly wasting a bunch of memory.

This change makes it so that Weak::new() allocates no memory at all. Instead, it is created with a null pointer. The only things done with a Weak are trying to upgrade, cloning, and dropping, meaning there are very few places that the code actually needs to check if the pointer is null.

@jonas-schievink
Copy link
Contributor

In order to not lose the NonNull optimization, would it be possible to use a known-invalid non-zero address instead (the same trick that's used for ZST allocations)?

@seanmonstar
Copy link
Contributor Author

@jonas-schievink I suppose it could be, would such trickery be worth it though? The only thing you do with Weak is upgrade, which already needs an atomic CAS loop.

@rust-highfive

This comment has been minimized.

@jonas-schievink
Copy link
Contributor

I think what matters most is how Weak is usually stored. I'm not really sure if something like Option<Weak> is common or not. Maybe someone else knows more.

@rust-highfive

This comment has been minimized.

@rust-highfive

This comment has been minimized.

@Mark-Simulacrum
Copy link
Member

It seems potentially common to have Option<Weak<T>> in code; a cursory search of crater's list of crates yielded quite a few results.

@seanmonstar
Copy link
Contributor Author

So, I could change it back to holding a NonNull, though it sounds weird to me to have a NonNull that is technically not-null-but-totally-null. Again, is such trickiness worth a decreased size of Option<Weak>?

@arthurprs
Copy link
Contributor

I think Option<Weak<_>> Is a common pattern for parent pointers, so it's probably worth the trouble.

@kennytm kennytm added T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 2, 2018
@rust-highfive
Copy link
Collaborator

The job x86_64-gnu-llvm-3.9 of your PR failed on Travis (raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.
[00:03:40]    Compiling rustc_tsan v0.0.0 (file:///checkout/src/librustc_tsan)
[00:03:57]    Compiling libc v0.0.0 (file:///checkout/src/rustc/libc_shim)
[00:03:57]    Compiling alloc v0.0.0 (file:///checkout/src/liballoc)
[00:03:57]    Compiling std_unicode v0.0.0 (file:///checkout/src/libstd_unicode)
[00:03:57] error: incorrect close delimiter: `)`
[00:03:57]    --> liballoc/arc.rs:809:44
[00:03:57]     |
[00:03:57] 809 |             let weak = Weak { ptr: this.ptr) };
[00:03:57]     |
[00:03:57] note: unclosed delimiter
[00:03:57]    --> liballoc/arc.rs:809:29
[00:03:57]     |
[00:03:57]     |
[00:03:57] 809 |             let weak = Weak { ptr: this.ptr) };
[00:03:57] 
[00:03:57] 
[00:03:57] error: unexpected close delimiter: `}`
[00:03:57]    --> liballoc/arc.rs:833:1
[00:03:57] 833 | }
[00:03:57]     | ^
[00:03:57] 
[00:03:57] error: aborting due to 2 previous errors
[00:03:57] error: aborting due to 2 previous errors
[00:03:57] 
[00:03:57] error: Could not compile `alloc`.
[00:03:57] 
[00:03:57] Caused by:
[00:03:57]   process didn't exit successfully: `/checkout/obj/build/bootstrap/debug/rustc --crate-name alloc liballoc/lib.rs --color always --error-format json --crate-type lib --emit=dep-info,link -C opt-level=3 -C metadata=5c6ca57f52fc716b -C extra-filename=-5c6ca57f52fc716b --out-dir /checkout/obj/build/x86_64-unknown-linux-gnu/stage0-std/x86_64-unknown-linux-gnu/release/deps --target x86_64-unknown-linux-gnu -L dependency=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-std/x86_64-unknown-linux-gnu/release/deps -L dependency=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-std/release/deps --extern compiler_builtins=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-std/x86_64-unknown-linux-gnu/release/deps/libcompiler_builtins-bad063b3019d016c.rlib --extern core=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-std/x86_64-unknown-linux-gnu/release/deps/libcore-fb1e36473ec4786e.rlib -L native=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-std/x86_64-unknown-linux-gnu/release/build/compiler_builtins-af41331a61619951/out` (exit code: 101)
 ./.git/modules/src/tools/clippy/objects/pack
8936 ./src/doc/book
8744 ./src/tools/lld/test
8568 ./src/llvm/lib/CodeGen

I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact @TimNN. (Feature Requests)

@rust-highfive
Copy link
Collaborator

The job x86_64-gnu-llvm-3.9 of your PR failed on Travis (raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.
[00:04:38]    Compiling panic_abort v0.0.0 (file:///checkout/src/libpanic_abort)
[00:04:39] error[E0308]: mismatched types
[00:04:39]     --> liballoc/arc.rs:1000:45
[00:04:39]      |
[00:04:39] 1000 |                 ptr: NonNull::new_unchecked(WEAK_EMPTY as *mut T),
[00:04:39]      |                                             ^^^^^^^^^^^^^^^^^^^^ expected struct `arc::ArcInner`, found type parameter
[00:04:39]      |
[00:04:39]      = note: expected type `*mut arc::ArcInner<T>`
[00:04:39]                 found type `*mut T`
[00:04:39] error[E0308]: mismatched types
[00:04:39]     --> liballoc/arc.rs:1065:58
[00:04:39]      |
[00:04:39]      |
[00:04:39] 1065 |                     ptr: unsafe { NonNull::new_unchecked(self.ptr) },
[00:04:39]      |                                                          ^^^^^^^^ expected *-ptr, found struct `core::ptr::NonNull`
[00:04:39]      |
[00:04:39]      = note: expected type `*mut arc::ArcInner<T>`
[00:04:39]                 found type `core::ptr::NonNull<arc::ArcInner<T>>`
[00:04:39] 
[00:04:39] error[E0606]: casting `*mut arc::ArcInner<T>` as `usize` is invalid
[00:04:39]     --> liballoc/arc.rs:1037:24
[00:04:39]      |
[00:04:39] 1037 |         let inner = if self.ptr.as_ptr() as usize == WEAK_EMPTY {
[00:04:39]      |
[00:04:39]      |
[00:04:39]      = help: cast through a thin pointer first
[00:04:39] 
[00:04:39] error[E0277]: the trait bound `T: core::marker::Sized` is not satisfied
[00:04:39]     --> liballoc/arc.rs:1090:20
[00:04:39]      |
[00:04:39] 1090 |             return Weak::new();
[00:04:39]      |                    ^^^^^^^^^ `T` does not have a constant size known at compile-time
[00:04:39]      |
[00:04:39]      = help: the trait `core::marker::Sized` is not implemented for `T`
[00:04:39]      = help: consider adding a `where T: core::marker::Sized` bound
[00:04:39] note: required by `<arc::Weak<T>>::new`
[00:04:39]     --> liballoc/arc.rs:997:5
[00:04:39]      |
[00:04:39] 997  |     pub fn new() -> Weak<T> {
[00:04:39] 
[00:04:39] 
[00:04:39] error[E0606]: casting `*mut arc::ArcInner<T>` as `usize` is invalid
[00:04:39]     --> liballoc/arc.rs:1089:24
[00:04:39]      |
[00:04:39] 1089 |         let inner = if self.ptr.as_ptr() as usize == WEAK_EMPTY {
[00:04:39]      |
[00:04:39]      |
[00:04:39]      = help: cast through a thin pointer first
[00:04:39] error[E0308]: mismatched types
[00:04:39]     --> liballoc/arc.rs:1176:55
[00:04:39]      |
[00:04:39]      |
[00:04:39] 1176 |                 let non_null = NonNull::new_unchecked(self.ptr);
[00:04:39]      |                                                       ^^^^^^^^ expected *-ptr, found struct `core::ptr::NonNull`
[00:04:39]      |
[00:04:39]      = note: expected type `*mut _`
[00:04:39]                 found type `core::ptr::NonNull<arc::ArcInner<T>>`
[00:04:39] 
[00:04:39] error[E0606]: casting `*mut arc::ArcInner<T>` as `usize` is invalid
[00:04:39]     --> liballoc/arc.rs:1167:24
[00:04:39]      |
[00:04:39] 1167 |         let inner = if self.ptr.as_ptr() as usize == WEAK_EMPTY {
[00:04:39]      |
[00:04:39]      |
[00:04:39]      = help: cast through a thin pointer first
[00:04:42] error: aborting due to 7 previous errors
[00:04:42] 
[00:04:42] Some errors occurred: E0277, E0308, E0606.
[00:04:42] For more information about an error, try `rustc --explain E0277`.
[00:04:42] For more information about an error, try `rustc --explain E0277`.
[00:04:42] error: Could not compile `alloc`.
[00:04:42] 
[00:04:42] Caused by:
[00:04:42]   process didn't exit successfully: `/checkout/obj/build/bootstrap/debug/rustc --crate-name alloc liballoc/lib.rs --color always --error-format json --crate-type lib --emit=dep-info,link -C opt-level=3 -C metadata=5c6ca57f52fc716b -C extra-filename=-5c6ca57f52fc716b --out-dir /checkout/obj/build/x86_64-unknown-linux-gnu/stage0-std/x86_64-unknown-linux-gnu/release/deps --target x86_64-unknown-linux-gnu -L dependency=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-std/x86_64-unknown-linux-gnu/release/deps -L dependency=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-std/release/deps --extern compiler_builtins=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-std/x86_64-unknown-linux-gnu/release/deps/libcompiler_builtins-bad063b3019d016c.rlib --extern core=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-std/x86_64-unknown-linux-gnu/release/deps/libcore-fb1e36473ec4786e.rlib -L native=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-std/x86_64-unknown-linux-gnu/release/build/compiler_builtins-af41331a61619951/out` (exit code: 101)
[00:04:42] command did not execute successfully: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo" "build" "--target" "x86_64-unknown-linux-gnu" "--release" "--locked" "--color" "always" "--features" "panic-unwind jemalloc backtrace" "--manifest-path" "/checkout/src/libstd/Cargo.toml" "--message-format" "json"
[00:04:42] expected success, got: exit code: 101
[00:04:42] thread 'main' panicked at 'cargo must succeed', bootstrap/compile.rs:1091:9
[00:04:42] travis_fold:end:stage0-std

[00:04:42] travis_time:end:stage0-std:start=1525289571517417792,finish=1525289627748884126,duration=56231466334


[00:04:42] failed to run: /checkout/obj/build/bootstrap/debug/bootstrap test src/tools/tidy
[00:04:42] Build completed unsuccessfully in 0:00:57
[00:04:42] Makefile:79: recipe for target 'tidy' failed
[00:04:42] make: *** [tidy] Error 1

The command "stamp sh -x -c "$RUN_SCRIPT"" exited with 2.
travis_time:start:05213b80
$ date && (curl -fs --head https://google.com | grep ^Date: | sed 's/Date: //g' || true)

I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact @TimNN. (Feature Requests)

@rust-highfive
Copy link
Collaborator

The job x86_64-gnu-llvm-3.9 of your PR failed on Travis (raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.
[00:03:25]    Compiling alloc v0.0.0 (file:///checkout/src/liballoc)
[00:03:25]    Compiling std_unicode v0.0.0 (file:///checkout/src/libstd_unicode)
[00:03:27]    Compiling alloc_system v0.0.0 (file:///checkout/src/liballoc_system)
[00:03:27]    Compiling panic_abort v0.0.0 (file:///checkout/src/libpanic_abort)
[00:03:27] error[E0606]: casting `usize` as `*mut arc::ArcInner<T>` is invalid
[00:03:27]     --> liballoc/arc.rs:1037:45
[00:03:27]      |
[00:03:27] 1037 |         let inner = if self.ptr.as_ptr() == WEAK_EMPTY as *mut _ {
[00:03:27] 
[00:03:27] 
[00:03:27] error[E0277]: the trait bound `T: core::marker::Sized` is not satisfied
[00:03:27]     --> liballoc/arc.rs:1090:20
[00:03:27]      |
[00:03:27] 1090 |             return Weak::new();
[00:03:27]      |                    ^^^^^^^^^ `T` does not have a constant size known at compile-time
[00:03:27]      |
[00:03:27]      = help: the trait `core::marker::Sized` is not implemented for `T`
[00:03:27]      = help: consider adding a `where T: core::marker::Sized` bound
[00:03:27] note: required by `<arc::Weak<T>>::new`
[00:03:27]     --> liballoc/arc.rs:997:5
[00:03:27]      |
[00:03:27] 997  |     pub fn new() -> Weak<T> {
[00:03:27] 
[00:03:27] 
[00:03:27] error[E0606]: casting `usize` as `*mut arc::ArcInner<T>` is invalid
[00:03:27]     --> liballoc/arc.rs:1089:45
[00:03:27]      |
[00:03:27] 1089 |         let inner = if self.ptr.as_ptr() == WEAK_EMPTY as *mut _ {
[00:03:27] 
[00:03:27] error[E0308]: mismatched types
[00:03:27]     --> liballoc/arc.rs:1176:55
[00:03:27]      |
[00:03:27]      |
[00:03:27] 1176 |                 let non_null = NonNull::new_unchecked(self.ptr);
[00:03:27]      |                                                       ^^^^^^^^ expected *-ptr, found struct `core::ptr::NonNull`
[00:03:27]      |
[00:03:27]      = note: expected type `*mut _`
[00:03:27]                 found type `core::ptr::NonNull<arc::ArcInner<T>>`
[00:03:27] 
[00:03:27] error[E0606]: casting `usize` as `*mut arc::ArcInner<T>` is invalid
[00:03:27]     --> liballoc/arc.rs:1167:45
[00:03:27]      |
[00:03:27] 1167 |         let inner = if self.ptr.as_ptr() == WEAK_EMPTY as *mut _ {
[00:03:27] 
[00:03:29] error: aborting due to 5 previous errors
[00:03:29] 
[00:03:29] Some errors occurred: E0277, E0308, E0606.
[00:03:29] Some errors occurred: E0277, E0308, E0606.
[00:03:29] For more information about an error, try `rustc --explain E0277`.
[00:03:29] error: Could not compile `alloc`.
[00:03:29] 
[00:03:29] Caused by:
[00:03:29]   process didn't exit successfully: `/checkout/obj/build/bootstrap/debug/rustc --crate-name alloc liballoc/lib.rs --color always --error-format json --crate-type lib --emit=dep-info,link -C opt-level=3 -C metadata=5c6ca57f52fc716b -C extra-filename=-5c6ca57f52fc716b --out-dir /checkout/obj/build/x86_64-unknown-linux-gnu/stage0-std/x86_64-unknown-linux-gnu/release/deps --target x86_64-unknown-linux-gnu -L dependency=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-std/x86_64-unknown-linux-gnu/release/deps -L dependency=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-std/release/deps --extern core=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-std/x86_64-unknown-linux-gnu/release/deps/libcore-fb1e36473ec4786e.rlib --extern compiler_builtins=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-std/x86_64-unknown-linux-gnu/release/deps/libcompiler_builtins-bad063b3019d016c.rlib -L native=/checkout/obj/build/x86_64-unknown-linux-gnu/stage0-std/x86_64-unknown-linux-gnu/release/build/compiler_builtins-af41331a61619951/out` (exit code: 101)
[00:03:29] command did not execute successfully: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo" "build" "--target" "x86_64-unknown-linux-gnu" "--release" "--locked" "--color" "always" "--features" "panic-unwind jemalloc backtrace" "--manifest-path" "/checkout/src/libstd/Cargo.toml" "--message-format" "json"
[00:03:29] expected success, got: exit code: 101
[00:03:29] thread 'main' panicked at 'cargo must succeed', bootstrap/compile.rs:1091:9
[00:03:29] travis_fold:end:stage0-std

[00:03:29] travis_time:end:stage0-std:start=1525290261655725535,finish=1525290299296669614,duration=37640944079


[00:03:29] failed to run: /checkout/obj/build/bootstrap/debug/bootstrap test src/tools/tidy
[00:03:29] Build completed unsuccessfully in 0:00:39
[00:03:29] Makefile:79: recipe for target 'tidy' failed
[00:03:29] make: *** [tidy] Error 1

The command "stamp sh -x -c "$RUN_SCRIPT"" exited with 2.
travis_time:start:090c2da8
$ date && (curl -fs --head https://google.com | grep ^Date: | sed 's/Date: //g' || true)

I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact @TimNN. (Feature Requests)

weak: atomic::AtomicUsize::new(1),
data: uninitialized(),
}),
ptr: NonNull::new_unchecked(WEAK_EMPTY as *mut _),
Copy link
Member

Choose a reason for hiding this comment

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

😕 Just use NonNull::dangling() here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That picks a value based on the alignment size of T. While RawVec can check cap if its pointer is actually null, Weak doesn't have that. So, to check, dangling would need to be called each time.

It's also not clear to me what having a never-used pointer to 8, or 16, etc would provide.

Copy link
Member

Choose a reason for hiding this comment

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

I don't think calling dangling every time is a problem.

Copy link
Contributor

Choose a reason for hiding this comment

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

It shouldn't be a problem, the value is constant for the input type.

It may avoid memory access errors in debuggers, or avoid tripping unaligned access warnings in tools like Valgrind (if that's a thing).

Copy link
Contributor

Choose a reason for hiding this comment

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

In #41064 we changed all dangling pointers to be aligned rather than 1. There is no reason for this case to be different.

dangling() optimizes to a constant, there is not issue with calling it many times.

@rust-highfive
Copy link
Collaborator

The job x86_64-gnu-llvm-3.9 of your PR failed on Travis (raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.
4.3.11(1)-release
travis_fold:start:before_install.1
travis_time:start:12cdaa85
$ if [ "$TRAVIS_OS_NAME" = linux ]; then pip install --user awscli; export PATH=$PATH:$HOME/.local/bin; fi
  Retrying (Retry(total=4, connect=None, read=None, redirect=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7fbe74616550>, 'Connection to pypi.org timed out. (connect timeout=15)')': /simple/awscli/
  InsecurePlatformWarning
Collecting awscli
/usr/local/lib/python2.7/dist-packages/pip/_vendor/requests/packages/urllib3/util/ssl_.py:318: SNIMissingWarning: An HTTPS request has been made, but the SNI (Subject Name Indication) extension to TLS is not available on this platform. This may cause the server to present an incorrect TLS certificate, which can cause validation failures. You can upgrade to a newer version of Python to solve this. For more information, see https://urllib3.readthedocs.io/en/latest/security.html#snimissingwarning.
  SNIMissingWarning
---
    50% |████████████████                | 2.1MB 9.1MB/s eta 0:00:01
    50% |████████████████                | 2.1MB 10.8MB/s eta 0:00:01
    50% |████████████████▏               | 2.1MB 10.1MB/s eta 0:00:01
    50% |████████████████▎               | 2.1MB 10.1MB/s eta 0:00:01
    51% |████████████████▍               | 2.2MB 10.7MB/s [K    53% |█████████████████               | 2.2MB 9.8MB/s eta 0:00:01
    53% |█████████████████▏              | 2.3MB 9.7MB/s eta 0:00:01
    53% |█████████████████▎              | 2.3MB 12.2MB/s eta 0:00:01
    54% |█████████████████▍              | 2.3MB 12.8MB/s eta 0:00:01
    54% |█████████████████▍              | 2.3MB 11.5MB/s eta 0:00:01
---
[00:00:01] Cloning into '/home/travis/build/rust-lang/rust/src/libcompiler_builtins'...
[00:00:01] Cloning into '/home/travis/build/rust-lang/rust/src/doc/reference'...
[00:03:33] curl: (7) Failed to connect to github.com port 443: Connection timed out
[00:03:33] Command failed. Attempt 2/5:
[00:03:33] curl: (7) Failed to connect to codeload.github.com port 443: Connection timed out
[00:00:02] Cloning into '/home/travis/build/rust-lang/rust/src/tools/rls'...
[00:00:02] Cloning into '/home/travis/build/rust-lang/rust/src/tools/miri'...
[00:00:02] Cloning into '/home/travis/build/rust-lang/rust/src/jemalloc'...
[00:03:34] fatal: unable to access 'https://github.com/rust-lang-nursery/reference.git/': Failed to connect to github.com port 443: Connection timed out
---
 74  234M   74  175M    0     0  17.1M      0  0:00:13  0:00:10  0:00:03 24.8M
 84  234M   84  199M    0     0  17.7M      0  0:00:13  0:00:11  0:00:02 25.0M
 97  234M   97  229M    0     0  18.7M      0  0:00:12  0:00:12 --:--:-- 25.4M
100  234M  100  234M    0     0  18.9M      0  0:00:12  0:00:12 --:--:-- 25.7M
[00:06:17] error pulling image configuration: Get https://dseasb33srnrn.cloudfront.net/registry-v2/docker/registry/v2/blobs/sha256/0b/0b1edfbffd27c935a666e233a0042ed634205f6f754dbe20769a60369c614f85/data?Expires=1525338194&Signature=a5vwPZRls8ZBBKQP2Xj5gVIUohcy8boPr2ZzZObOKe5QbRufhFGwMcVhilE~rjJ81kwQEInhBbrGVJZbsg4mKhg-5G8vy4fWoHdvoYPCVbae1KTiofXBCiYSyeANfAWcdOsuYjcHjgaId6kwe-b2hZA7LjwM7TCmfat0wMTDD7w_&Key-Pair-Id=APKAJECH5M7VWIS5YZ6Q: net/http: TLS handshake timeout
[00:06:17] Sending build context to Docker daemon  507.9kB
[00:06:17] Step 1/6 : FROM ubuntu:16.04
[00:00:42] Downloaded containers:\nsha256:0cc55b95eafd2ee0982fd3cb65a292999da0ec7437e4e34f981cb5895355ee46
[00:00:42] sha256:81a974266f4ea16f006d530771b8e1f54050a8c7f1678f5fb3cdb7da1242e7eb
---
##########################################################                81.3%
#####################################################################     96.3%
######################################################################## 100.0%
[00:06:33] 686596545a94: Waiting
[00:06:33] error pulling image configuration: Get https://dseasb33srnrn.cloudfront.net/registry-v2/docker/registry/v2/blobs/sha256/0b/0b1edfbffd27c935a666e233a0042ed634205f6f754dbe20769a60369c614f85/data?Expires=1525338206&Signature=VIrrgytO-b~A6tADutZ42NoTW2zroWXe2n3sGSQGl4kp9OZUzBRDGYpafaQYU-63Lns-nhsLA57oVMcOKTSqJNfPYqrypLe4sIoel29mEox3zoGCZ8jYxZOLoOcJgLGjcekY1UYdqWbdCRfPZKosX~V56vGirLFBJ7CTKPkriXw_&Key-Pair-Id=APKAJECH5M7VWIS5YZ6Q: net/http: TLS handshake timeout
[00:06:33] Sending build context to Docker daemon  507.9kB
[00:06:33] Step 1/6 : FROM ubuntu:16.04
[00:00:52] extracting /checkout/obj/build/cache/2018-04-24/rust-std-beta-x86_64-unknown-linux-gnu.tar.gz
[00:00:52] downloading https://static.rust-lang.org/dist/2018-04-24/rustc-beta-x86_64-unknown-linux-gnu.tar.gz
---
####################################################################      95.2%
######################################################################    97.8%
#######################################################################   99.6%
######################################################################## 100.0%
[00:07:10] error pulling image configuration: Get https://dseasb33srnrn.cloudfront.net/registry-v2/docker/registry/v2/blobs/sha256/0b/0b1edfbffd27c935a666e233a0042ed634205f6f754dbe20769a60369c614f85/data?Expires=1525338234&Signature=KJtr7eoz5-iwK4LVsUyVg0OKK0p8wZIGFFAa~nZcG5iTm7hHg5rpMFQuv1dWLdG9pDhKXlV24ez~E3VYgowtWXT1Z16Q5ZbSeOcM8~5s3~4cABOTSsmVJ~v5Ntk8dgm7e0gLZSf7hqShhOif~iYgM~F1-sID22QYX57SMZO~3B0_&Key-Pair-Id=APKAJECH5M7VWIS5YZ6Q: net/http: TLS handshake timeout
[00:07:10] Sending build context to Docker daemon  507.9kB
[00:07:10] Step 1/6 : FROM ubuntu:16.04
[00:01:11] extracting /checkout/obj/build/cache/2018-04-24/rustc-beta-x86_64-unknown-linux-gnu.tar.gz
[00:07:15] 16.04: Pulling from library/ubuntu
---
[00:07:15] 686596545a94: Pulling fs layer
[00:07:15] 686596545a94: Waiting
[00:07:15] 8fe36b178d25: Waiting
[00:01:11] downloading https://static.rust-lang.org/dist/2018-04-24/cargo-beta-x86_64-unknown-linux-gnu.tar.gz
[00:07:36] error pulling image configuration: Get https://dseasb33srnrn.cloudfront.net/registry-v2/docker/registry/v2/blobs/sha256/0b/0b1edfbffd27c935a666e233a0042ed634205f6f754dbe20769a60369c614f85/data?Expires=1525338267&Signature=EeOAc5MtU2cwttbIVpUFDJkqxGEAC0Rk4z~AkNUs8rNxxa-Wfcd75ZbtBU~bit4LDNqjcSIG6-G8rsZe-zoU5iFynnasHUqTbdIQ2HO48J1kC9718vGcoJya6dtBWv3WdO7Sh5esN-G48vL0Q2GwdWfWxc3okJfMLXFIZgRK4bk_&Key-Pair-Id=APKAJECH5M7VWIS5YZ6Q: net/http: TLS handshake timeout
[00:07:36] Sending build context to Docker daemon  507.9kB
[00:07:36] Step 1/6 : FROM ubuntu:16.04
[00:01:12] 
################################                                          45.2%
---
[00:08:03] 686596545a94: Pulling fs layer
[00:08:03] 8fe36b178d25: Waiting
[00:01:16]     Updating registry `https://github.com/rust-lang/crates.io-index`
[00:08:21] 686596545a94: Waiting
[00:08:21] error pulling image configuration: Get https://dseasb33srnrn.cloudfront.net/registry-v2/docker/registry/v2/blobs/sha256/0b/0b1edfbffd27c935a666e233a0042ed634205f6f754dbe20769a60369c614f85/data?Expires=1525338312&Signature=Nbx8egNALD8zMGftOrej24JlGBknLc6V5vIjrUPYb47M71Zj0CoCoymhh2oIZ9F7qiZBBwsGIokkfTAapn~QBwpDPMAbOYoyUmzPDPCx0Exk~5J3fpkav0NyCfNWO4ykE21xKeHi9rbzkYCjP83bn~2aWJArX2IDgm~gcYzZyAY_&Key-Pair-Id=APKAJECH5M7VWIS5YZ6Q: net/http: TLS handshake timeout
[00:08:21] The command has failed after 5 attempts.

The command "stamp sh -x -c "$RUN_SCRIPT"" exited with 1.
travis_time:start:05e8f5f0
$ date && (curl -fs --head https://google.com | grep ^Date: | sed 's/Date: //g' || true)
---
12160 ./src/llvm-emscripten/include/llvm
11900 ./src/tools/lld
11744 ./src/doc
10052 ./src/test/compile-fail
10012 ./src/llvm/test/MC/AMDGPU
9648 ./src/llvm/test/MC/Disassembler/AMDGPU
9336 ./.git/modules/src/tools/clippy/objects
9328 ./.git/modules/src/tools/c[00:01:34]  Downloading lazy_static v0.2.11
[00:01:34]  Downloading getopts v0.2.17
[00:01:34]  Downloading filetime v0.1.15

I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact @TimNN. (Feature Requests)

@shepmaster
Copy link
Member

Ping from triage, @seanmonstar and @kennytm ! What's the status here?

@seanmonstar
Copy link
Contributor Author

@shepmaster looks like the CI failure is spurious. It's tagged waiting on review, is that correct, or is that blocked on me?

@kennytm
Copy link
Member

kennytm commented May 8, 2018

@shepmaster @seanmonstar This is blocked by the lack of a reviewer 😐

@shepmaster
Copy link
Member

by the lack of a reviewer 😐

haha, I thought you were the reviewer!

random says...

r? @KodrAus

@seanmonstar
Copy link
Contributor Author

I can resolve the conflicts. I no longer have the benchmark code setup, is it needed to see Weak::upgrade?

@KodrAus
Copy link
Contributor

KodrAus commented Jun 28, 2018

Thanks for your patience with this @seanmonstar. I figured since we had the little bit of extra logic in upgrade it would be good to look at. So I went ahead and benched Weak::upgrade using the following:

#![feature(test)]
extern crate test;

use std::sync::{Arc, Weak};

#[bench]
fn weak_new(b: &mut test::Bencher) {
    b.iter(|| test::black_box(Weak::<()>::new()));
}

#[bench]
fn weak_upgrade_none(b: &mut test::Bencher) {
    let w = Weak::<i32>::new();

    b.iter(|| test::black_box(w.upgrade()));
}

#[bench]
fn weak_upgrade_some(b: &mut test::Bencher) {
    let a = Arc::new(42);
    let w = Arc::downgrade(&a);

    b.iter(|| test::black_box(w.upgrade()));
}

On my machine I get:

master: test weak_new                 ... bench:          21 ns/iter (+/- 1)
arc-weak-null: test weak_new          ... bench:           0 ns/iter (+/- 0)

master: test weak_upgrade_none        ... bench:           1 ns/iter (+/- 0)
arc-weak-null: test weak_upgrade_none ... bench:           1 ns/iter (+/- 0)

master: test weak_upgrade_some        ... bench:          15 ns/iter (+/- 2)
arc-weak-null: test weak_upgrade_some ... bench:          16 ns/iter (+/- 2)

The difference looks negligible to me. I'm happy to r+ again after we resolve the conflicts.

@KodrAus
Copy link
Contributor

KodrAus commented Jun 28, 2018

It looks like we're get the benefits of not allocating Weak::new when we don't need to without incurring a noticeable cost for other operations. So...

@bors r+

@bors
Copy link
Contributor

bors commented Jun 28, 2018

📌 Commit 24ce259 has been approved by KodrAus

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 28, 2018
@bors
Copy link
Contributor

bors commented Jun 29, 2018

⌛ Testing commit 24ce259 with merge 3b50455...

bors added a commit that referenced this pull request Jun 29, 2018
Arc: remove unused allocation from Weak::new()

It seems non-obvious that calling `Weak::new()` actually allocates space for the entire size of `T`, even though you can **never** access that data from such a constructed weak pointer. Besides that, if someone were to create many `Weak:new()`s, they could be unknowingly wasting a bunch of memory.

This change makes it so that `Weak::new()` allocates no memory at all. Instead, it is created with a null pointer. The only things done with a `Weak` are trying to upgrade, cloning, and dropping, meaning there are very few places that the code actually needs to check if the pointer is null.
@bors
Copy link
Contributor

bors commented Jun 29, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: KodrAus
Pushing 3b50455 to master...

@RalfJung
Copy link
Member

Why do this for Arc but not Rc?

@SimonSapin
Copy link
Contributor

Same for Rc: #51901

SimonSapin added a commit that referenced this pull request Jun 29, 2018
SimonSapin added a commit that referenced this pull request Jul 4, 2018
bors added a commit that referenced this pull request Jul 6, 2018
Rc: remove unused allocation and fix segfault in Weak::new()

Same as #50357 did for `Arc`.

Fixes #48493
SimonSapin added a commit that referenced this pull request Jul 6, 2018
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this pull request Jul 7, 2018
…hton

Rc: remove unused allocation and fix segfault in Weak::new()

Same as rust-lang#50357 did for `Arc`.

Fixes rust-lang#48493
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 16, 2021
Correct outdated documentation for rc::Weak

This was overlooked in ~~rust-lang#50357~~ rust-lang#51901
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.