From: Ryan Pavlik <rpav@users.sourceforge.net>
To: ruby-core@ruby-lang.org
Subject: Re: _id2ref bug? (REPRODUCED, short)
Date: Fri, 15 Aug 2003 03:09:19 +0900 [thread overview]
Message-ID: <20030814110915.31f8f04d.rpav@users.sf.net> (raw)
In-Reply-To: <1060880235.666474.27392.nullmailer@picachu.netlab.jp>
[-- Attachment #1: Type: text/plain, Size: 315 bytes --]
On Fri, 15 Aug 2003 01:57:18 +0900
matz@ruby-lang.org (Yukihiro Matsumoto) wrote:
<snip>
> We can't say much without error reproducing code.
<snip>
I was wrong, here's a short example that reproduces the problem exactly.
--
Ryan Pavlik <rpav@users.sf.net>
"I distinctly remember dancing on your grave." - 8BT
[-- Attachment #2: weakref.rb --]
[-- Type: application/octet-stream, Size: 1374 bytes --]
class Object
@@oid = 0
def _oid
unless defined?(@_oid)
@_oid = @@oid + 1
@@oid += 1
end
@_oid
end
end
class Cache
def initialize(max = 5, reduce = 3)
@s_cache = {}
@w_cache = {}
@cachehits = {}
@max = 5
@reduce = 3
end #m:initialize
def cache(o)
oid = o._oid
return if(@s_cache.has_key? oid)
if(@s_cache.size + 1 > @max)
cache_free(@reduce)
end
@cachehits[oid] = 1
@s_cache[oid] = o
@w_cache[oid] = o.__id__
return oid
end #m:cache
def [](oid)
if(o = @s_cache[oid])
@cachehits[oid] += 1
return o
elsif(id = @w_cache[oid])
begin
o = ObjectSpace._id2ref(id)
cache(o)
return o
rescue RangeError
return nil
end
end
return nil
end #m:[]
def cache_free(size)
print "Freeing #{size}...\n"
list = @cachehits.sort { |a, b| (a[1] <=> b[1]) }
size.times {
| i |
break if(i >= list.size)
oid = list[i][0]
store @s_cache[oid]
@s_cache.delete oid
@cachehits.delete oid
# oid remains in the weak cache
}
end #m:cache_free
def store(object)
# Does nothing in this example
end #m:store
end #c:Cache
c = Cache.new
(1...100).each {
c.cache(Object.new)
(1..100).each { |i| p c[i]; }
}
next prev parent reply other threads:[~2003-08-14 18:03 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2003-08-14 16:45 _id2ref bug? Ryan Pavlik
2003-08-14 16:57 ` Yukihiro Matsumoto
2003-08-14 17:01 ` ts
2003-08-14 17:08 ` Yukihiro Matsumoto
2003-08-14 17:44 ` Ryan Pavlik
2003-08-14 17:46 ` Ryan Pavlik
2003-08-15 0:10 ` Yukihiro Matsumoto
2003-08-14 18:09 ` Ryan Pavlik [this message]
2003-08-15 0:21 ` _id2ref bug? (REPRODUCED, short) Yukihiro Matsumoto
2003-08-15 2:37 ` _id2ref bug? (another break) Ryan Pavlik
2003-08-15 3:35 ` nobu.nokada
2003-08-15 4:19 ` Ryan Pavlik
2003-08-15 4:48 ` Ruby Tk on Mac OS X Mark Wilson
2003-08-15 5:22 ` nobu.nokada
2003-08-15 6:10 ` Mark Wilson
2003-08-15 7:14 ` nobu.nokada
2003-08-15 10:17 ` _id2ref bug? (another break) ts
2003-08-15 17:42 ` Ryan Pavlik
2003-08-16 9:35 ` ts
2003-08-16 11:02 ` nobu.nokada
2003-08-16 11:08 ` ts
2003-08-16 12:54 ` nobu.nokada
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.ruby-lang.org/en/community/mailing-lists/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20030814110915.31f8f04d.rpav@users.sf.net \
--to=ruby-core@ruby-lang.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).