compute huge datastructure once on dll-load
-
hi, i'm writing a dll and there's a huge but constant data structure that is used from time to time inside the library. when i put it into an ordinary class, this data-structure (it's a tree) has to be rebuilt everytime an object of the class is constructed. there's no main-function or app-object to keep the tree reference throughout the use of the dll. reference lost, garbage-collector feeded ;) what's the most effective way to solve this? :wq
-
hi, i'm writing a dll and there's a huge but constant data structure that is used from time to time inside the library. when i put it into an ordinary class, this data-structure (it's a tree) has to be rebuilt everytime an object of the class is constructed. there's no main-function or app-object to keep the tree reference throughout the use of the dll. reference lost, garbage-collector feeded ;) what's the most effective way to solve this? :wq
Hi Have tried putting it in a different class and exposed it a static member? You dont so whether u make changes to it, or it just serves as a "lookup table"... Maybe it helps ;) MYrc : A .NET IRC client with C# Plugin Capabilities. See http://sourceforge.net/projects/myrc for more info. :-D
-
Hi Have tried putting it in a different class and exposed it a static member? You dont so whether u make changes to it, or it just serves as a "lookup table"... Maybe it helps ;) MYrc : A .NET IRC client with C# Plugin Capabilities. See http://sourceforge.net/projects/myrc for more info. :-D
yes it is kind of a lookup-table (a lookup-tree if you like) but when i have that thing a static member of a class, it still has to be created once. when should i do that? in the constructor? but then the procedure takes place each time an object of this class is created via new. or do you think an additional static bool set to true after the first creation may help? lets say
Class A { static Tree lookupTree; static bool created=false; A() { if (!created) { ExpensiveTreeCreationFunction(); created = true; } } ... }
and then i can useA control = new A(); A.foo(); //using lookupTree
as often as i like without ExpensiveTreeCreationFunction(); called each time? :omg: :wq -
yes it is kind of a lookup-table (a lookup-tree if you like) but when i have that thing a static member of a class, it still has to be created once. when should i do that? in the constructor? but then the procedure takes place each time an object of this class is created via new. or do you think an additional static bool set to true after the first creation may help? lets say
Class A { static Tree lookupTree; static bool created=false; A() { if (!created) { ExpensiveTreeCreationFunction(); created = true; } } ... }
and then i can useA control = new A(); A.foo(); //using lookupTree
as often as i like without ExpensiveTreeCreationFunction(); called each time? :omg: :wqyes ! almost, but it would be better to check when calling foo() instead of a static constructor (some things never makes sense :) ) or just add a static method to create it...there are many choices :) Do whats best for you. MYrc : A .NET IRC client with C# Plugin Capabilities. See http://sourceforge.net/projects/myrc for more info. :-D
-
yes ! almost, but it would be better to check when calling foo() instead of a static constructor (some things never makes sense :) ) or just add a static method to create it...there are many choices :) Do whats best for you. MYrc : A .NET IRC client with C# Plugin Capabilities. See http://sourceforge.net/projects/myrc for more info. :-D
checking in foo() is ok. i just tried and it seems to work. to get things clear: when a class consists of a static member, this member is stored somewhere else than the objects of that class (which are destroyed by the GC when references go to zero) and are freed/destroyed when? at the end of the program-run? i just want to understand that...:) :wq
-
hi, i'm writing a dll and there's a huge but constant data structure that is used from time to time inside the library. when i put it into an ordinary class, this data-structure (it's a tree) has to be rebuilt everytime an object of the class is constructed. there's no main-function or app-object to keep the tree reference throughout the use of the dll. reference lost, garbage-collector feeded ;) what's the most effective way to solve this? :wq
The best solution to this is to use a static member and static constructor. Create some class which has the static member of the type you are referring to. Then put hte build datastructure code in the static COnstructor. This way the table will only be built ont time and you odn't have to worry about when to build it. The static constructor will be called before anything in the class is accessed.
-
The best solution to this is to use a static member and static constructor. Create some class which has the static member of the type you are referring to. Then put hte build datastructure code in the static COnstructor. This way the table will only be built ont time and you odn't have to worry about when to build it. The static constructor will be called before anything in the class is accessed.
Thanx , didnt know that about the static constructor :) MYrc : A .NET IRC client with C# Plugin Capabilities. See http://sourceforge.net/projects/myrc for more info. :-D