Pages: 1
  Print  
Author Topic: Why do we use placement new?  (Read 2363 times)
Offline (Unknown gender) sorlok_reaves
Posted on: October 07, 2014, 08:16:59 PM
Contributor
Joined: Dec 2013
Posts: 261

View Profile
Why do vars initialize with placement new in ENIGMA?
Logged
Offline (Male) Goombert
Reply #1 Posted on: October 07, 2014, 08:29:31 PM

Developer
Location: Cappuccino, CA
Joined: Jan 2013
Posts: 3110

View Profile
Can you show me a code example sorlok? I am a tad confused by what you mean.
Logged
I think it was Leonardo da Vinci who once said something along the lines of "If you build the robots, they will make games." or something to that effect.

Offline (Unknown gender) sorlok_reaves
Reply #2 Posted on: October 07, 2014, 08:35:21 PM
Contributor
Joined: Dec 2013
Posts: 261

View Profile
In var4_lua.cpp:

Code: [Select]
void var::initialize() {
  new(&values) vararray;
}
void var::cleanup() {
  as_lua(values) . ~vararray();
}
Logged
Offline (Male) Goombert
Reply #3 Posted on: October 07, 2014, 08:38:14 PM

Developer
Location: Cappuccino, CA
Joined: Jan 2013
Posts: 3110

View Profile
Oh wow, I am not sure you'll have to ask Josh about that one. I'll forward this to him.
Logged
I think it was Leonardo da Vinci who once said something along the lines of "If you build the robots, they will make games." or something to that effect.

Offline (Unknown gender) sorlok_reaves
Reply #4 Posted on: October 07, 2014, 08:41:23 PM
Contributor
Joined: Dec 2013
Posts: 261

View Profile
Yeah, I never really enjoy dealing with placement new. :P
Logged
Offline (Male) Josh @ Dreamland
Reply #5 Posted on: October 08, 2014, 06:27:15 PM

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2958

View Profile Email
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.
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Unknown gender) sorlok_reaves
Reply #6 Posted on: October 08, 2014, 06:29:40 PM
Contributor
Joined: Dec 2013
Posts: 261

View Profile
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.)
Logged
Offline (Male) Josh @ Dreamland
Reply #7 Posted on: October 11, 2014, 08:48:25 AM

Prince of all Goldfish
Developer
Location: Pittsburgh, PA, USA
Joined: Feb 2008
Posts: 2958

View Profile Email
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.
Logged
"That is the single most cryptic piece of code I have ever seen." -Master PobbleWobble
"I disapprove of what you say, but I will defend to the death your right to say it." -Evelyn Beatrice Hall, Friends of Voltaire
Offline (Unknown gender) sorlok_reaves
Reply #8 Posted on: October 14, 2014, 09:16:25 AM
Contributor
Joined: Dec 2013
Posts: 261

View Profile
Thanks for the description; I think I get it now.
Logged
Pages: 1
  Print