These are chat archives for rust-lang/rust

22nd
Nov 2017
Sean
@seanr707
Nov 22 2017 04:00

Anyone know why my gtk-rs app is getting progressively slower as the app lives?

After around 30s - 1min the front end (GTK) starts to lag like crazy or outright freeze

Sean Perry
@shaleh
Nov 22 2017 05:54
Can I write a include_if macro which checks for the file to exist before it tries to include it? My naive attempt failed before I used a let in the macro to store the Path.
Aleksey Kladov
@matklad
Nov 22 2017 08:09
@shaleh I am not sure that's possible. Probably a build script would be a better approach for this use-case though? You can do anything in a bulid script.
Sean Perry
@shaleh
Nov 22 2017 08:19
@matklad fair enough. We already have a build script generating code, so a little more won't hurt :-)
Sherab Giovannini
@Shaddy
Nov 22 2017 10:11
Hi guys! I'm doing some ffi bindings and need to cast a Vec<u8> (8 sized) to a u64, I'm playing with transmute but seems that it requires some kind of [u8; 8] to be compatible
any ideas?
Aleksey Kladov
@matklad
Nov 22 2017 10:16
Have looked at byteorder crate?
Also, using Vec<u8> for u64 seems like an overkill...
Sherab Giovannini
@Shaddy
Nov 22 2017 10:16
why?
well, I could understand your point but it is not always a u64
it is a windows i/o channel to receive different buffers
so sometimes it could be just a u64, or a more complex struct
so as a ioctl buffer I'm using a Vec<u8>, which don't think is a bad decision, is it?
Aleksey Kladov
@matklad
Nov 22 2017 10:19
If there's an upper bound on the size of the output, using a stack allocated struct will be more efficient.
Sherab Giovannini
@Shaddy
Nov 22 2017 10:20
well, buffers could be sometimes 4K, I think it is kinda huge for an allocation in stack
Aleksey Kladov
@matklad
Nov 22 2017 10:20
I am not sure how exactely the C api is looking, but I expect that a low lever wrapper accepts something like &mut [u8], and a higher-level layer could then choose appropriate buffer (either stack allocated or heap allocated).
Sherab Giovannini
@Shaddy
Nov 22 2017 10:20
maybe it could have an optimization for those cases with less memory, but I would prefer to start with the biggest possibility
yup, the client (Windows DeviceIoControl) requires a pointer to a u8 and a limit (OutputBuffer / OutputLength)
using stack or heap for me is just an optimization, what I really would like to know is what's the best way to extract an u64 having a Vec<u8> with size 8
but in an optimal way
like in C, a direct cast
Aleksey Kladov
@matklad
Nov 22 2017 10:22

Like, if you statically know that you are expecting u64, it should be possible to use [u8; 8]. But I am probably wrong, because I don't know the precise API here :)

Though byteorder is defenitelly the best way to go from [u8] to primitives.

Sherab Giovannini
@Shaddy
Nov 22 2017 10:23
all right, so byteorder should be the candidate and optimal choice
I can't give static types because the C API requires sometimes [u8; 8] or [u8; 0x10000]
Sherab Giovannini
@Shaddy
Nov 22 2017 10:35

How does this works

