Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!border3.nntp.ams.giganews.com!Xl.tags.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!local2.nntp.ams.giganews.com!news.giganews.com.POSTED!not-for-mail NNTP-Posting-Date: Mon, 22 Jul 2013 15:12:52 -0500 From: Chris Hinsley Newsgroups: comp.lang.python Date: Mon, 22 Jul 2013 21:12:52 +0100 Message-ID: <2013072221125291649-chrishinsley@gmailcom> References: <2013072219364160391-chrishinsley@gmailcom> <2013072219424735368-chrishinsley@gmailcom> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: odd behavoiur seen User-Agent: Unison/2.1.10 Lines: 50 X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-Vsir/1yfm9THQc+DHLAl4vaxrhQ1ppOyxD2ggJaAs45nB7ETefqCmcrY4KxMudqT8kM+MhEaUQgA4Qb!+VFNu0fecVuXrf4+GjpxqtbEC1hAzmcJsquk2Koay4FUKFr8u9T+9JGdIJBP+iFWmlA= X-Complaints-To: abuse@giganews.com X-DMCA-Notifications: http://www.giganews.com/info/dmca.html X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 2476 Xref: csiph.com comp.lang.python:51058 On 2013-07-22 19:47:33 +0000, Peter Otten said: > Chris Hinsley wrote: > >> On 2013-07-22 18:36:41 +0000, Chris Hinsley said: >> >>> Folks, I have this decorator: >>> >>> def memoize(maxsize): >>> def _memoize(func): >>> lru_cache = {} >>> lru_list = [] >> >> Other clues, I use it on a recursive function: >> >> @memoize(64) >> def next_move(board, colour, alpha, beta, ply): >> if ply <= 0: >> return evaluate(board) * colour >> for new_board in all_moves(board[:], colour): >> score = -next_move(new_board, -colour, -beta, -alpha, ply - 1) >> if score >= beta: >> return score >> if score > alpha: >> alpha = score >> return alpha >> >> And I notice I don't get the strange problem on a non-recursive >> function ! Or at least I don't seam to. > > That's indeed the problem: > >> if len(lru_list) >= maxsize: >> del(lru_cache[lru_list[0]]) >> del(lru_list[0]) >> ret = func(*args, **kwargs) >> lru_cache[key] = ret >> lru_list.append(key) > > You delete a cached item, then call the original function which causes calls > of the decorated function. This causes a length check which sees the already > reduced length and decides that the cache is not yet full. > > If you remove the oldest item after calling the original function you should > be OK. Ah ! Thank you kindly sir ! Chris