The Artima Developer Community
Sponsored Link

Articles Forum
Backyard Hotrodding C++

10 replies on 1 page. Most recent reply: Sep 15, 2006 6:35 PM by Walter Karas

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 10 replies on 1 page
Bill Venners

Posts: 7
Nickname: bop
Registered: Jan, 2002

Backyard Hotrodding C++ Posted: May 23, 2006 6:00 PM
Reply to this message Reply
Advertisement
Ever feel the need for speed in C++? Real speed? If you're careful, you can get it without making a mess of things.

Read this The C++ Source article:

http://www.artima.com/cppsource/backyard.html

What do you think of the author's ideas?


Bruno Martínez

Posts: 2
Nickname: br1
Registered: Nov, 2003

Re: Backyard Hotrodding C++ Posted: May 23, 2006 6:52 PM
Reply to this message Reply
Nice article. I guess, it's true that sometimes you have to leave safety. :)

I have two comments:

About Vptr Jamming, I think you can avoid the problem of finding the vptr by introducing an object with just a vptr at the top of the hierarchy:

struct Empty {
virtual ~Empty() {}
};

struct Collection : Empty
{
virtual void foo() = 0;

void toSingle();
void toMulti();
};

struct SingleThreadedCollection : Collection
{
static SingleThreadedCollection tmp;

void foo()
{
std::cout << "st\n";
}
};

struct MultiThreadedCollection : Collection
{
static MultiThreadedCollection tmp;
void foo()
{
std::cout << "mt\n";
}
};

SingleThreadedCollection SingleThreadedCollection::tmp;
MultiThreadedCollection MultiThreadedCollection::tmp;

void Collection::toSingle()
{
Empty* st = &SingleThreadedCollection::tmp;
Empty* me = this;
memcpy(me, st, sizeof Empty);
}



My second comment is about RTTI Sniping. I realize dynamic_cast is slower, but what about typeid?

Bruno

Steve Massey

Posts: 2
Nickname: sbmassey
Registered: May, 2006

Re: Backyard Hotrodding C++ Posted: May 23, 2006 11:32 PM
Reply to this message Reply
The Counterfit this solution seems a bit unnecessary. What is the advantage of using reinterpret_cast's rather than just inheriting Implementation from Foo?

Walter Bright

Posts: 13
Nickname: walterb
Registered: Jul, 2004

Re: Backyard Hotrodding C++ Posted: May 24, 2006 2:42 AM
Reply to this message Reply
> The Counterfit this solution seems a bit unnecessary.
> What is the advantage of using reinterpret_cast's rather
> r than just inheriting Implementation from Foo?

The problem is that C++ makes it easy to create derived classes, but not so easy to insert a base class - especially if you (for many reasons) cannot change the implementation class.

E. Nielsen

Posts: 9
Nickname: en
Registered: Feb, 2003

Re: Backyard Hotrodding C++ Posted: May 24, 2006 7:29 AM
Reply to this message Reply
Hmmm... This is really nice to know... ;-D

"...He worked for Boeing for 3 years on the development of the 757 stabilizer trim system..."

Adi Shavit

Posts: 6
Nickname: adish
Registered: Apr, 2005

Re: Backyard Hotrodding C++ Posted: May 25, 2006 3:06 AM
Reply to this message Reply
Interesting stuff!
I think that with some clever additional compile time constraints and assertions, some of these techniques might even be made usable in more "normal" contexts. no?

Achilleas Margaritis

Posts: 674
Nickname: achilleas
Registered: Feb, 2005

Re: Backyard Hotrodding C++ Posted: May 25, 2006 6:04 AM
Reply to this message Reply
Walter Bright is a bright fellow who wrote the D programming language. Although I like D, it is not widely adopted, and it seems to me it will never be. It seems the embedded/realtime world is satisfied with C++ while the business world is satisfied with Java.

Anyway here is another trick that one can do to hide the implementation details of a class. It requires a little bit more work, but it is more efficient than the one presented in the article.

Here is the interface of the class:


#ifndef FOO_HPP
#define FOO_HPP