let mut rdr = Cursor::new(vec![2, 5, 3, 0]);
assert_eq!(0x8, rdr.read_u64::<BigEndian>().unwrap()

compared to a

unsigned char buffer[8];

unsigned int64 = (unsigned int64) buffer;
Aleksey Kladov
@matklad
Nov 22 2017 10:48
The first one reads bytes one-by one, and assembles them into an integer, the second one probably just reinterprets memory
Sherab Giovannini
@Shaddy
Nov 22 2017 10:50
thats the point, reading one by one is not really optimal
isn't there an unsafe way to just reinterpret memory?
Jonas Platte
@jplatte
Nov 22 2017 10:51
There is, but I'm pretty sure byteorder doesn't read bytes one-by-one, esp. not if the endianness you want is the endianness of your (target) architecture
Sherab Giovannini
@Shaddy
Nov 22 2017 10:51
it should be done by using transmute, but can't find a way to unsafely transmute a Vec<u8> (8 sized) to a u64
i'll take a look over the code, you're probably right
Denis Lisov
@tanriol
Nov 22 2017 10:52
@Shaddy Are there lots of these that you care about this possible minor performance loss?
Sherab Giovannini
@Shaddy
Nov 22 2017 10:53
yes
it will be really intensive, I'll require to destructure data most times for example, for each native call of a process
or as many times as a firewall would inspect a packet
Aleksey Kladov
@matklad
Nov 22 2017 10:54
Here's how byteorder does this if endianess match: https://github.com/BurntSushi/byteorder/blob/master/src/lib.rs#L1705
Jonas Platte
@jplatte
Nov 22 2017 10:55
@matklad That code is used regardless of endianness
the important part is the last line, data.$which()
$which is either to_be or to_le
but the implementation of those are dependent on target architecture
Sherab Giovannini
@Shaddy
Nov 22 2017 10:59
well it steams that it fits pretty much with requirements, and internally does exactly what I want
thanks guys, I wont steal more time :)
Aleksey Kladov
@matklad
Nov 22 2017 10:59
Wow, I didn't realized that to_be and to_le are build in since 1.0! Thanks a lot for pointing that out @jplatte ! All this time ignorant me was sure that there's a macro generated loop with <<8 somewhere inside byteorder...
Jonas Platte
@jplatte
Nov 22 2017 11:01
@matklad I haven't found the actual implementation of swap_bytes yet... But I'd still assume that it is as efficient as is possible
Aleksey Kladov
@matklad
Nov 22 2017 11:03
@jplatte there's bswap in core::intrinsics, so it goes directly into LLVM I think
Sherab Giovannini
@Shaddy
Nov 22 2017 11:07
byteorder works like a charm XD
Fra ns
@snarf95_twitter
Nov 22 2017 12:21
Quick question:
I updated my nightly version of rustc thru rustup and now I'm experiencing a crash when I run my app
Is there a way to downgrade a rust nightly toolchain?
Steve Klabnik
@steveklabnik
Nov 22 2017 12:22
what kind of crash
you can do rustup update nightly-YYY-MM-DD && to get a specific nightly
Fra ns
@snarf95_twitter
Nov 22 2017 12:23
Well tbh I don't know, it's a crash in a dep
Steve Klabnik
@steveklabnik
Nov 22 2017 12:23
oops no && there sorry
Fra ns
@snarf95_twitter
Nov 22 2017 12:23
Where can I find the specific version to downgrade to?
Steve Klabnik
@steveklabnik
Nov 22 2017 12:23
but, then you'll need to use it like a totally different toolchain, that is, it doesn't change the "nightly" toolchain
Fra ns
@snarf95_twitter
Nov 22 2017 12:23
is there a list
Steve Klabnik
@steveklabnik
Nov 22 2017 12:23
no idea, it'd be the last one you used
when you updated it should have said wat it updated from
Fra ns
@snarf95_twitter
Nov 22 2017 12:23
okay, yea
Michal 'vorner' Vaner
@vorner
Nov 22 2017 12:23
@snarf95_twitter: Try to figure out what happened and report what you can. Crashes are bad. But yes, you downgrade.
The YYYY-MM-DD is a date. You say „nightly as if it was that day“
Steve Klabnik
@steveklabnik
Nov 22 2017 12:24
but yeah i'm interested in the specifics. crash like segfault? crash like ICE?
Fra ns
@snarf95_twitter
Nov 22 2017 12:24
I went from 2017-11-16 to 2017-11-21
this is the same issue: gfx-rs/gfx#1629
Steve Klabnik
@steveklabnik
Nov 22 2017 12:25
ah
Fra ns
@snarf95_twitter
Nov 22 2017 12:27
I thought maybe trying a version in between to see if it's still an issue
Ariel Ben-Yehuda
@arielb1
Nov 22 2017 13:32
I think you can
rustup install nightly-2017-11-19
@snarf95_twitter could you get a backtrace for your crash?
I think I can fix it
Fra ns
@snarf95_twitter
Nov 22 2017 13:33
sure
Ariel Ben-Yehuda
@arielb1
Nov 22 2017 13:35
we landed a PR that broke a lot of things
Fra ns
@snarf95_twitter
Nov 22 2017 13:35
give me a sec
or a couple of minutes
pc is slow today
Ariel Ben-Yehuda
@arielb1
Nov 22 2017 13:41
could you gist it
upload it to gist.github.com?
Bernhard Schuster
@drahnr
Nov 22 2017 13:42
How would I model a protocol that would require some background handling (ping pong, keep alive)?
Right now I have a core loop that would need to run all the time, and a client that feeds this core loop and gets features back.
The only question I have right now is, how to design an example around it since on the one handside I need to work on a tokio_core::net::TcpStream which gives me MqttPackets (FramedReader/Writer) and on the other handside I need to run my main loop
Fra ns
@snarf95_twitter
Nov 22 2017 13:53
running with nightly-2017-11-19-x86_64-pc-windows-msvc toolchain works
Ariel Ben-Yehuda
@arielb1
Nov 22 2017 14:09
@snarf95_twitter can you reproduce your crash on a Linux?
Fra ns
@snarf95_twitter
Nov 22 2017 14:36
@arielb1 I have not got access to a linux system..
rust-lang/rust#46182
Ariel Ben-Yehuda
@arielb1
Nov 22 2017 15:03
@msiglreith
I actually found that bug too
msiglreith
@msiglreith
Nov 22 2017 15:03
@arielb1 Oh interesting
Ariel Ben-Yehuda
@arielb1
Nov 22 2017 15:03
and it's UB
the problem is that GetProcAddress returns a PROC
msiglreith
@msiglreith
Nov 22 2017 15:04
rust-lang/rust#46158
Is it the same as here?
a PROC is an extern "system" fn()
which can't be null
@msiglreith does it really return a PROC?
Ariel Ben-Yehuda
@arielb1
Nov 22 2017 15:07
I mean, the Rust binding
returns a PROC
msiglreith
@msiglreith
Nov 22 2017 15:07
need to check
Ariel Ben-Yehuda
@arielb1
Nov 22 2017 15:07
(I don't have a Windows handy ATM, so I can't check)
@msiglreith: did you check?
msiglreith
@msiglreith
Nov 22 2017 15:14

@arielb1 Uhm it's defined like this on the rust side

#[link_name="wglGetProcAddress"]
            pub fn GetProcAddress(lpszProc: types::LPCSTR) -> types::PROC;

but don't know where to search for PROC in the windows files..

types::PROC is the same as you posted above
msiglreith
@msiglreith
Nov 22 2017 15:20
@arielb1 typedef int (WINAPI *PROC)();
Michal 'vorner' Vaner
@vorner
Nov 22 2017 15:21
Looking at the widespread wrong usage of GetProcAddress/PROC, is there a crates.io-wide grep? List all crates that have one of these?
Ariel Ben-Yehuda
@arielb1
Nov 22 2017 15:26
@msiglreith
then that's the problem
msiglreith
@msiglreith
Nov 22 2017 15:28
Great, thanks! Suggestions how to properly fix this?
Ariel Ben-Yehuda
@arielb1
Nov 22 2017 15:28
if possible
change PROC to be an *const ()
that should get rid of the problem
or #[repr(C)] struct Function; *const Function or whatever
actually, that's the only fix
msiglreith
@msiglreith
Nov 22 2017 15:30
I'm just a bit worried about the different ABIs:
#ifdef _WIN64
typedef INT_PTR (FAR WINAPI *FARPROC)();
typedef INT_PTR (NEAR WINAPI *NEARPROC)();
typedef INT_PTR (WINAPI *PROC)();
#else
typedef int (FAR WINAPI *FARPROC)();
typedef int (NEAR WINAPI *NEARPROC)();
typedef int (WINAPI *PROC)();
#endif  // _WIN64
#else
typedef int (CALLBACK *FARPROC)();
typedef int (CALLBACK *NEARPROC)();
typedef int (CALLBACK *PROC)();
#endif
#if _MSC_VER >= 1200
#pragma warning(pop)
#endif
#else
typedef INT_PTR (WINAPI *FARPROC)(void);
typedef INT_PTR (WINAPI *NEARPROC)(void);
typedef INT_PTR (WINAPI *PROC)(void);
#endif
Ariel Ben-Yehuda
@arielb1
Nov 22 2017 15:30
because IIRC we emit !nonnull on the function GetProcAddress when we see it returns a function pointer
@msiglreith on Windows a function pointer has the same size as a data pointer
always
msiglreith
@msiglreith
Nov 22 2017 15:32
awesome, thanks again!
Ariel Ben-Yehuda
@arielb1
Nov 22 2017 15:33
@msiglreith if that can't be done without a major version break
and you just want to get your code running
I suppose you could do test::black_box(|| GetProcAddress() as *const ())
but that's still UB
test::black_box(GetProcAddress() as const ())
eh that doesn't work on stable
so no
it would be hard to get your code to work
Paul Daniel Faria
@Nashenas88
Nov 22 2017 16:15
Has anyone else seen jemalloc fail to build intermittently? I'm getting this sporadically:
running: "make" "build_lib_static" "-j" "16"


