ENIGMA Forums

Outsourcing saves money => Programming Help => Topic started by: sorlok_reaves on October 07, 2014, 08:16:59 PM

Title: Why do we use placement new?
Post by: sorlok_reaves on October 07, 2014, 08:16:59 PM
Why do vars initialize with placement new in ENIGMA?
Title: Re: Why do we use placement new?
Post by: Goombert on October 07, 2014, 08:29:31 PM
Can you show me a code example sorlok? I am a tad confused by what you mean.
Title: Re: Why do we use placement new?
Post by: sorlok_reaves on October 07, 2014, 08:35:21 PM
In var4_lua.cpp:

Code: [Select]
void var::initialize() {
  new(&values) vararray;
}
void var::cleanup() {
  as_lua(values) . ~vararray();
}
Title: Re: Why do we use placement new?
Post by: Goombert on October 07, 2014, 08:38:14 PM
Oh wow, I am not sure you'll have to ask Josh about that one. I'll forward this to him.
Title: Re: Why do we use placement new?
Post by: sorlok_reaves on October 07, 2014, 08:41:23 PM
Yeah, I never really enjoy dealing with placement new. :P
Title: Re: Why do we use placement new?
Post by: Josh @ Dreamland on October 08, 2014, 06:27:15 PM
When I wrote var, I had the sense to want to keep the matrix kernel modular, but lacked the sense to do it right. It uses placement new so that the main var class doesn't have to know anything about the type it's storing, which is itself just a pointer. You could easily refactor it so that the lua_table just works with the values data pointer directly, instead of pretending to own it. At this point, I don't even really care if you want to just move the lua_table code into var. If you want to do that refactoring, feel freeā€”it might even speed up var arithmetic because the compiler will be more confident in its optimizations.
Title: Re: Why do we use placement new?
Post by: sorlok_reaves on October 08, 2014, 06:29:40 PM
I'll give it a shot. I'm assuming the lua_table<lua_table<variant>> construct is used for 2-D arrays. Does that actually work right internally? (I have received mixed information on whether or not arrays "work" in ENIGMA.)
Title: Re: Why do we use placement new?
Post by: Josh @ Dreamland on October 11, 2014, 08:48:25 AM
The arrays problem in ENIGMA is that variables of type var, when accessed using [], must be encoded using (), because otherwise 2-D access using commas is not supported. Basically, my_var[1,2] goes in, my_var(1,2) comes out. ENIGMA's current type coercion is woefully inadequate, and as a consequence, this ends up happening for all arrays. So only var arrays work. A quick band-aid on this is to only make that replacement when a comma is detected outside of parentheses inside the brackets.

The lua_table implementation is fine. All it does is keep a dense portion and a sparse portion of an array of whatever object type you give it. It's pretty inefficient for one-dimensional access... I should do something about it.
Title: Re: Why do we use placement new?
Post by: sorlok_reaves on October 14, 2014, 09:16:25 AM
Thanks for the description; I think I get it now.