These are chat archives for rust-lang/rust

23rd
May 2017
Joonas Koivunen
@koivunej
May 23 2017 09:58

my struct has a need for a "scratch" memory area or a Vec<MyType<'_>>; during each fn operation<'a>(&mut self) { ... } this temporary Vec<MyType<'a>> is filled with produced values and a method on an another struct fn listener<'a>(&mut self, values: Vec<MyType<'a>>) is invoked in the end. as the MyType<'_> is quite large, the allocation shows up in profiling. is there any way to have the struct own the Vec<MyType<'_>> to be used as a temporary inside fn operation<'a>(&mut self)?

the struct might hold a Vec<MyType<'static>> but that would require transmuting the lifetimes away from the values living only inside the fn operation<'a>(...) . I wonder if anyone has created a safe abstraction for this already? Something like a ClearOnDropGuard<&'b Vec<_>>?

Zakarum
@omni-viral
May 23 2017 12:38
Have you seen arena crate?
Joonas Koivunen
@koivunej
May 23 2017 12:44
@SCareAngel hmm I didn't think about it... fn operation call takes a ~10 milliseconds, so reusing the space would be nice instead of what happends now (I create and drop a new Vec::with_capacity(n) for each call). wouldn't creating an TypedArena work the same way, except that the data already allocated in the TypedArena would be really wrong since there'd be no guarantee on the internal references of MyType<'a>
i think the easiest might be to store an Option<Vec<MyType<'static>> in the struct, in fn operation take it out, transmute it as Vec<MyType<'a>> ('a being the function lifetime), wrap it in a guard that clears, transmutes it back Vec<MyType<'static>> and returns it on drop
Joonas Koivunen
@koivunej
May 23 2017 12:52
probably a Rc<RefCell<Option<_>>> actually as otherwise the &mut self would be locked
Fra ns
@snarf95_twitter
May 23 2017 12:55
dat readability tho
Denis Lisov
@tanriol
May 23 2017 12:57
Readability? There's none, it seems :-)
Fra ns
@snarf95_twitter
May 23 2017 12:59
it's not a problem as long as it works tho
Zakarum
@omni-viral
May 23 2017 13:04
@koivunej RefCell<Option<_>> would be enough. Without Rc<_>
And how you can guarantee that your data with 'a lifetime wouldn’t be accessed in next function call?
The only way I can think is calling Vec::clear
Zakarum
@omni-viral
May 23 2017 13:14
@koivunej look at this crate also https://crates.io/crates/recycler
Joonas Koivunen
@koivunej
May 23 2017 13:15
@SCareAngel by creating a guard that clears before returning the vec
@SCareAngel this is the same in code, was surprisingly easy to write: https://is.gd/cUfUZM
I don't think I've ran into recycler yet... also cannot immediatedly understand if it could help me. need to look a bit more
Michal 'vorner' Vaner
@vorner
May 23 2017 13:20
That crate looks useful. But the last release is a year ago… I often wonder if such crates are still alive or maintained or what'll happen to them.
Joonas Koivunen
@koivunej
May 23 2017 13:24
looking at it sources there's not much to change :) perhaps the commented out parts can now be enabled as stuff has been stabilized
my case would need a solution to the issue frankmcsherry/recycler#3 ... and I cannot immediatedly how that could fit in with the trait.
Zakarum
@omni-viral
May 23 2017 13:38
@koivunej I prefer more manual control https://is.gd/aaZsZs
Joonas Koivunen
@koivunej
May 23 2017 13:40
hmm interesting, that one fails the assertion on size_of
Zakarum
@omni-viral
May 23 2017 13:42
&’static u8 instead of &’static [u8] in buffer
Joonas Koivunen
@koivunej
May 23 2017 13:43
oh yes so it'd seem
nicely done with the removal of Rc, RefCell and Option as well, though I don't think you can do the automatic replacement in drop without those. not sure if that (drop functionality) matters much though
Zakarum
@omni-viral
May 23 2017 13:47
Yeap. To replace automatically in Drop::drop you have to pass storage location to the guard
But it does not require RefCell or Rc if you don’t need to borrow whole self in your function
If you do need to borrow self - Rc<RefCell<…>> is your friend
Also you can look for on-stack allocation instead of keeping buffer
Joonas Koivunen
@koivunej
May 23 2017 13:59
@SCareAngel if you mean something like SmallVec there's the small issue that my struct is around 500 bytes and there can be quite many of those :)
except now that you mention it... this struct runs in it's own thread so there is not much else in the stack
Denis Lisov
@tanriol
May 23 2017 14:26
@snarf95_twitter Every code will need to be modified. Then it will matter.
@snarf95_twitter And I suspect that your code's performance can be rather low :-(
Fra ns
@snarf95_twitter
May 23 2017 14:41
@tanriol pro coders, write once
Denis Lisov
@tanriol
May 23 2017 14:48
Until they need to refactor their code or debug it :-)
Denis Lisov
@tanriol
May 23 2017 14:59
"Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?"
Michal 'vorner' Vaner
@vorner
May 23 2017 15:05
@tanriol I've been there… you actually have two potions. Either you throw the code away and give up or you become more clever. In that sense, there are times where you might want to write the code as clever as you can get and then be forced to debug it. It is quite effective learning exercise. But it's probably not a very smart thing to do when you want to use that code or push it to production or something like that.
Denis Lisov
@tanriol
May 23 2017 15:06

you actually have two potions.

Sounds like magic :-)

And yes, that's an interesting exercise, but often not a good habit :-(
Michal 'vorner' Vaner
@vorner
May 23 2017 15:08
Crap, stupid typo O:-). But, you know, programming is often magic.
Agreed on that habit thing. It shouldn't be the default mode of work.
Denis Lisov
@tanriol
May 23 2017 19:27
@snarf95_twitter How about a challenge? Try implementing the same rectangles count in such a way that your code works for any input sized up to, say 200x200 in less than a second. The worst case for the code you've demonstrated above would be about an hour... can you improve it to under one second?
Denis Lisov
@tanriol
May 23 2017 19:37
In case you're interested, I've been using the following test.
Fra ns
@snarf95_twitter
May 23 2017 20:10
@tanriol I'm always up for a challenge ;)