These are chat archives for rust-lang/rust

25th
Apr 2018
Andy Grove
@andygrove
Apr 25 2018 04:04
I have a question on HashMap performance ... I've been happily using the let entry = map.entry(key).or_insert_with(|| ...) idiom but it means I have to create a new key each time which in my case is a Vec
(I'm processing millions of rows of data)
I would like to do the above but pass in a &key instead so I can re-use the vec for each row
so I started down the path of map.get(&key)
but then in my None arm I want to call map.insert but I can't borrow map as mutable because blah blah blah
any suggestions?
Andy Grove
@andygrove
Apr 25 2018 04:41
I ended up doing something like this
let x = match map.get(&key) {
    Some(entry) => {
        // do update
        true
    }
    None => false
};

if !x {
    let entry = create_aggregate_entry(aggr_expr);
    {
        let mut entry_mut = entry.borrow_mut();
        // update it
    }
    map.insert(key.clone(), entry);
}
got some good speed improvements but now my code is ugly
Michal 'vorner' Vaner
@vorner
Apr 25 2018 05:28
@andygrove You're not the first to notice, there's an RFC that should solve this problem. But it is not possible yet. Another workaround would be to use Rc<Key>, but that could hurt performance too, due to the allocations.
Anyway, if you tune performance around hash maps… are you aware that the default hasher is something cryptographically strong or so and that there are faster ones if you can afford it?
Paul Masurel
@fulmicoton
Apr 25 2018 05:30
@andrygrove Note that your solution is not optimal either, because you have to pay for the lookup twice if you end up inserting
Dylan DPC
@Dylan-DPC
Apr 25 2018 11:11
say suppose i have a struct that's working on contents from a file. what's the most suited type for the members? strings? (json) Values? Vecs? slices?
Paul Masurel
@fulmicoton
Apr 25 2018 12:19
Could you be more specific?
What is actual task? what does "working on" mean? do you have control over the file format? Is it large, binary, textual?
Dylan DPC
@Dylan-DPC
Apr 25 2018 12:26
so i am reading the contents of a file (you can assume a normal text file though a buffer and placing it in a string). Call that contents. Then say i'm doing Foo { contents }.
Now since i need to iterate over each line and this is a lib (which means that any other user might use Foo directly) i'm wondering what will be best data type for it?
Paul Masurel
@fulmicoton
Apr 25 2018 12:32
If it is textual, and your file is reasonably small, loading everything in a String is ok I think.
alternatively, especially if you do that on one pass, you don't need to load the file for that.
Dylan DPC
@Dylan-DPC
Apr 25 2018 12:39
ya it is in a string. but for further processing i'm json'g it and collecting it in a vector
i think i'll leave it as it is. Thanks
Andy Grove
@andygrove
Apr 25 2018 13:06
@vorner Thanks for the tip. I found the FnvHashMap crate and it shaved a little more time off :-)
Andrey Gridchin
@Gedweb
Apr 25 2018 14:03
Can i try Rust 2018 with new features, it's possible?
Ingvar Stepanyan
@RReverser
Apr 25 2018 15:06
@Gedweb Please clarify your question or what you mean by "Rust 2018"
Dylan DPC
@Dylan-DPC
Apr 25 2018 15:11
he is referring to the things that are not yet developed :P
Andrey Gridchin
@Gedweb
Apr 25 2018 18:21
I think we have some beta futures, like a async fn
Dylan DPC
@Dylan-DPC
Apr 25 2018 18:27
you can use some of the stuff by using the latest nightly
Dylan DPC
@Dylan-DPC
Apr 25 2018 19:02
i wish you could use comparison operators with match :thought_balloon:
Jason Williams
@jasonwilliams
Apr 25 2018 19:20
Hey all, if I was to use a http server for my project today, which one one would I pick? Is hyper stable?
Nikolay Kim
@fafhrd91
Apr 25 2018 19:22
@jasonwilliams depends on what you need, if you need http server then hyper, if you need web framework then try actix-web
Denis Lisov
@tanriol
Apr 25 2018 19:26
@Dylan-DPC Sure you know you can use if guards with match arms, don't you?
Jason Williams
@jasonwilliams
Apr 25 2018 19:29
@fafhrd91 I’m building an API
Nikolay Kim
@fafhrd91
Apr 25 2018 19:31
@jasonwilliams then try actix-web
Jason Williams
@jasonwilliams
Apr 25 2018 19:32
It supports http2 nice, what’s it using for http under the surface? I didn’t think hyper supported h2 yet?
Nikolay Kim
@fafhrd91
Apr 25 2018 19:36
it uses h2 crate for http/2
Sam A. Horvath-Hunt
@SamHH
Apr 25 2018 20:02
Is there a way to listen for characters in the terminal without having to wait for Enter key, and without having to use ncurses? I've tried Stdin, all wait for new lines (couldn't try chars as deprecated). StackOverflow suggested text_Io crate but same behaviour there; I get everything back from stdin but only after I hit Enter. (going offline now but will check responses, any help appreciated :smile: )
Andrey Gridchin
@Gedweb
Apr 25 2018 20:40
@SamHH only with external crate, like this https://github.com/jeaye/ncurses-rs
Sam A. Horvath-Hunt
@SamHH
Apr 25 2018 21:18
Okay, thanks @Gedweb
Katharina
@spacekookie
Apr 25 2018 22:53

I'm a little confused right now about a trait I want to make.

pub trait Storable: Serialize + DeserializeOwned {
    fn pre_load(path: &str, name: &str) -> Box<Storable> {
       // ...
        return Box::new(serde_json::from_str(loaded_string).unwrap());
    }
}

This tells me note: the trait cannot require that 'Self : Sized', method 'serialize' has generic type parameters ... What generic type for serialize? I would expect that just to be...the trait?

Katharina
@spacekookie
Apr 25 2018 23:04
Ehhh...nevermind. I should just use -> Self instead of the explicit type... :sweat_smile: