Skip to content

Commit

Permalink
Move Cell to cells.py and cache QuasiConstants
Browse files Browse the repository at this point in the history
  • Loading branch information
fniephaus committed Mar 13, 2017
1 parent 028d123 commit 971903d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
4 changes: 2 additions & 2 deletions rsqueakvm/objspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ def __init__(self):
self.altf4quit = QuasiConstant(False)

from rsqueakvm.display import NullDisplay
self._display = QuasiConstant(None, type=NullDisplay)
self._display = QuasiConstant(None, cls=NullDisplay)
from rsqueakvm.interpreter import Interpreter
self.interp = QuasiConstant(None, type=Interpreter)
self.interp = QuasiConstant(None, cls=Interpreter)

self.make_special_objects()
self.strategy_factory = storage.StrategyFactory(self)
Expand Down
9 changes: 1 addition & 8 deletions rsqueakvm/plugins/misc_primitive_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,12 @@
from rsqueakvm.error import PrimitiveFailedError
from rsqueakvm.model.variable import W_BytesObject
from rsqueakvm.plugins.plugin import Plugin
from rsqueakvm.util.cells import Cell

from rpython.rlib.rarithmetic import r_uint, intmask
from rpython.rlib import jit


class Cell(object):
_attrs_ = ["value"]
_immutable_fields_ = ["value?"]
def __init__(self, value): self.value = value
def set(self, v): self.value = v
def get(self): return self.value


class MiscPrimitivePlugin(Plugin):
_attrs_ = ["ascii_order"]
_immutable_fields_ = ["ascii_order"]
Expand Down
8 changes: 1 addition & 7 deletions rsqueakvm/plugins/socket_plugin.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import errno
import sys

from rsqueakvm import error
from rsqueakvm.model.base import W_AbstractObjectWithIdentityHash
from rsqueakvm.model.variable import W_BytesObject
from rsqueakvm.plugins.plugin import Plugin
from rsqueakvm.util.cells import Cell
from rsqueakvm.util.system import IS_SHELL, IS_WINDOWS

from rpython.rlib import rsocket, _rsocket_rffi, objectmodel
Expand All @@ -23,12 +23,6 @@ def non_blocking_recv(self, count):
return self.socket.recv(count, _rsocket_rffi.MSG_DONTWAIT)


class Cell(object):
_attrs_ = ["value"]
def __init__(self, value): self.value = value
def set(self, v): self.value = v
def get(self): return self.value

ResolverUninitialized = 0
ResolverReady = 1
ResolverBusy = 2
Expand Down
29 changes: 25 additions & 4 deletions rsqueakvm/util/cells.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
from rpython.rlib import jit
from rpython.rlib.cache import Cache
from rpython.rlib.objectmodel import specialize, import_from_mixin

from rsqueakvm.util.version import Version


class QuasiConstantCache(Cache):
def _build(self, obj):
class NewQuasiConst(object):
import_from_mixin(QuasiConstantMixin)
return NewQuasiConst


cache = QuasiConstantCache()


class QuasiConstantMixin(object):
"""Mixin for constant values that can be edited, but will be promoted
to a constant when jitting."""
Expand All @@ -28,9 +39,19 @@ def changed(self):
self.set(Version())


@specialize.memo()
def QuasiConstant(initial_value, cls=None):
if cls is not None:
return cache.getorbuild(cls)(initial_value)
return cache.getorbuild(type(initial_value))(initial_value)


@specialize.arg(1)
@specialize.argtype(0)
def QuasiConstant(initial_value, type=object):
class NewQuasiConst(object):
import_from_mixin(QuasiConstantMixin)
return NewQuasiConst(initial_value)
def Cell(initial_value, type=object):
class NewCell(object):
_attrs_ = ["value"]
def __init__(self, value): self.value = value
def set(self, v): self.value = v
def get(self): return self.value
return NewCell(initial_value)

0 comments on commit 971903d

Please sign in to comment.