LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-09-2006, 07:13 AM   #1
George2
Member
 
Registered: Oct 2003
Posts: 354

Rep: Reputation: 30
any comments about a cache design?


Hello everyone,


I am going to design a Cache in C/C++ (memory cache). I have two different approaches to design/implement it, any comments will be appreciated.

Design 1, using a map (hashtable) to store key --> object pointer relationship (simple and straight forward);

Design 2, store real object or object pointer in a stack/linked list/queue/array as an internal storage data structure, then using a map (hashtable) as an external interface to mapping key --> index in the stack/linked list/queue/array; (in this design, when accessing an object, we need two times mapping, first mapping key to index in the stack/linked list/queue/array, then using the index to access the real object)

I am wondering any advantages or disadvantages of the two designs. Any better design approach suggestion will be appreciated.


thanks in advance,
George
 
Old 09-09-2006, 09:22 AM   #2
kstan
Member
 
Registered: Sep 2004
Location: Malaysia, Johor
Distribution: Dual boot MacOS X/Ubuntu 9.10
Posts: 851

Rep: Reputation: 31
I really don't recommend 2nd method, because it like nightmare for me to learn during I study abstract data type paper at university. However it give you full control on all objects in memory, but you need to pay with much more programming afford.
I don't think at current year we need to go for it.
Ks
 
Old 09-09-2006, 12:32 PM   #3
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
The main difference I see is in destruction control. The first allows destruction in-scope (or dynamically), however something external would have to notify the table when something is deleted. The second forces its own allocation, making the cache responsible for destruction. The first might require modified destructors or containers which deregister the objects, but the second would require an external function to remove an object when it's no longer needed. Is this a dynamic or fixed cache, i.e. will the program start and end with the same number of objects?
ta0kira
 
Old 09-14-2006, 07:37 AM   #4
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
Thank you kstan!


Quote:
Originally Posted by kstan
I really don't recommend 2nd method, because it like nightmare for me to learn during I study abstract data type paper at university. However it give you full control on all objects in memory, but you need to pay with much more programming afford.
I don't think at current year we need to go for it.
Ks
Could you give more information about what in your mind is the advantages of the 2nd design, when compared with the 1st design?


regards,
George
 
Old 09-14-2006, 07:39 AM   #5
George2
Member
 
Registered: Oct 2003
Posts: 354

Original Poster
Rep: Reputation: 30
Thank you ta0kira!


Quote:
Originally Posted by ta0kira
The main difference I see is in destruction control. The first allows destruction in-scope (or dynamically), however something external would have to notify the table when something is deleted. The second forces its own allocation, making the cache responsible for destruction. The first might require modified destructors or containers which deregister the objects, but the second would require an external function to remove an object when it's no longer needed. Is this a dynamic or fixed cache, i.e. will the program start and end with the same number of objects?
ta0kira
It is a fixed sized cache for some customer structure. You talked something about the destruction differences of the two designs. Any comments about the advantages of each design?


regards,
George
 
Old 09-14-2006, 08:00 AM   #6
kstan
Member
 
Registered: Sep 2004
Location: Malaysia, Johor
Distribution: Dual boot MacOS X/Ubuntu 9.10
Posts: 851

Rep: Reputation: 31
this is an example, and only 1 of the applied method
1. Imagine you have a groups of object which have same field, maybe a student in a school.
2. Consider student have 2 hand, left hand grep previous student hand, right hand grep next person hand.
3. then hand consider as pointer or cursur.
4. If many student in a school grep their hand together, then we can have a long linked student (consider as link list)
5. If you ask the 50th student left hand grep 2nd student right hand, 3rd student left hand grep last studentleft hand, then you will see the entire structure change.
6. pointer is more suitable if compare with the array, because it is dynamic size and you can extent it with 2 handed student, 3 handed student, 4 handed student (just imagine and don't go to find this kind of student)
7. queue is same but the first student left hand grep the last student right hand
8. stack is similar but using fifo mechanism

Now you know the ideal, it good for manipulate data, but normally we manage the data with database because easier and much more function. However, if you want to have a method to handle large amount of data in lighting speed, then go for it because it keep all data into memory.

So, in linked list you can simply instruct student to change their hand position, during programming maybe you just tell function where is the 1st student.

Finally, this is only my opinion and maybe it not 100% correct.

Last edited by kstan; 09-14-2006 at 08:05 AM.
 
Old 09-14-2006, 11:30 AM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Quote:
Originally Posted by George2
It is a fixed sized cache for some customer structure. You talked something about the destruction differences of the two designs. Any comments about the advantages of each design?
If you just store a table of pointers to objects, something else will have to be responsible for them, which means they'll be deleted when the responsible scope ends. If they are created by the cache itself (or if you pass 'new' objects and make the cache responsible) then they outlast all scopes until the cache destructs. If your cache is fixed size with all the same elements (no polymorphism), I'd recommend just a fixed array. If they will require polymorphism at all or any dynamic sizing or arangement, I'd recommend a linked list where the user would register 'new' objects, making the cache ultimately responsible for their deletion. When the cache destructs it deletes any objects it currently contains.
ta0kira

PS Probably with an additional hash table, as you suggested.

Last edited by ta0kira; 09-14-2006 at 11:32 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Linux for Graphic Design, web design, and publishing maelstrom209 Linux - Software 8 07-17-2011 11:35 AM
cache folder like /var/cache/apt/packages on Debian Shaddy SUSE / openSUSE 0 08-13-2006 10:02 AM
Ram wiht suse, cache Disk cache??? fadelhomsi Linux - Newbie 2 02-05-2006 11:29 PM
clearing cache, web cache on linux varunbihani Linux - General 2 12-08-2005 12:02 PM
Error: Caching enabled and local cache: //var/cache/yum/base/primary.xml.gz does... dr_zayus69 Linux - Software 2 07-06-2005 04:32 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration