473,396 Members | 1,755 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Lazy container

Hi everyone,

I'm trying to write a Container which should mimic a list. Basically,
the container pulls items on the fly from an unspecified source through
a function and returns an instance of a given class over the pulled item.

That is:

class lazy(object):
def __getitem__(self,index):
return Foo(f(index))

lc = lazy()

Here I see a problem: two consecutive accesses to lc for the same index
would retrieve two different instances of Foo.

In some scenarios this is not desirable since one wants to have all the
accessors to share the same instance for the same index so as to reduce
memory consumption.

So, I thought to use an internal dictionary of all the Foo instances
given away so far. Something like:

class lazy(object):
def __init__(self):
self.cache = {}

def __getitem__(self,index):
if not self.cache.has_key(index):
value = Foo(f(index)) # MISS
self.cache[index] = value
else: # HIT
value = self.cache[index]

return value

This is acceptable as it reduces the number of instances living in the
system at any given time.

The problem with this implementation is that the cache never decreases
in length as Foo instances are no longer referenced by the lc accessor
since they're all referenced by the internal cache.

There are scenarios in which f() retrieves items from a very huge source
(i.e. a database) and threads sharing the lazing container and consuming
Foo instances to use them for a very short time and then discardi them:
after a while you'll eventually end up having a whole copy of the source
in memory, which is unacceptable.

One solution may be to use gc.get_referrers() to search the cache for
unused instances every time we have a cache miss so as to see if we
could discard some instance before inserting the new one in the cache.

But this comes at a cost.

May be someone has faced this problem before and came up with some
obscure language feature to solve this elegantly :D

Thanks for your replies.

Cristiano
Feb 13 '07 #1
3 1499
Cristiano Paris <fr***@theshire.orgwrote:
May be someone has faced this problem before and came up with some
obscure language feature to solve this elegantly :D
Yes. Try using weak references. Specifically a weakref.WeakValueDictionary
should meet your needs.
Feb 13 '07 #2
Cristiano Paris wrote:
I'm trying to write a Container which should mimic a list. Basically,
the container pulls items on the fly from an unspecified source through
a function and returns an instance of a given class over the pulled item.

That is:

class lazy(object):
def __getitem__(self,index):
return Foo(f(index))

lc = lazy()

Here I see a problem: two consecutive accesses to lc for the same index
would retrieve two different instances of Foo.

In some scenarios this is not desirable since one wants to have all the
accessors to share the same instance for the same index so as to reduce
memory consumption.

So, I thought to use an internal dictionary of all the Foo instances
given away so far. Something like:
The problem with this implementation is that the cache never decreases
in length as Foo instances are no longer referenced by the lc accessor
since they're all referenced by the internal cache.

You want a WeakValueDictionary:
http://docs.python.org/lib/module-weakref.html

Peter
Feb 13 '07 #3
Duncan Booth wrote:
...
Yes. Try using weak references. Specifically a weakref.WeakValueDictionary
should meet your needs.
:D

Thank you.

Cristiano
Feb 13 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Vinay Aggarwal | last post by:
I have been thinking about the lazy initialization and double checked locking problem. This problem is explain in detail here http://www.cs.umd.edu/~pugh/java/memoryModel/DoubleCheckedLocking.html...
25
by: Steven Bethard | last post by:
So I end up writing code like this a fair bit: map = {} for key, value in sequence: map.setdefault(key, ).append(value) This code basically constructs a one-to-many mapping -- each value that...
3
by: Jeff Johnson [MVP: VB] | last post by:
What is the point of lazy * and lazy ? ? "Nothing" will always succeed first, right? If not, can someone give me an example of when either of these might be used?
4
by: Siemel Naran | last post by:
What is a good idiom for handling a lazy object? I see 2 good possibilities. Any more, any comments? Which way do people here use? (1) class Thing { public: Thing(double x, double y) :...
2
by: Ole V.-M. | last post by:
Greetings, i have a UserControl, that contains a DataList. That DataList contains as items other DataLists. example: DataList A Row 1 Nested DataList 1 Row 1
9
by: sturlamolden | last post by:
Python allows the binding behaviour to be defined for descriptors, using the __set__ and __get__ methods. I think it would be a major advantage if this could be generalized to any object, by...
39
by: Boltar | last post by:
Why does C do lazy evaluation for logical boolean operations but not bitwise ones? Ie: the following program prints "1 2" , not "1 1" under gcc main() { int a = 1; int b = 1; 0 && ++a;
2
by: fredd00 | last post by:
Hi, i'm trying to use lazy loading with Linq to sql and related objects seems like you can only call the child object if the context is still open, this is not real lazy loading. here is my...
6
by: Peng Yu | last post by:
Hi, I'm wondering if the following assignment is lazy copy or not? Thanks, Peng std::vector<intv. v.push_back(1); v.push_back(2);
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.