class foo {
public:
//the default constructor
foo();

//the copy constructor
foo(const foo &f);

//the destructor
virtual ~foo();

//the assignment operator
foo &operator = (const foo &f);

//get content
int value() const;

//set content
void set_value(int v);

private:
unsigned char content[4];
};

#endif //FOO_HPP


Here is the implementation file foo.cpp:


#include "foo.hpp"
#include <stdexcept>
#include <iostream>

//macros for making the conversion easier
#define self ((_foo *)this)
#define rhs ((_foo *)&f)

//the implementation; it must have the same layout as the interface
struct _foo {
int value;
virtual ~_foo() {
}
};

//the default constructor
foo::foo() {
self->value = 0;
}

//the copy constructor
foo::foo(const foo &f) {
self->value = rhs->value;
}

//the destructor
foo::~foo() {
std::cout << self->value << std::endl;
}

//the assignment operator
foo &foo::operator = (const foo &f) {
self->value = rhs->value;
return *this;
}

//a method that returns data
int foo::value() const {
return self->value;
}

//a method that sets data
void foo::set_value(int v) {
self->value = v;
}

//a class that ensures size of interface == size of implementation
template <class A, class B> struct assert_size {
assert_size() {
if (sizeof(A) != sizeof(B)) throw std::logic_error("implementation size different from interface size");
}
};

//ensure size of interface == size of implementation
static assert_size<foo, _foo> assert_size_of_foo;


and here is usage of the class:


#include "foo.hpp"
#include <iostream>
using namespace std;

void test() {
foo f;
f.set_value(10);
cout << f.value() << endl;
f.set_value(20);
}

int main() {
test();
getchar();
return 0;
}


The result is:


10
20

Ion Gaztañaga

Posts: 8
Nickname: igaztanaga
Registered: Jan, 2006

Re: Backyard Hotrodding C++ Posted: May 25, 2006 8:16 AM
Reply to this message Reply
> class foo {
> //...
> private:
> unsigned char content[4];
> };
>
> #endif //FOO_HPP

This implementation hiding won't work for any type since you must align internal memory to meet the requirements of the implementation. if "content" is nota aligned to 4 bytes in some 32 systems this will crash.

Another option is to use something similar to boost::aligned_storage with the required alignment or use the most restringet alignment.

Achilleas Margaritis

Posts: 674
Nickname: achilleas
Registered: Feb, 2005

Re: Backyard Hotrodding C++ Posted: May 25, 2006 10:38 AM
Reply to this message Reply
> This implementation hiding won't work for any type since
> you must align internal memory to meet the requirements of
> the implementation.

As long as the implementation has the same size and vtable with the interface, there is no problem.

The class 'assert_size' ensures the interface and implementation have the same size.

Gregor Zeitlinger

Posts: 108
Nickname: gregor
Registered: Aug, 2005

Re: Backyard Hotrodding C++ Posted: May 28, 2006 12:14 PM
Reply to this message Reply
Pointer dehydration/hydration is much faster than traditional serialization - but you still have to dehydrate/hydrate all pointers - even if you didn't need them in one session. Therefore it could be called "lightweight serialization".

Even faster (at least in some scenarios) is "on-demand hydration/no dehydration".
I used this technique to implement an XML database.
Loading persisent data (a database page) involves no hydration at all. Whenever you want an item from the database page, you get a pointer (start of page + offset) that is created on demand. All data that is written to those items is relative to the start of page. Therefore you don't need any dehydration at all.

This technique is more complicated to implement, of course - and is only justified when performance has to pushed to its limits.

Walter Karas

Posts: 12
Nickname: wkaras
Registered: Dec, 2003

Re: Backyard Hotrodding C++ Posted: Sep 15, 2006 6:35 PM
Reply to this message Reply
Another approach to easy container persistence is to make the container templates generic enough so that array indexes can be used for links instead of pointers. For example:

http://www.geocities.com/wkaras/gen_cpp/avl_tree.html

Flat View: This topic has 10 replies on 1 page
Topic: Human-Oriented Architecture Previous Topic   Next Topic Topic: The NetBeans Platform


Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2014 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us