Re: Reducing unncessary string copying



* Yasushi SHOJI <yashi atmark-techno com> schrieb:

<snip>

> it sounds, at least to me, more like a convention than new APIs
> or a new class.

Yes, that would also be an idea. but in this case (when dynamic
data structures are filled out with those strings), we'll need
to track whether the string was copied anyways. But, of course,
that bit could be somewhere inside that data structure instead
of in each pointer/reference.

Oh, BTW, even when using my GCSTR approach, these dynamic structures
dont need to use it internally - they could easily convert it.

    struct {
	const char* title;
	const char* description;
	const char* blubbtext;
	int flags;
	...
    } MyFoo;

    #define MYFOO_STATIC_TITLE		1
    #define MYFOO_STATIC_DESCRIPTION	2
    #define MYFOO_STATIC_BLUBB		4

    MyFoo* foo_create(GCSTR title, GCSTR description, GCSTR blubb, int a)
    {
	    MyFoo* foo = (MyFoo*)malloc(sizeof(MyFoo));
	    foo->flags = 0;
	    if (GCSTR_IS_STATIC(title))
	    {
		foo->title = title.str;
		foo->flags |= MYFOO_STATIC_TITLE;
	    }
	    else
	    {
		foo->title = strdup(title.str);
	    }
	    ...
    }

    void foo_delete(MyFoo* foo)
    {
	    if (foo == NULL)
		return;
	    if (!(foo->flags && MYFOO_STATIC_TITLE))
		free(foo->title);
	    if (!(foo->flags && MYFOO_STATIC_DESCRIPTION))
		free(foo->description);
	    ...
    }

> how many bytes or how many CPU clocks are we waisting in
> real world application?

Well, I didn't have a chance to do real measurements yet.
But my observation is that glib/gtk-based applications, even
small ones, are eating up much heap memory.

Anyways, I suspect the double size of ints and pointers seems
to be a larger problem than strings (I've noticed a huge increase
when switching vom 32bit gentoo to 64bit ubuntu - my system is
contigously swapping, sometimes even hanging for several seconds).

> did you have a copy-on-write string class, like std::string in C++, in
> your mind?  having a copy-on-write string class in glib might be a
> good idea.

Well, could be the next step. But I dont have a good solution
w/o (compiler-driven) destructors for that yet.


cu
--
----------------------------------------------------------------------
 Enrico Weigelt, metux IT service -- http://www.metux.de/

 phone:  +49 36207 519931  email: weigelt metux de
 mobile: +49 151 27565287  icq:   210169427         skype: nekrad666
----------------------------------------------------------------------
 Embedded-Linux / Portierung / Opensource-QM / Verteilte Systeme
----------------------------------------------------------------------


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]