command did not execute successfully: "make" "build_lib_static" "-j" "16"
expected success, got: exit code: 2



--- stderr
make: *** No rule to make target '/home/paulf/rust2/src/liballoc_jemalloc/../jemalloc/src/jemalloc.c', needed by 'src/jemalloc.o'.  Stop.
Though sometimes it skips that and just works =/
Actually, let me clean and rebuild. I wonder if this is caused by my recent rebase/cherry-picking adventures this morning.
Moggers
@Moggers
Nov 22 2017 18:09
If I have a repo with multiple service cargos in it, and a common codebase cargo
can I somehow set up a cargo build and cargo run for the services in the root
Steve Klabnik
@steveklabnik
Nov 22 2017 18:10
if they're in a workspace, then cargo build --all
Moggers
@Moggers
Nov 22 2017 18:24
its more, when I add a [[bin]] entry for the service, it uses the root Cargo.toml for dependencies instead of the service's Cargo.toml
Moggers
@Moggers
Nov 22 2017 18:30
oh I think -p is what I want
Kelly Thomas Kline
@kellytk
Nov 22 2017 20:15
Can a Rust program load additional Rust at run-time? For example, a plugin system where the plugins are compiled Rust binaries. Where in documentation should I refer to for more information?
Aleksey Kladov
@matklad
Nov 22 2017 20:16
It is possible in the same way as C does it: via dlopen
etc
Aleksey Kladov
@matklad
Nov 22 2017 20:17
yep!, @steveklabnik was faster :)
Steve Klabnik
@steveklabnik
Nov 22 2017 20:18
<3
Kelly Thomas Kline
@kellytk
Nov 22 2017 20:19
Good news, thanks
Coming from TypeScript where I used RxJS, what is the equivalent in Rust to RxJS?
Steve Klabnik
@steveklabnik
Nov 22 2017 20:19
rx isn't as popular in rust
there's a crate iirc
Moggers
@Moggers
Nov 22 2017 20:20
fn from_str<'a, T>(s: &'a str) -> Result<T> <- what if my T needs to live for longer than my string?
that was used to make it
Trying to pull a string out of redis, deserialize it, and return that
Kelly Thomas Kline
@kellytk
Nov 22 2017 20:20
What is the preferred means of achieving in Rust what RxJS provides?
Steve Klabnik
@steveklabnik
Nov 22 2017 20:20
that signature does not tie T to the string
@kellytk futures
to some degree
Paul Daniel Faria
@Nashenas88
Nov 22 2017 20:22
@Moggers that signature tells me that the Result is created inside of the fn and does not use data borrowed from s.
Moggers
@Moggers
Nov 22 2017 20:22
Yeah I just realised I misread the signature..
But my rustc is telling me that in my usage, str does not live live long enough, and must be valid for the static lifetime
Paul Daniel Faria
@Nashenas88
Nov 22 2017 20:24
Any chance you could share the code or the error message in more detail? It's hard to help out more without context.
Moggers
@Moggers
Nov 22 2017 20:25
I'm going to quickly make a minimal reproduction, at least to help myself figure out what I'm trying to do and why its not working
Kelly Thomas Kline
@kellytk
Nov 22 2017 20:32
Are the libraries libloading (https://docs.rs/libloading/0.4.2/libloading/) loads single [binary] files of compiled Rust?
Steve Klabnik
@steveklabnik
Nov 22 2017 20:36
yes
Kelly Thomas Kline
@kellytk
Nov 22 2017 20:43
Thanks @steveklabnik, as you can probably assume I'm 0-day new to Rust :-)
Moggers
@Moggers
Nov 22 2017 20:46
turns out it's the lifetime I put on the Deserialize of my trait
Steve Klabnik
@steveklabnik
Nov 22 2017 20:50
@kellytk np :)