tag:blogger.com,1999:blog-39405680149383338822024-03-09T17:26:03.267+01:00Wardrobe strengthGood enough thoughtsTobiashttp://www.blogger.com/profile/15796529762063980134noreply@blogger.comBlogger38125tag:blogger.com,1999:blog-3940568014938333882.post-54659884144303522162013-02-19T10:42:00.003+01:002013-02-19T10:45:18.356+01:00JVMs and kill signals<p>Ever wondered how a JVM reacts to various kill signals? The (intended) behaviour might be documented somewhere already, but I found having a table of the actual behaviour available quite useful. In particular I wanted to know which kill signals trigger the JVM to run registered shutdown hooks, and which kill signals don't actually terminate the JVM. So I decided to compile a table of that information.</p>
<p>I wrote up a small Java application that just registers a shutdown hook that I can detect whether it has executed or not, and then sleeps until I get a chance to kill it:</p>
<code><pre>
class Death {
public static void main(String... args) throws Exception {
Runtime.getRuntime().addShutdownHook( new Thread(){
@Override
public void run()
{
System.out.println("Shutting down");
}
} );
for (;;) Thread.sleep(100);
}
}
</pre></code>
<p>Then I ran the program in one terminal window (<code>java Death; echo $?</code>) while iterating through all kill signals (0-31) in another:<p>
<code><pre>
kill -$SIGNAL $(jps | grep Death | cut -d\ -f1)
</pre></code>
<table>
<tr><th>signal</th><th>shutdown</th><th>runs hook</th><th>exit code</th><th>comment</th></tr>
<tr><td>default (15)</td><td>yes</td><td>yes</td><td>143</td><td>SIGTERM is the default unix kill signal</td></tr>
<tr><td>0</td><td>no</td><td>-</td><td>-</td><td></td></tr>
<tr><td>1 (SIGHUP)</td><td>yes</td><td>yes</td><td>129</td><td></td></tr>
<tr><td>2 (SIGINT)</td><td>yes</td><td>yes</td><td>130</td><td>SIGINT is the signal sent on ^C</td></tr>
<tr><td>3 (SIGQUIT)</td><td>no</td><td>-</td><td>-</td><td>Makes the JVM dump threads / stack-traces</td></tr>
<tr><td>4 (SIGILL)</td><td>yes</td><td>no</td><td>134</td><td>Makes the JVM write a core dump and abort on trap 6</td></tr>
<tr><td>5</td><td>yes</td><td>no</td><td>133</td><td>Makes the JVM exit with "Trace/BPT trap: 5"</td></tr>
<tr><td>6 (SIGABRT)</td><td>yes</td><td>no</td><td>134</td><td>Makes the JVM exit with "Abort trap: 6"</td></tr>
<tr><td>7</td><td>yes</td><td>no</td><td>135</td><td>Makes the JVM exit with "EMT trap: 7"</td></tr>
<tr><td>8 (SIGFPE)</td><td>yes</td><td>no</td><td>134</td><td>Makes the JVM write a core dump and abort on trap 6</td></tr>
<tr><td>9 (SIGKILL)</td><td>yes</td><td>no</td><td>137</td><td>The JVM is forcibly killed (exits with "Killed: 9")</td></tr>
<tr><td>10 (SIGBUS)</td><td>yes</td><td>no</td><td>134</td><td>Emulates a "Bus Error"</td></tr>
<tr><td>11 (SIGSEGV)</td><td>yes</td><td>no</td><td>134</td><td>Emulates a "Segmentation fault"</td></tr>
<tr><td>12</td><td>yes</td><td>no</td><td>140</td><td>Makes the JVM exit with "Bad system call: 12"</td></tr>
<tr><td>13</td><td>no</td><td>-</td><td>-</td><td></td></tr>
<tr><td>14</td><td>yes</td><td>no</td><td>142</td><td>Makes the JVM exit with "Alarm clock: 14"</td></tr>
<tr><td>15 (SIGTERM)</td><td>yes</td><td>yes</td><td>143</td><td>This is the default unix kill signal</td></tr>
<tr><td>16</td><td>no</td><td>-</td><td>-</td><td></td></tr>
<tr><td>17</td><td>no</td><td>-</td><td>145</td><td>Stops the application (sends it to the background), same as ^Z</td></tr>
<tr><td>18</td><td>no</td><td>-</td><td>146</td><td>Stops the application (sends it to the background), same as ^Z</td></tr>
<tr><td>19</td><td>no</td><td>-</td><td>-</td><td></td></tr>
<tr><td>20</td><td>no</td><td>-</td><td>-</td><td></td></tr>
<tr><td>21</td><td>no</td><td>-</td><td>149</td><td>Stops the application (sends it to the background), same as ^Z</td></tr>
<tr><td>22</td><td>no</td><td>-</td><td>150</td><td>Stops the application (sends it to the background), same as ^Z</td></tr>
<tr><td>23</td><td>no</td><td>-</td><td>-</td><td></td></tr>
<tr><td>24</td><td>yes</td><td>no</td><td>152</td><td>Makes the JVM exit with "Cputime limit exceeded: 24"</td></tr>
<tr><td>25</td><td>no</td><td>-</td><td>-</td><td></td></tr>
<tr><td>26</td><td>yes</td><td>no</td><td>154</td><td>Makes the JVM exit with "Virtual timer expired: 26"</td></tr>
<tr><td>27</td><td>yes</td><td>no</td><td>155</td><td>Makes the JVM exit with "Profiling timer expired: 27"</td></tr>
<tr><td>28</td><td>no</td><td>-</td><td>-</td><td></td></tr>
<tr><td>29</td><td>no</td><td>-</td><td>-</td><td></td></tr>
<tr><td>30</td><td>yes</td><td>no</td><td>158</td><td>Makes the JVM exit with "User defined signal 1: 30"</td></tr>
<tr><td>31</td><td>yes</td><td>no</td><td>134</td><td>Makes the JVM exit on Segmentation fault</td></tr>
</table>
<p>This list was compiled using (a quite old) Oracle Hotspot Java 8 EA on Mac OS X:</p>
<code><pre>
java version "1.8.0-ea"
Java(TM) SE Runtime Environment (build 1.8.0-ea-b65)
Java HotSpot(TM) 64-Bit Server VM (build 25.0-b09, mixed mode)
</pre></code>
<p>Hope this is useful to more people than myself.</p>Tobiashttp://www.blogger.com/profile/07972773521986135690noreply@blogger.com4tag:blogger.com,1999:blog-3940568014938333882.post-54232520651764467602011-10-12T10:16:00.000+02:002011-10-12T10:16:21.113+02:00Be my friend, work with me<p>We are hiring engineers for <a href="http://neo4j.org/">Neo4j</a> at <a href="http://neotechnology.com/">Neo Technology</a>, and I want to work with you!</p>
<p>I never grew up and got a job. While I was still in college I found myself in a career as an open source developer and international speaker. Sounds like a job? Well I never got paid (other than different organizations sponsoring my trips and hotel expenses), I did it for the fun of it. This got me a bit of attention, and a few offers from high profile companies. No. Even before I graduated I had joined a startup (pre funding), founded by some friends of mine. They worked on interesting technology, and did things differently, challenging the established technology of the field of database management systems. Perhaps that spoke to the mindset of my inner rebel.</p>
<p>Now, four years after me joining Neo Technology, this once small group of friends who used to camp on empty desks at various offices where our friends worked, has grown a lot and keeps growing faster than ever before. But to me, that original feeling never disappeared, we are still a group of friends, playing with technologies that we think are interesting.</p>
<p>I don't have much of a social life outside of work. Why would I, I have many of my closest friends at work. Even though we are over 20 people now, compared to three when I started, I really feel like we are all friends playing with cool tech. Sure there have been times when I've been a bit annoyed over having to share my toys with some new kid, but after getting to know them and the cool things they can do, we've always ended up being friends.<br />
So no, I wouldn't say that I've grown up and gotten a job yet. It doesn't feel like a job, it really does feel like play. Nor do I intend to, why would I? I have the privilege to keep playing and making a living out of it.</p>
<p>So why don't you come play with us? We've got room for more in our sandbox!</p>
<p>We are looking for great talent worldwide who can help us build the coolest sandcastle ever! You don't have to be a Java developer, but JVM experience helps. The important part is a talent for using technology in exciting new ways. Neo4j is (to my knowledge) the most widely used graph database in the world. It is an awesome product, but still has lots of room for improvement, from the lower levels, to the higher for modeling, or bindings for different languages. Everything is open source, so the best resume you can send us is a code contribution.</p>
<p>While we are recruiting from anywhere in the world, we are still trying to keep the organizations focused around only a few locations. So being able to relocate to the <a href="http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=1370+Willow+Road,+Menlo+Park,+CA+(Neo+Technology+US)&aq=&sll=37.477975,-122.151182&sspn=0.006309,0.012392&vpsrc=0&ie=UTF8&hq=&hnear=1370+Willow+Rd,+Menlo+Park,+California+94025&t=h&z=17&iwloc=A">San Francisco Bay Area</a> or <a href="http://maps.google.com/maps?f=q&source=s_q&hl=en&geocode=&q=Anckargripsgatan+3,+Malm%C3%B6,+Sverige+(Neo+Technology+Sweden)&aq=t&sll=37.477975,-122.151182&sspn=0.006309,0.012392&vpsrc=0&ie=UTF8&hq=&hnear=Anckargripsgatan+3,+211+19+Malm%C3%B6,+Sk%C3%A5ne+L%C3%A4n,+Sweden&t=h&z=16&iwloc=A">Malmö, Sweden</a> is a huge plus. We are looking for product engineers, QA experts, and support engineers to help <a href="http://neotechnology.com/customers/">our customers</a> with everything from domain design to troubleshooting. Versatility is expected, you will not be doing one task only, product engineering experience makes for better support engineers, and customer support experience makes for better product engineers.</p>
<p>For official details see: <a href="http://neotechnology.com/about-us/jobs/">http://neotechnology.com/about-us/jobs/</a></p>
<p>I'm looking forward to play with you.</p>Tobiashttp://www.blogger.com/profile/07972773521986135690noreply@blogger.com3tag:blogger.com,1999:blog-3940568014938333882.post-1378916889453814532011-02-18T14:15:00.000+01:002011-02-18T14:15:22.607+01:00Better support for short strings in Neo4j<p>
In the past few days I've been working on a feature in the <a href="http://neo4j.org">Neo4j Graph Database</a> to store short strings with less overhead. I'm pleased to announce that this feature is now in trunk and will be part of the next milestone release. In this blog post I will describe what a short string is, and how Neo4j now stores them more efficiently.
</p>
<p>
At <a href="http://neotechnology.com">Neo Technology</a> we spend one day each week working on what we call "lab projects", a chance to explore new, experimental features outside of the regular roadmap, that might be useful. Two weeks ago I spiked a solution for storing short strings in a compressed way as a lab day project. To understand why we first need a bit of background on how strings are usually stored in Neo4j. Since strings can be of variable length, Neo4j stores them in something called the DynamicStringStore. This consists of a number of blocks, 120 bytes in size, plus 13 bytes header for each block. A string is divided into chunks of 60 characters, each such chunk stored in its own block (a character is two bytes). For a short string, such as "hello", that if encoded in UTF-8 would occupy only 5 bytes, the overhead of storing it in the DynamicStringStore (including the property record of 22 bytes needed to reference the block in the DynamicStringStore) is almost 97 percent!
</p>
<p>
My initial spike analyzed all strings shorter than or equal to 9 characters, and if all characters were found to be 7bit ASCII, stored it directly in the property record, without involving the DynamicStringStore at all. The 7bit part is important. The property record contains a 64bit payload field, which when the DynamicStringStore is involved contains the id of the first block. 9 7bit characters sums up to 63bits, I can store that in the 64bit payload field. I can then use the high order bit to denote that the content is a full 9 char string, and if it isn't, the high order bit doesn't get set, but instead the first byte denotes the length of the string, and the rest of the 56 (7*8) bits are the actual string.
</p>
<p>
While this started out as something I thought was a fun project to hack on for a day, we quickly found use for it. When importing the <a href="http://www.openstreetmap.org/">OpenStreetMap</a> data for Germany, with this feature in place we found that the DynamicStringStore was now 80% smaller than before! Not only that but time for reading and writing strings had improved by at least 25%! (the benchmark I got this from creates nodes and relationships as well, so pure string operations is probably even faster) Such figures are great for getting a feature into the backlog.
</p>
<p>
I am not a big fan of ASCII though. It was designed for communicating with line printers, not for storing text. Also, with short strings the number of exotic characters that people use drops significantly, it is more likely to just be some simple alphanumerical name or identifier, such as "hello world", "UPPER_CASE", "192.168.0.1", or "+1.555.634.5773". So the next thing I did was to write a tool that could analyze the data stored in actual Neo4j instances and generate a report on the statistics of strings actually stored. I then sent <a href="http://lists.neo4j.org/pipermail/user/2011-February/006579.html">this</a> to our public <a href="https://lists.neo4j.org/mailman/listinfo/user">users mailing list</a>. The feedback confirmed my suspicions about what kind of text people store, and also suggested that we would be able to store up to 65% of our users strings as short strings.
</p>
<p>
Armed with statistics about actual strings I set out (along with my most recent colleague, <a href="http://digitalstain.blogspot.com/">Chris Gioran</a>) to write an even better short string encoding, and incorporate it into Neo4j. Last night we <a href="https://github.com/neo4j/graphdb/commit/b83cacfc9eb8836cd2270b0378610452d73e592d">pushed it to git</a>. The format we ended up with can select between 6 different encodings, all encoded using the high order nibble of the payload entry of the property record:
</p>
<ul>
<li><b>Numerical</b> up to 15 characters <a href="http://en.wikipedia.org/wiki/Binary-coded_decimal">binary coded decimal</a>, with the additional 6 codepoints used to encode punctuation characters commonly used in phone numbers or as thousand separators. This can encode any integer from -10<sup>15</sup> to 10<sup>16</sup> (both edges exclusively), most international phone numbers, IPv4 addresses, et.c.</li>
<li><b>All UPPER CASE strings</b> or <b>all lower case strings</b> up to 12 characters, including space, underscore, dot, dash, colon, or slash. Useful for identifiers (Java enum constant names et.c.). This doesn't support mixed case though.</li>
<li><b>Alphanumerical strings</b> up to 10 characters, including space or underscore. Supports mixed case.</li>
<li><b>European words</b> up to 9 characters, this includes alphanumerical, space, underscore, dash, dot and the acute characters in the <a href="http://en.wikipedia.org/wiki/ISO/IEC_8859-1#Codepage_layout">latin-1 table</a>. Useful for building translation graphs.</li>
<li><b>Latin-1</b> up to 7 characters. Will give you parenthesis if you have those in a short string.</li>
<li><b>UTF-8</b> if the string can be encoded in 7 bytes (or less). Useful for short CJK strings for example.</li>
</ul>
<p>
The code is still in internal review, and shouldn't be considered stable until its inclusion in the next milestone release a week from now. But I am very exited about the benefits this will give to Neo4j users, both in terms of lower storage sizes, but also in terms of performance improvements. Reading (and writing) a string that is encoded as a short string is much faster than reading (or writing) a string in the DynamicStringStore, since it is only one disk read instead of two.
</p>
<p>
A big thank you goes out to the people in the Neo4j community who provided me with the string statistics that made this possible.
</p>
<p>Happy hacking</p>Tobiashttp://www.blogger.com/profile/15796529762063980134noreply@blogger.com3tag:blogger.com,1999:blog-3940568014938333882.post-48277106973530964112009-12-15T00:22:00.000+01:002009-12-15T00:22:38.827+01:00Seamless Neo4j integration in Django<p>
About a year ago I gave a <a href="http://www.parleys.com/display/PARLEYS/Home#slide=1;title=Jython;talk=30375937">presentation</a> at <a href="http://www.devoxx.com/display/JV08/Home">Devoxx</a> where I showed off how easy it was to use any Java library with Django in Jython. The library I demonstrated this with was of course <a href="http://www.neo4j.org/">Neo4j</a>. I had written some code for using Neo4j to define models for Django, and now it is ready to be released for you to use it.
</p>
<!-- TODO: get in 'how': Integrates on a model layer, replacing the ORM, like what google did to integrate bigtable with django for appengine - Not a DB-engine from the Django perspective, since there is no SQL. -->
<p>
The way that the integration between Django and Neo4j is implemented is in the Model layer. Since Neo4j does not have a SQL engine it would not have been efficient or practical to implement the support as a database layer for Django. Google did their implementation in the same way when they integrated BigTable with Django for App Engine. This means that there will be some minor modifications needed in your code compared to using <a href="http://www.postgresql.org/">PostgreSQL</a> or <a href="http://www.mysql.com/">MySQL</a>. Just as with BigTable on App Engine, you will have to use a special library for defining your models when working with Neo4j, but the model definition is <i>very</i> similar to Djangos built in ORM. With persistence systems that integrate on the database layer the only difference is in configuration, but that requires the database to fit the mold of a SQL database.
</p>
<h2>Why the **** has this taken a year to finish?</h2>
<p><i>Short answer:</i> <a href="http://pragprog.com/titles/tpp/the-pragmatic-programmer"><abbr title="The Pragmatic Programmer, page 2">The cat ate my source code</abbr></a>.</p>
<p>
A mess of symlinks that stemmed from the fact that Jython didn't have good support for <a href="http://pypi.python.org/pypi/setuptools">setuptools</a> when I started writing this code actually lead to the complete loss of my source code. But to be honest the code wasn't that good anyways. I wanted to add support for <a href="http://docs.djangoproject.com/en/dev/ref/contrib/admin/">Django's administration interface</a>, and I knew that undertaking would require a complete rewrite of my code. A complete rewrite is done and now it will be possible for me to support the administrative interface of Django in the next release. So why not until now, a year after the first prototype? I was working on other things, it's that simple.
</p>
<h2>Getting started</h2>
<p>
While the demonstration I gave a year ago was geared towards Jython, since that was the topic of the presentation, the <a href="http://components.neo4j.org/neo4j.py/">Python bindings for Neo4j</a> work equally well with CPython. That is all you need, Neo4j and Django, the Python bindings for Neo4j comes with a Django integration layer built in as of the most recent revisions in the repository. The source distribution also contains a few sample applications for demonstrating how the integration works. The Django integration is still in a very early stage of development, but the base is pretty solid, so new features should be much easier to add now. Since the state is pre-alpha, installation from source is the only option at the moment. Let me walk you through how to get things up and running:
</p>
<ul>
<li>Set up and activate a <a href="http://pypi.python.org/pypi/virtualenv">virtualenv</a> for your development. This isn't strictly necessary, but it's <b>so</b> nice to know that you will not destroy your system Python installation if you mess up. Since we got Jython to support virtualenv I use it for everything. If you use CPython your virtualenv will contain a <code>python</code> executable, and if you use Jython it will contain a <code>jython</code> executable, I will refer to either simply as <i><code>python</code></i> from here on, but substitute for that for <code>jython</code> if you, like me, prefer that implementation.</li>
<li><i>If you are using CPython:</i> Install <a href="http://sourceforge.net/projects/jpype/files/">JPype</a>, it is currently a dependency for accessing the JVM-based core of Neo4j from CPython:<pre>
$ unzip <abbr title="JPype 0.5.4.1 is the most recent release at the time of writing">JPype-0.5.4.1.zip</abbr>
$ cd <abbr title="JPype 0.5.4.1 is the most recent release at the time of writing">JPype-0.5.4.1</abbr>
$ python setup.py install
</pre></li>
<li>Check out the source code for the Python bindings for Neo4j, and install it:<pre>
$ svn co https://svn.neo4j.org/components/neo4j.py/trunk neo4j-python
$ cd neo4j-python
$ python setup.py install
</pre></li>
<li>Install Django:<pre>
$ easy_install django
</pre></li>
<li>Create a new Django project:<pre>
$ django-admin.py startproject neo4django
</pre></li>
<li>Create a new app in your Django project:<pre>
$ python neo4django/manage.py startapp business
</pre></li>
<li>Set up the configuration parameters for using with Neo4j in Django by adding the following configurations to your <code>settings.py</code>:<pre name="code" class="python">
NEO4J_RESOURCE_URI = '/var/neo4j/neo4django'
# NEO4J_RESOURCE_URI should be the path to where
# you want to store the Neo4j database.
NEO4J_OPTIONS = {
# this is optional and can be used to specify
# extra startup parameters for Neo4j, such as
# the classpath to load Neo4j from.
}
</pre>You can ignore the default Django configurations for RDBMS connections if you only plan to use Neo4j, but if you want to use Djangos built in Admin interface (not supported with Neo4j quite yet) or authentication module you will need to configure this.</li>
<li>You are now ready to create your first Neo4j backed domain objects for your Django application, by editing <code>business/models.py</code>. Let's create a simple model for companies with owners and employees:<pre name="code" class="python">
from neo4j.model import django_model as model
class Person(model.NodeModel):
first_name = model.Property()
last_name = model.Property()
def __unicode__(self):
return u"%s %s" % (self.first_name, self.last_name)
class Company(model.NodeModel):
name = model.Property(indexed=True)
owners = model.Relationship(Person,
type=model.Outgoing.OWNED_BY,
related_name="owns",
)
employees = model.Relationship(Person,
type=model.Incoming.WORKS_AT,
related_name="employer",
related_single=True, # Only allow Persons to work at one Company
)
def __unicode__(self):
return self.name
</pre></li>
<li>That's it, you've created your first Django domain model using Neo4j, let's try it out:<pre>
$ python neo4django/manage.py shell
>>> from neo4django.business import models
>>> seven_eleven = models.Company.objects.create(name="Seven Eleven")
>>> seven_eleven.employees.add(
... models.Person.objects.create(
... first_name="Sally", last_name="Getitdone"),
... models.Person.objects.create(
... first_name="John", last_name="Workerbee"))
>>> seven_eleven.save() # store the newly created relationships
>>> people = list(seven_eleven.employees.all())
>>> someone = people[0]
>>> print someone, "works at", someone.employer
</pre></li>
</ul>
<p>
Notice how the model objects are compatible with model objects created using Djangos built in ORM, making it easy to port your existing applications to a Neo4j backend, all you need to change is the model definitions. For more examples, see the example directory in the repository:
<a href="https://svn.neo4j.org/components/neo4j.py/trunk/src/examples/python/">https://svn.neo4j.org/components/neo4j.py/trunk/src/examples/python/</a>.
</p>
<h2>Future evolution</h2>
<p>
There is still more work to be done. As this is the first release, there are likely to be bugs, and I know about a few things (mainly involving querying) that I have not implemented support for yet. I also have a list of (slightly bigger) features that I am going to add as well, to keep you interested, I'll list them with a brief explanation:
</p>
<ul>
<li><i>Add support for the Django admin interface.</i> You should be able to manage your Neo4j entities in the Django administration interface, just as you manage ORM entities. To do this I need to dig further into the internals of the admin source code, to find out what it expects from the model objects to be able to pick up on them and manage them. The hardest part with this is that the admin system has a policy of silent failure, meaning that it will not tell me how my code violates its expectations.</li>
<li><i>Add support for Relationship models.</i> Currently you can only assign properties to nodes in the domain modeling API, you should be able to have entities represented by relationships as well. The way you will do this is by extending the <code>Relationship</code>-class.</li>
<li><i>Add a few basic property types.</i> I will add support for creating your own property types by extending the <code>Property</code>-class (this is implemented already, but not tested, so if it works it's only by accident). I will also add a few basic subtypes of <code>Property</code>, a <a href="http://docs.python.org/library/datetime.html"><code>datetime</code></a> type at the very least. I will also add support for choosing what kind of index to use with each indexed property, in the case of <code>datetime</code> a <a href="http://components.neo4j.org/index-util/apidocs/org/neo4j/util/timeline/TimelineIndex.html"><code>Timeline</code>-index</a> seems quite natural for example... Supporting enumerated values for Properties is also planned, i.e. limiting the set of allowed values to an enumerated set of values.</li>
<li><i>Tapping in to the power of Neo4j.</i> By adding support for methods that do arbitrary operations on the graph (such as <a href="http://components.neo4j.org/neo4j.py/apidoc/traverse.html">traversals</a>), and where the returned nodes are then automatically converted to entity objects. I think this will be a really cool and powerful feature, but I have not worked out the details of the API yet.</li>
</ul>
<p>
Report any bugs you encounter to either the <a href="https://trac.neo4j.org/">Neo4j bug tracker</a>, or on the <a href="https://lists.neo4j.org/mailman/listinfo/user">Neo4j mailing list</a>. Suggestions for improvements and other ideas are also welcome on the mailing list, to me personally, or why not as a comment on this blog.
</p>
<p><i><b>Happy Hacking</b></i></p>Tobiashttp://www.blogger.com/profile/15796529762063980134noreply@blogger.com11tag:blogger.com,1999:blog-3940568014938333882.post-56630450854188011642009-08-07T01:28:00.001+02:002009-08-07T01:31:14.984+02:00Java integration in future Jython<p>
I've <a href="http://fiber-space.de/wordpress/?p=858">seen</a> <a href="http://fiber-space.de/wordpress/?p=908">a lot</a> <a href="http://fiber-space.de/wordpress/?p=1047">of this</a> <a href="http://fiber-space.de/wordpress/?p=1059">lately</a>, so I thought that it was time for an actual Jython developer (myself) to share some ideas on how Java integration in Jython could be improved. At the same time I'd like to propose some changes that could make the different Python implementations more unified, and even could lead to a common Java integration API in all of them.
</p>
<p>
The most basic part of the Java integration in Jython is the ability to import and use Java classes. This is impossible for other Python implementations to do in the same way, and thus breaks compatibility fundamentally. I therefore propose that we remove this functionality as it is in Jython today (!). Instead we should look at how IronPython enables using CLR (.NET) classes. In IronPython you first need to <code>import clr</code> before you can access any of the CLR types. The same is done in other languages on the JVM as well, for example JRuby where you need to <code>require 'java'</code> before using any Java libraries. I propose we require something similar in Jython, and what better package to require you to import than <code>java</code>?
</p>
<p>
<i>An observation:</i> The <code>java</code> package in Java does not contain any classes, only sub-packages. Furthermore all the sub-packages of the <code>java</code> package follow the Java naming conventions, i.e. They all start with a lowercase letter. This gives us a name space to play with: anything under the <code>java</code> package that starts with an uppercase letter.
</p>
<p>
<i>What happens when you <code>import java</code>?</i> The <code>java</code> Python module is a "magic module" that registers a Python import hook. This import hook will then enable you to import real Java packages and classes. In Jython many of the builtin libraries will of course <code>import java</code>, which means that this will be enabled by default in Jython. But writing code that is compatible across Python implementations would now be possible, by simply ensuring that you <code>import java</code> before any other Java packages.
</p>
<h2>The content of the <code>java</code> module</h2>
<p>
Most if not all of what is needed to utilize Java classes from Python code is provided by the import hook that the <code>java</code> module registers when it is loaded. This means that the content of the <code>java</code> module needs to deal with the other direction of the interfacing: defining and implementing APIs in Python that Java code can utilize. I propose that the Python <code>java</code> module contain the following:
</p>
<dl>
<dt><code>JavaClass</code></dt><dd>A class decorator that exposes the decorated class as a class that can be accessed from Java. Accepts a <code>package</code> keyword argument for defining the Java package to define the class in, if omitted it is derived from the __module__ attribute of the class.<br />Possibly <code>JavaClass</code> should also be the Python type of imported Java classes.</dd>
<dt><code>Field</code></dt><dd>An object for defining Java fields in classes. Takes a single argument, the type of the field. Example usage:<pre name="code" class="python">@java.JavaClass
class WithAField:
data = java.Field(java.lang.String)
</pre></dd>
<dt><code>Array</code></dt><dd>An object for defining Java arrays. This is used to define Java array types. Examples:<ul>
<li><code>Array[java.Primitive.int]</code> corresponds to the Java type <code>int[]</code></li>
<li><code>Array[java.lang.String]</code> corresponds to the Java type <code>java.lang.String[]</code></li>
<li><code>Array</code> corresponds to the Java type <code>java.lang.Object[]</code></li>
<li><code>Array[Array[java.lang.String]]</code> corresponds to the Java type <code>java.lang.String[][]</code></li>
</ul></dd>
<dt><code>Access</code></dt><dd>A set of Java access definition decorators. Contains:<ul>
<li><code>Access.public</code></li>
<li><code>Access.package</code> - this needs to be explicitly available since it does not make sense as the default in Python code.</li>
<li><code>Access.protected</code></li>
<li><code>Access.module</code> - for the new access modifier in the upcoming module system (a.k.a. Project Jigsaw) for Java.</li>
<li><code>Access.private</code></li>
<li>The default access modifier should either be <code>public</code> or the absence of an access modifier decorator would mean that the method is not exposed in the Java class at all. This needs further discussion.</li>
</ul></dd>
<dt><code>Primitive</code></dt><dd>The set of primitive types in Java:<ul>
<li><code>Primitive.void</code></li>
<li><code>Primitive.boolean</code></li>
<li><code>Primitive.byte</code></li>
<li><code>Primitive.char</code></li>
<li><code>Primitive.short</code></li>
<li><code>Primitive.int</code></li>
<li><code>Primitive.long</code></li>
<li><code>Primitive.float</code></li>
<li><code>Primitive.double</code></li>
<li>These can be used as type parameters for <code>Array</code> but not for Generic types (Since primitives are not allowed as generic type parameters in Java).</li>
</ul></dd>
<dt><code>Overload</code></dt><dd>Used to implement (and define) overloaded methods, several different methods with the same name, but different type signatures. Example usage:<code><pre name="code" class="python">
@java.JavaClass
class WithOverloadedMethod:
@java.Access.public
def method(self, value:java.lang.String) -> java.util.List[java.lang.String]:
...
@java.Overload(method)
@java.Access.public
def method(self, value:java.lang.Integer) -> java.lang.String:
...
@java.Overload(method)
@java.Access.public
def method(self, value:java.lang.Iterable[java.lang.String]) -> java.Primitive.void:
...
</pre></code></dd>
</dl>
<p>
Java classes and interfaces, when imported, are Pythonized in such a way that they can be used as bases for Python classes. Generics are specified by subscripting the generic Java class. Java annotations are Pythonized in a way that turns them into decorators that add a special attribute to the decorated element: <code>__java_annotations__</code>. Annotations on imported Java classes and methods would also be exposed through the <code>__java_annotations__</code> property for consistency. <code>Access</code> modifiers would similarly add a <code>__java_access__</code> property to the object they decorate.
</p>
<p>
Kay Schluer also suggested allowing <a href="http://fiber-space.de/wordpress/?p=1047">decorators on assignments</a>, to be able to support annotations on fields. I don't really have an opinion on this. Since I don't think fields should be exported in any public API anyway it's a bit useless, and for the the cases where fields are used (such as dependency injection systems) I think it suffices to have it all in the same assignment: <code>dependency = javax.inject.Inject(java.Access.private(java.Field(JavaClassIDependOn)))</code>, the name will be extracted to be "<code>dependency</code>" when the class is processed by the <code>JavaClass</code> class decorator. But if others find assignment decorators useful, I am not opposed to them. If assignment decorators are added to Python, it might be worth considering having a slightly different signature for these decorator function, so that the name of the target variable is passed as a parameter as well. Then my example could look like this:
</p>
<code><pre name="code" class="python">
@java.JavaClass
class WithInjectedDependency:
@javax.inject.Inject # This is <a href="http://jcp.org/en/jsr/detail?id=330">JSR 330</a> by the way
@java.Access.private
@java.Field
dependency = JavaClassIDependOn
# could expand to: dependency = javax.inject.Inject(
# "dependency", java.Access.private(
# "dependency", java.Field(
# "dependency", JavaClassIDependOn)))
# or to the same thing as above, depending on how
# assignment decorators were implemented...
</pre></code>
<p>
When defining methods in Java integration classes we use Python 3 function annotations to define the method signatures. These can be omitted, the default types in that case would of course be <code>java.lang.Object</code>. It is important that we support exposing classes that don't have any Java integration added to them from Jython, since we want to enable importing existing Python libraries into Java projects and use them without having to port them. These classes will not have the <code>JavaClass</code> decorator applied to them. Instead this will be done automatically by Jython at the point when the Python class first need to expose a class to Java. This is not something that the <code>java</code> module need to deal with, since it doesn't fit with other Python implementations.
</p>
<h2>Outstanding issues</h2>
<p>
There are still a few Java integration issues that I have not dealt with, because I have not found a solution that I feel good about yet.
</p>
<dl>
<dt>Defining Java interfaces</dt><dd>Is this something we need to be able to do? If so, the proper approach is probably to add a <code>JavaInterface</code> decorator to the <code>java</code> module, similar to the <code>JavaClass</code> decorator.</dd>
<dt>Defining Java enums</dt><dd>This might be something that we want to support. I can think of two options for how to declare the class. Either we add a <code>JavaEnum</code> decorator to the <code>java</code> module, or we add special case treatment for when a class extends <code>java.lang.Enum</code> (I am leaning towards this approach). Then we need to have some way to define the enum instances. Perhaps something like this:<code><pre name="code" class="python">
@java.JavaClass
class MyEnum(java.lang.Enum):
ONE = java.EnumInstance(1)
TWO = java.EnumInstance(2, True)
THREE = java.EnumInstance(3, True)
FOUR = java.EnumInstance(4)
def __init__(self, number, is_prime=False):
self.number = number
self.is_prime = is_prime
def __str__(self):
return self.name()
class SEVENTEEN(java.EnumInstance):
"""This is an enum instance with specialized behavior.
Will extend MyEnum, but there will only be one instance."""
def __init__(self):
"""This class gets automatically instantiated
by the __metaclass__ of Enum."""
self.number = 17
self.is_prime = True
def __str__(self):
return "The most random number there is."
</pre></code></dd>
<dt>Defining generic types</dt><dd>I have discussed how to specify type parameters for generic types, but how would you <i>define</i> a generic Java type in Python? How about something like this:<code><pre name="code" class="python">
@java.JavaClass
class GenericClass:
T = java.TypeParameter() # default is "extends=java.lang.Object"
C = java.TypeParameter(extends=java.util.concurrent.Callable)
</pre></code>This gets complicated when wanting to support self references in the type parameters, but the same is true for implemented interfaces, such as:<code><pre name="code" class="java">
class Something implements Comparable<? extends Something> {
...
}
</pre></code></dd>
<dt>Defining Java annotations</dt><dd>I have dealt with supporting the use of Java annotations, but what about defining them? I highly doubt that defining Java annotations in Python is going to be useful, but I prefer to not underestimate what developers might want to do. I do however think we could get far without the ability to define Java annotations in Python, but if we were to support it, what would it look like? Defining the class would probably be a lot like how enums are defined, either by special casing <code>java.lang.annotation.Annotation</code> or providing a special <code>java.Annotation</code> decorator.<code><pre name="code" class="python">
@java.JavaInterface
class MyAnnotation(java.lang.annotation.Annotation):
name = java.AnnotationParameter(java.lang.String)
description = java.AnnotationParameter(java.lang.String, default="")
</pre></code></dd>
</dl>
<h2><code>java</code> for other Python implementations</h2>
<p>
I mentioned that requiring the user to explicitly <code>import java</code> to make use of Java classes would make it possible for other Python implementations to support the same Java integration API. So what would the default implementation of the <code>java</code> module look like? There is a very nice standardized API for integrating with Java from other external programming languages: JNI. The default <code>java</code> module would simply implement the same functionality as the Jython counterpart by interacting with JNI using <code>ctypes</code>. Since <code>ctypes</code> is supported by all Python implementations (Jython support is under development) the <code>java</code> integration module would work across all Python implementations without additional effort. Right there is a major advantage over JPype and JCC (the two major Java integration modules for CPython today).
</p>
<h2>Integration from the Java perspective</h2>
<p>
I have not given as much thought to the area of utilizing Python code from Java. Still this is one of the most important tasks for Jython to fulfill. This section is therefore just going to be some ideas of what I want to be able to do.
</p>
<dl>
<dt>Use Python for application scripting</dt><dd>This is possible today, and a quite simple case, but I still think that it can be improved. Specifically the problem with Jython today is that there is no good API for doing so. Or to be frank, there is hardly an API at all. This is being improved upon though, the next update of Jython will include an updated implementation of the Java Scripting API, and the next release will introduce a first draft of a proper Jython API, something that we will support long term after a few iterations, and that you can build your applications against.</dd>
<dt>Use Jython to implement parts of your application</dt><dd>We want to be able to write an polyglot applications, where parts of it is implemented in Python. This is more than just scripting the application. Applications generally work without scripts. We want to be able to write the implementation of parts of an application in Python with Jython. This is possible today, but a bit awkward without an official Jython API. This is being worked on in a separate project called <a href="http://kenai.com/projects/plyjy">PlyJy</a>, where we are experimenting with an API for creating object factories for Jython. Jython object factories are objects that call into a Python module, instantiate a Python class, conforms it to a Java interface and returns it. So far this project is looking good and there is a good possibility that this will get included in the Jython API.</dd>
<dt>Directly link (Java) applications to Python code</dt><dd>This is where things are starting to get advanced. It would be nice if you could write a library in Python (or import an existing one) and link your Java code with the classes and functions defined in that library directly. This would require Jython to generate Java proxies, actual Java classes where the methods correspond to the actual signatures, with proper constructors and the things you would need to use it like any other Java code, while hiding away the dynamic aspects that make it Python. This could either be done through a compilation step, where some Jython proxy compiler generates the proxies that the Java code can link with, or through utilizing a <code>ClassLoader</code> that loads a Python module and inspects the content, automatically generating the required proxies. With the <code>ClassLoader</code> approach javac would need to know about and use it to load signatures from Python code. This is of course where the Java integration decorators described above fits in.</dd>
</dl>
<h2>What do you think?</h2>
<p>
I would love to get feedback on these ideas. Either through comments to this entry, via <a href="http://twitter.com/thobe">Twitter</a> or on the <a href="https://lists.sourceforge.net/lists/listinfo/jython-dev">Jython-dev mailing list</a>.
</p>
<p style="color: red;">Please note that the ideas presented in this blog post are my own and does not reflect any current effort in the Jython project.</p>Tobiashttp://www.blogger.com/profile/15796529762063980134noreply@blogger.com9tag:blogger.com,1999:blog-3940568014938333882.post-65105466540238578072009-07-31T13:10:00.000+02:002009-07-31T13:10:26.811+02:00"Social networking" killed productivity<p>
<a href="http://srtsolutions.com/blogs/diannemarsh/archive/2009/07/29/funny-thing-happened-to-twitter.aspx">Twitter has become work</a>. Not <i>acceptable for work</i>, i.e. something that is not frowned upon to do at work, but actual work, something you are <i>required to do</i> at work. At least this is the case if you are involved somewhere where the development team is the marketing team, like a startup or an open source project. For the record my involvement in <a href="http://neo4j.org">Neo4j</a> qualify to both categories, and <a href="http://jython.org">Jython</a> is most certainly an open source project, and quite a high profile such as well.
</p>
<p>
In order to stay on top of things in this situation you easily find yourself with push based twitter notification or at least reading a lot of material on a regular basis. I for example get about 150 to 200 tweets per day from the people I follow. Combine this with the expectation to stay on top of email (yet again yo go for push), and you've got a constant stream of interrupts, and this really kills productivity.
</p>
<p>
Just the other day I read the <i><a href="http://www.aaronsw.com/weblog/offline2">Life Offline</a></i> posts by Aaron Swartz, and found that very much recognize myself in <a href="http://www.aaronsw.com/weblog/offline">how he describes</a> the problems of the constant online presence. It would be wonderful if I, like he was able to do, could take a long stretch of time away from being connected, but I don't think that is possible, at least not now or in a near future. The problem stands though, I am not being productive. And some things don't get done in time. And this is a problem.
</p>
<p>
I've tried shifting my email and twitter use to only do processing of these things once per day, but it still takes two hours or more from my day to simply process the incoming stream. By processing I mean:
</p>
<ul>
<li>Read all the email and define actions.</li>
<li>Read all tweets, open tabs for links that seem interesting, skim those pages and define actions.</li>
<li>Read feeds and define actions.</li>
</ul>
<p>
That takes two hours. Then I still have to <i>perform</i> the actions that I have defined. Which could take up to the rest of the day.
</p>
<p>
I noticed already about twelve years ago how destructive online communities and social networks could be, and how much time they consume. I have thus tried to stay away from them, which is why I don't use my facebook account. But when social networking has become part of work it is much harder to avoid. In the case of Twitter it is also difficult to ignore because of how hugely influential it is. Twitter is the de facto way to find out about new things and interesting articles.
</p>
<p>
I am starting to believe that perhaps Donald Knuth made a wise decision in <a href="http://www-cs-staff.stanford.edu/~uno/email.html">not having an email address</a>, but as he points out having an email address is for people who need to be on top of things, and that he does not have an email address because he does not have to be on top of things anymore. I will agree with that, Donald Knuth has contributed a lot to the field of computer science, but he is definitely not on top of things anymore. So how do you cope with both being on top of things while still being productive? Is it possible? I would love to get any insight into the secrets that I am obviously unaware of.
</p>Tobiashttp://www.blogger.com/profile/15796529762063980134noreply@blogger.com3tag:blogger.com,1999:blog-3940568014938333882.post-56033641015708725892009-07-15T01:55:00.000+02:002009-07-15T01:55:59.578+02:00Improving performance in Jython<p>
About two weeks ago I published a writeup about my findings about the <a href="http://journal.thobe.org/2009/06/performance-of-synchronization.html">performance of synchronization primitives in Jython</a> from my <a href="http://journal.thobe.org/2009/06/javaone-2009-from-my-perspective.html">presentation at JavaOne</a>. During the presentation I said that these performance issues were something that I was going to work on, and improve. And indeed I did. I cannot take full credit for this, <a href="http://zyasoft.com/pythoneering/">Jim Baker</a> played a substantial part in this work as well. The end result is still something I'm <i>very proud</i> of since we managed to improve the performance of this benchmark as much as <b>50 times</b>.
</p>
<h2>The benchmarks</h2>
<p>
The comparisons were performed based on the execution of this benchmark script invoked with:
</p>
<ul>
<li><code>JAVA_HOME=$JAVA_6_HOME jython synchbench.py</code></li>
<li><code>JAVA_HOME=$JAVA_6_HOME jython -J-server synchbench.py</code></li>
</ul>
<pre name="code" class="python"># -*- coding: utf-8 -*-
from __future__ import with_statement, division
from java.lang.System import nanoTime
from java.util.concurrent import Executors, Callable
from java.util.concurrent.atomic import AtomicInteger
from functools import wraps
from threading import Lock
def adder(a, b):
return a+b
count = 0
def counting_adder(a, b):
global count
count += 1 # NOT SYNCHRONIZED!
return a+b
lock = Lock()
sync_count = 0
def synchronized_counting_adder(a, b):
global sync_count
with lock:
sync_count += 1
return a+b
atomic_count = AtomicInteger()
def atomic_counting_adder(a,b):
atomic_count.incrementAndGet()
return a+b
class Task(Callable):
def __init__(self, func):
self.call = func
def callit(function):
@Task
@wraps(function)
def callable():
timings = []
for x in xrange(5):
start = nanoTime()
for x in xrange(10000):
function(5,10)
timings.append((nanoTime() - start)/1000000.0)
return min(timings)
return callable
def timeit(function):
futures = []
for i in xrange(40):
futures.append(pool.submit(function))
sum = 0
for future in futures:
sum += future.get()
print sum
all = (adder,counting_adder,synchronized_counting_adder,atomic_counting_adder)
all = [callit(f) for f in all]
WARMUP = 20000
print "<WARMUP>"
for function in all:
function.call()
for function in all:
for x in xrange(WARMUP):
function.call()
print "</WARMUP>"
pool = Executors.newFixedThreadPool(3)
for function in all:
print
print function.call.__name__
timeit(function)
pool.shutdown()
glob = list(globals())
for name in glob:
if name.endswith('count'):
print name, globals()[name]
</pre>
<p>
And the JRuby equivalent for comparison:
</p>
<pre name="code" class="ruby">
require 'java'
import java.lang.System
import java.util.concurrent.Executors
require 'thread'
def adder(a,b)
a+b
end
class Counting
def initialize
@count = 0
end
def count
@count
end
def adder(a,b)
@count = @count + 1
a+b
end
end
class Synchronized
def initialize
@mutex = Mutex.new
@count = 0
end
def count
@count
end
def adder(a,b)
@mutex.synchronize {
@count = @count + 1
}
a + b
end
end
counting = Counting.new
synchronized = Synchronized.new
puts "<WARMUP>"
10.times do
10000.times do
adder 5, 10
counting.adder 5, 10
synchronized.adder 5, 10
end
end
puts "</WARMUP>"
class Body
def initialize
@pool = Executors.newFixedThreadPool(3)
end
def timeit(name)
puts
puts name
result = []
40.times do
result << @pool.submit do
times = []
5.times do
t = System.nanoTime
10000.times do
yield
end
times << (System.nanoTime - t) / 1000000.0
end
times.min
end
end
result.each {|future| puts future.get()}
end
def done
@pool.shutdown
end
end
body = Body.new
body.timeit("adder") {adder 5, 10}
body.timeit("counting adder") {counting.adder 5, 10}
body.timeit("synchronized adder") {synchronized.adder 5, 10}
body.done
</pre>
<h2>Where we started</h2>
<div class="separator" style="clear: both; text-align: center;">
<a style="margin-left: 1em; margin-right: 1em;" imageanchor="1" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-00xsnt8REnj-waxlp0j37cTKJ2ctgttZQaXx9OdFK6GYhu-leoxaw5CrYv7cltbH7AyD0Y17NKWz4oweCkGZVpORTy9suJbBNxDaMd3bpHW0XQPWT-rmxBCyjSG_v5Et25PctFpka6Io/s1600-h/DynaLangConcurrency-Counter.png">
<img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-00xsnt8REnj-waxlp0j37cTKJ2ctgttZQaXx9OdFK6GYhu-leoxaw5CrYv7cltbH7AyD0Y17NKWz4oweCkGZVpORTy9suJbBNxDaMd3bpHW0XQPWT-rmxBCyjSG_v5Et25PctFpka6Io/s320/DynaLangConcurrency-Counter.png"/>
</a>
</div>
<p>
A week ago the performance of this Jython benchmark was bad. Compared to the equivalent code in JRuby, Jython required over 10 times as much time to complete.
</p>
<p>
When I analyzed the code that Jython and JRuby generated and executed, I came to the conclusion that the reason Jython performed so badly was that the call path from the running code to the actual <code>lock</code>/<code>unlock</code> instructions introduced too much overhead for the JVM to have any chance at analyzing and optimizing the lock. I published this analysis in <a href="http://journal.thobe.org/2009/06/performance-of-synchronization.html">my writeup on the problem</a>. It would of course be possible to lower this overhead by importing and utilizing the pure Java classes for synchronization instead of using the Jython threading module, but <i>we like how the <code>with</code>-statement reads</i> for synchronization:
</p>
<pre name="code" class="python">
with lock:
counter += 1
</pre>
<h2>Getting better</h2>
<p>
Based on my analysis of the how the <code>with</code>-statement compiles and the way that this introduces overhead I worked out the following redesign of the <code>with</code>-statement context manager interaction that would allow us to get closer to the metal, while remaining compatible with <a href="http://www.python.org/dev/peps/pep-0343/">PEP 434</a>:
</p>
<ul>
<li>When entering the <code>with</code>-block we transform the object that constitutes the context manager to a <a href="http://fisheye3.atlassian.com/browse/~raw,r=6535/jython/trunk/jython/src/org/python/core/ContextManager.java"><code>ContextManager</code></a>-object.</li>
<li><b>If</b> the object that constitutes the context manager implements the <code>ContextManager</code> interface it is simply returned. This is where context managers written in Java get their huge benefit by getting really close to the metal.</li>
<li><b>Otherwise</b> a default implementation of the <code>ContextManager</code> is returned. This object is created by retrieving the <code>__exit__</code> method and invoking the <code>__enter__</code> method of the context manager object.</li>
<li>The compiled code of the <code>with</code>-statement then only invokes the <code>__enter__</code> and <code>__exit__</code> methods of the returned <code>ContextManager</code> object.</li>
<li>This has the added benefit that even for context managers written in pure Python the <code>ContextManager</code> could be optimized and cached when we implement call site caching.</li>
</ul>
<p>
This specification was <a href="http://fisheye3.atlassian.com/browse/~raw,r=6535/jython/trunk/jython/src/org/python/core/ContextGuard.java">easily implemented</a> by <a href="http://twitter.com/jimbaker">Jim</a> and then he could rewrite the threading module in Java to let the lock implementation take direct benefit of the rewritten <code>with</code>-statement and thereby get the actual code really close to the locking and unlocking. The result were instantaneous and beyond expectation:
</p>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEir3SEj0J3sa0aS-a8W9zVudTahvwtIJoeAvRbacax7hh1naTgPYOE6Iov8nd3HcWPhz2E-GVLfrZmZpWKCp2aHOrhBRYf1fJzaBCPiqWBYqUCzIq4tCHHQYMNfYsGqpKKt1gabDsZbg7bz/s1600-h/Old-vs-JRuby-vs-New.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEir3SEj0J3sa0aS-a8W9zVudTahvwtIJoeAvRbacax7hh1naTgPYOE6Iov8nd3HcWPhz2E-GVLfrZmZpWKCp2aHOrhBRYf1fJzaBCPiqWBYqUCzIq4tCHHQYMNfYsGqpKKt1gabDsZbg7bz/s320/Old-vs-JRuby-vs-New.png" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
</div>
<p>
Not only did we improve performance, but we passed the performance of the JRuby equivalent! Even using the client compiler, with no warm up we perform almost two times better than JRuby. Turn on the server compiler and let the JIT warm up and perform all it's compilation and we end up with a speedup of slightly more than <b>50 times</b>.
</p>
<p>
A disclaimer is appropriate here. With the first benchmark (before this was optimized) I didn't have time to wait for a full warmup. This because of the fact that the benchmark was so incredibly slow at that point and the fact that I was doing the benchmarks quite late before the presentation and didn't have time to leave it running over the night. Instead I turned down the compilation threshold of the Hotspot server compiler and ran just a few warmup iterations. It is possible that the JVM could have optimized the previous code slightly better given (a lot) more time. The actual speedup might be closer to the speedup from the first code to the new code using the client compiler and no warmup. But this is still a speedup of almost 20 times, which is still something I'm very proud of. There is also the possibility that I didn't run/implement the JRuby version in the best possible way, meaning that there might be ways of making the JRuby version run faster that I don't know about. The new figures are still very nice, much nicer than the old ones for sure.
</p>
<h2>The current state of performance of Jython synchronization primitives</h2>
<p>
It is also interesting to compare how the current implementation compares to the other versions in Jython that I included in my presentation:
</p>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlT4WsBsbIu80VApPOMcg4TiQCTQMAb2ySpUxzay9GWxdzj4iQZHGMqojwIZX3kYMcZnl5rf6GsOlhNSzNB1UnxNXmyPiXjHL-RO94kxJTCDFmXxWvjoJ29NG3Zfuzek-b3ZyBMfKj60Sp/s1600-h/NewClient-vs-NewServer.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlT4WsBsbIu80VApPOMcg4TiQCTQMAb2ySpUxzay9GWxdzj4iQZHGMqojwIZX3kYMcZnl5rf6GsOlhNSzNB1UnxNXmyPiXjHL-RO94kxJTCDFmXxWvjoJ29NG3Zfuzek-b3ZyBMfKj60Sp/s320/NewClient-vs-NewServer.png" /></a></div>
<p>
Without synchronization the code runs about three times as fast as with synchronization, but the counter does not return the correct result here due to race conditions. It's interesting from the point of view of analyzing the overhead added by synchronization but not for an actual implementation. Two times overhead is quite good in my opinion. What is more interesting to see is that the fastest version from the presentation, the one using <code>AtomicInteger</code>, is now suffering from the overhead of reflection required for the method invocations compared to the synchronized version. In a system with more hardware threads (commonly referred to as "cores") the implementation based on <code>AtomicInteger</code> could still be faster though.
</p>
<h2>Where do we proceed from here?</h2>
<p>
Now that we have proven that it was possible to get a nice speedup from this redesign of the code paths the next step is to provide the same kind of optimizations for code written in pure Python. Providing a better version of <a href="http://docs.python.org/library/contextlib.html#contextlib.contextmanager"><code>contextlib.contextmanager</code></a> that exploits these faster code paths should be the easiest way to improve context managers written in Python. Then there are of course a wide range of other areas in Python where performance could be improved through the same kind of thorough analysis. I don't know at this point what we will focus on next, but you can look forward to many more performance improvements in Jython in the time to come.
</p>Tobiashttp://www.blogger.com/profile/15796529762063980134noreply@blogger.com0tag:blogger.com,1999:blog-3940568014938333882.post-32571395035208747822009-07-06T13:51:00.000+02:002009-07-06T13:51:00.400+02:00My view of EuroPython 2009<p>
I was not very enthusiastic going to <a href="http://www.europython.eu/">EuroPython</a> this year. I don't like it when all I do is fly in, give a talk then fly out again, but that was all I could afford to do for EuroPython. I go to a few conferences in a year, and all of them can be filed under expenses for me, since I don't have an employer that pays for my conference trips. With EuroPython being in Birmingham and the UK not being the cheapest country there is there was no chance for the to afford staying more than two days.
</p>
<p>
My plane landed Tuesday at lunch time, and my talk was the first talk after the afternoon keynote. I arrived to the venue with about two and a half hours to take care of registration and payment. I had requested to not pay when I registered online and was told that the best solution would be to pay when I arrived. My reason was of course that I didn't have any money when they required my online registration, freelance open source development does not pay every month. The only problem with this was that when I arrived there was no one there to take care of registration, it was only open in the morning. I tried asking a few guys in staff t-shirts, but they were not very helpful, and seemed like they just wanted me to go away. I decided to wait until the next day with my registration and went to see the keynote.
</p>
<p>
It was nice to get to see Cory Doctorow in person. His keynote was about the copyright war and why it matters to us as developers. Scary stuff. The world that the media industry is forcing on us is all but pleasant. He could even provide examples of actual cases that have already happened where large music publishers have forced open source developers working on projects they didn't like to change profession or face millions of dollars in fines. Not on the basis of the software being illegal (it wasn't), but with a lawsuit on copyright infringement from downloaded MP3 files. He also talked about how the media industry seem to prefer the entire internet to be illegal, along with open source software all together, something that Reinout van Rees has a <a href="http://reinout.vanrees.org/weblog/2009/06/30/europython-cory-doctorow.html">more complete summary of</a>.
</p>
<p>
My presentation on what would make Jython <a href="http://www.slideshare.net/thobe/a-better-python-for-the-jvm-1686090">a better Python for the JVM</a> felt like the best presentation I have given to this day. The material was an updated version of <a href="http://www.slideshare.net/thobe/a-better-python-for-the-jvm">the talk I gave at PyCon</a> earlier this year. Experience really does make you a better speaker and this was the 11th presentation of my speaker career so far (in less than two years). It really feels good to stand in front of people and talk when a large number of people are nodding and smiling at what you are saying throughout most of the presentation. And the audience was great here, they asked a lot of good questions, both during the presentation and afterwards. I didn't go through all of my slides but I covered all of my material. Some things came more natural with other slides with this crowd, so I altered the talk slightly while doing it. This was a scenario I had prepared for, I was prepared to use the slides in either way, and with this audience I felt more comfortable doing it this way. The only negative aspect of the presentation was the room, it was laid out in such a way that if I stood by my computer I would block parts of the projected screen. I had to switch to my remote control and step away from the computer to allow everyone to see the material, which meant that I could not read my speaker notes on my screen. Fortunately I knew my material well enough.
</p>
<p>
The rest of the day I went around to a few different talks in the venue, still without badge. Then me and the rest of the Jython team that were there rounded off the day at a chinese restaurant with <a href="http://www.voidspace.org.uk/">Michael Foord</a> and some fifteen other attendees, followed by a pint at an outdoor bar before me and Frank headed to our hotel. The food and beer was great but the conference venue and the hotel were not. As I mentioned the venue had a problem with the room I presented in, but being in the basement of the Birmingham Conservatoire it was dark and small, with corridors to all the presentation rooms. Not an ideal place for a conference. The hotel (Premier Inn) was probably the worst I've experienced at a conference so far. When we checked in they were out of twin rooms even though that was what we had ordered, so we had to share a queen size bed. There were not enough towels, no AC, breakfast not included, and only one of our key cards worked. When showering one could choose between scolding hot drizzle, freezing cold shower, or loudly howling pipes. All of this at a rate of £65 per night.
</p>
<p>
The next morning I made a new attempt at registering. This failed again, this time due to the fact that my registration had not been paid. At this point I felt rather put off by the conference, I had payed for the plane and the hotel, and was expected to pay for entrance to a conference where my talk had been the only rewarding thing. I sent an e-mail to the organizers expressing my disappointment and asking them if they wanted my money and what I should do in order to give it to them. In the response I got they said that if I was not happy then maybe I should not have proposed a talk. How was I supposed to know that I was going to be unhappy about the conference before I got there? EuroPython 2008 in Vilnius was great, I had no reason to expect it to not be good in Birmingham 2009. It took me until the afternoon before I was able to track down the organizers, pay for my registration and get my badge. I did not get a t-shirt since they had made a mess of them and were unable to find a shirt in the size I had ordered.
</p>
<p>
Now that I was finally registered and all I felt that I had earned the EuroPython dinner that night. This was a very nice addition to the conference, a purely social event with the other attendees. After dinner and a pint at a nice pub I got about four hours of sleep before I headed to the airport. My plane took off at 6:30 am, so I got to the train station at 4 am to catch a sufficiently early train to the airport only to find out that the first train of the day left at 5:30. I had to run when I arrived at the airport with only 20 minutes until final boarding call. With check in luggage I would have never made it, but since I like to travel lightly and only had a small backpack I made it to the gate just after priority boarding had finished. It still would have been much better if the flight had departed an hour or so later, because I had to wait 160 minutes for the bus after I landed in Sweden.
</p>
<p>
Despite all the problems I think it was worth my time and money to go to EuroPython, the community is great, I love the Python people! But I don't think I'll attend next year unless I get the entire conference payed for. When it's not in Birmingham anymore I'll be more interested in attending again. There were also a number of interesting talks that I will now summarize.
</p>
<dl>
<dt><a href="http://fwierzbicki.blogspot.com/">Frank Wierzbicki</a>'s talk about web frameworks and Jython</dt>
<dd>Franks talk was right after mine, in the same room. Frank has also become a much better speaker in the past year, and I enjoyed seeing all the web frameworks that we now support with Jython. And the fact stands that for normal applications Jython performs about the same as CPython, which is nice for all of these web frameworks. I tend to forget this since I spend most of my time looking at the places where Jython performance should improve.</dd>
<dt><a href="http://www.dcs.gla.ac.uk/~marks/">Mark Shannon</a>'s talk about his Python VM "HotPy"</dt>
<dd>Python is now starting to see the phenomenon that <a href="http://blog.headius.com/">Charles Nutter</a> has been <a href="http://blog.headius.com/2009/03/on-benchmarking.html">talking about</a> in the Ruby community: a number of new implementations and VMs are popping up and claiming to have better performance on a lot of things for different reasons. HotPy is a research Python VM that builds on a research VM toolkit by Mark Shannon from University of Glasgow. It is not complete but has a good approach to optimization, optimizing the code yields better result than compiling it.</dd>
<dt><a href="http://www.tismer.com/Home.html">Christian Tismer</a>'s talk about the status of Psyco</dt>
<dd>This was really impressive to me. Psyco V2 is being released this weekend. An heroic upgrading effort done by Christian alone. Everything about Psyco V2 is impressive. It yields enormous performance boosts to Python, to the point where Christian has started replacing builtin functions that are written in C in CPython with corresponding versions written in Python to improve performance. And properties get about a 100 times speedup with Psyco. I need to look at the source for Psyco and find out which of these optimization techniques we can apply to Jython, and perhaps even in the JVM.</dd>
<dt>Thursday afternoon keynote about <a href="http://www.bletchleypark.org.uk/">Bletchley Park</a></dt>
<dd><a href="http://twitter.com/Dr_Black">Sue Black</a> of the <a href="http://www.savingbletchleypark.org/">Saving Bletchley Park project</a> and Simon Greenish gave a keynote presentation about Bletchley Park and how they are using social media to attract more visitors. The presentation featured an actual working <a href="http://en.wikipedia.org/wiki/Enigma_machine">Enigma machine</a> on stage.</dd>
<dt>Thursday evening keynote by <a href="http://en.wikipedia.org/wiki/C._A._R._Hoare">Sir Charles Antony Richard Hoare</a></dt>
<dd>Tony Hoare talked about the difference between science and engineering. Not the most entertaining talk, I had hoped for some interesting and motivating story on what had been driving his career. It would for example have been great to hear how he came up with the idea for quicksort, or hear about what interesting projects he is working on now. I did like his conclusion that Computer science and Software engineering are still allowed to be imperfect, being a much younger science than for example physics or biology, both of which were just studies of simple observable phenomena when they were at the age that computer science is today. His vision was that at some point in the future software engineering will be the most reliable form of engineering, since software never degrades. I like this vision, but I agree that computer science has to mature and evolve before we can develop zero fault software. Finally his response during Q&A to the question <i>"Is there research that can help marketing come up with specifications so that we engineers can build the software?"</i> was very entertaining: <i>"That's the engineer's job. Marketing doesn't understand programming. Neither does marketing understand the customers."</i>.</dd>
</dl>
<div style="width:425px;text-align:left" id="__ss_1686090"><a style="font:14px Helvetica,Arial,Sans-serif;display:block;margin:12px 0 3px 0;text-decoration:underline;" href="http://www.slideshare.net/thobe/a-better-python-for-the-jvm-1686090" title="A Better Python for the JVM">Slideshow - A Better Python for the JVM, EuroPython 2009</a><object style="margin:0px" width="425" height="355"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=abetterjython2-090706053440-phpapp02&stripped_title=a-better-python-for-the-jvm-1686090" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=abetterjython2-090706053440-phpapp02&stripped_title=a-better-python-for-the-jvm-1686090" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"></embed></object></div>Tobiashttp://www.blogger.com/profile/15796529762063980134noreply@blogger.com1Birmingham Conservatoire52.479442 -1.90518952.466373499999996 -1.9343715000000001 52.4925105 -1.8760065tag:blogger.com,1999:blog-3940568014938333882.post-53552162345310099242009-06-28T12:41:00.000+02:002009-06-28T12:41:43.070+02:00Performance of Synchronization primitives in Jython<p>
As I pointed out in my <a href="http://journal.thobe.org/2009/06/javaone-2009-from-my-perspective.html">previous blog post about JavaOne</a> there are a few points where the performance of Jython is much worse than that of the “competitors”. In this post I provide my analysis of the the performance of Jython in the micro benchmark I used in my presentation. I will compare Jython to JRuby here for three reasons. The first reason is the obvious, Python and Ruby are quite similar languages. This takes us to the second reason, the performance of JRuby and Clojure was not that different in comparison, and it's therefore better to choose the most similar language for comparison. Finally, the third reason, JRuby is a darn good, highly optimized dynamic language implementation. That makes it a good baseline for comparison.
</p>
<p>
The first diagram shows Jython and JRuby with no contention at all. We can clearly see, as early as in this picture that Jython needs a bit more work in the performance department. I still feel excited about this though. The recent release of Jython, version 2.5, focused heavily on Python compatibility. The next versions will be where we start focusing on performance. The work towards Jython 3.0 is even more exciting here. But that excitement will be saved for a later post.
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiZg3MdaNZxgBdOEXQWh5w6Nnb4cl6vpoNSBX5_f66sKlUdlggsJG8HvC21wZg98DwsDHnqqqP8mueV_y-5lz4oSWc272NBxNpl5pFr2fAcjD55qfqbi-gIukcma_xLWZlnHJUqnGW5XSBw/s1600-h/DynaLangConcurrency-NoCounter.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiZg3MdaNZxgBdOEXQWh5w6Nnb4cl6vpoNSBX5_f66sKlUdlggsJG8HvC21wZg98DwsDHnqqqP8mueV_y-5lz4oSWc272NBxNpl5pFr2fAcjD55qfqbi-gIukcma_xLWZlnHJUqnGW5XSBw/s320/DynaLangConcurrency-NoCounter.png" /></a></div>
<p>
The second slide shows another version of pretty much the same code, but with a shared, contended resource. Here we find that Jython performs much worse than JRuby. While the JRuby version is about 10 times slower with a contended shared resource, Jython is almost 100 times slower. Why is this?
</p>
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-00xsnt8REnj-waxlp0j37cTKJ2ctgttZQaXx9OdFK6GYhu-leoxaw5CrYv7cltbH7AyD0Y17NKWz4oweCkGZVpORTy9suJbBNxDaMd3bpHW0XQPWT-rmxBCyjSG_v5Et25PctFpka6Io/s1600-h/DynaLangConcurrency-Counter.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-00xsnt8REnj-waxlp0j37cTKJ2ctgttZQaXx9OdFK6GYhu-leoxaw5CrYv7cltbH7AyD0Y17NKWz4oweCkGZVpORTy9suJbBNxDaMd3bpHW0XQPWT-rmxBCyjSG_v5Et25PctFpka6Io/s320/DynaLangConcurrency-Counter.png" /></a></div>
<h2>The effect of the implementation</h2>
<p>
I have already mentioned the first reason for JRuby performing better than Jython. It applies here as well. The JRuby team has spent more time on performance tuning than the Jython team, this is something we will be able to improve. In fact this plays a bigger part than one might think.
</p>
<p>
The JRuby Mutex implementation is written in about a page of plain old Java (not that important) that gets compiled to a small number of simple bytecodes (more important). Because a closure or block in JRuby is just an object with a method containing the compiled bytecode (compared to a function that contains argument parsing logic before that), the dispatch from the synchronization code to the guarded code is just a simple virtual call. For the call into the synchronization code there are two VM level calls, since this is a JRuby <i>method</i>. First there is the call to the dispatch logic and argument parsing, then the call to the actual code for the synchronization code. Much of the work of the first call is cached by JRubys call site cache from the first invocation so that subsequent invocations are much faster.
</p>
<p>
The Jython implementation on the other hand has no call site caching. So each call does full dispatch and argument parsing. The call structure is also different. A <a href="http://www.python.org/dev/peps/pep-0343/"><i>with statement</i></a> in Python is just a <code>try/except/finally</code> block with simplified syntax, where the <i>context manager</i> (a lock in this case) is called for setup before the block inside the <i>with statement</i> and then invoked again after the with statement block completes.
</p>
<p>
In shorter terms: JRuby has much lower call overhead on the VM level. This makes a difference because a call is a call, and even when in-lined it imposes some overhead. It is also important because the JVM is better at making optimizations across few levels of calls than across several. Still, this only explains a small part of the difference seen in the chart.
</p>
<h2>The incredible benefit of closures</h2>
<p>
Most JVMs has built in support for analyzing and optimizing locks. In order to be able to do that it needs to have the the entire synchronized region in one compilation unit, i.e. both the lock instruction and the unlock instruction needs to be in the same compilation unit. Due to the fact that code analysis is super-linear there is a limit to how big a compilation unit is allowed to be. Initially a compilation unit corresponds to a Java bytecode method, but may grow as an effect of code in-lining (up to the compilation unit size limit). The key to get good performance from synchronized regions is therefore to either have both the lock and unlock instructions in the same method, or at least in a shallow call hierarchy from the same method.
</p>
<p>
Compare the two following snippets of pseudo bytecode (indented regions are the body of the invoked methods).
</p>
<p>JRuby:</p>
<pre><code>lock()
invoke closure_body
// do stuff...
unlock()</code></pre>
<p>Jython:</p>
<pre><code>invoke __enter__
Do Jython dispatch
Do Jython to Java dispatch
Do Java reflection dispatch
lock()
// do stuff...
invoke __exit__
Do Jython dispatch
Do Jython to Java dispatch
Do Java reflection dispatch
unlock()</code></pre>
<p>
The key insight here is that in the first code snippet the lock and unlock instructions are in the same compilation unit. In the second example they are in two different call paths. The Jython dispatch logic is three levels of calls, and the Jython to Java dispatch logic is two levels, then there is the reflection dispatch that is a number of calls as well. Not only that, but there is quite a lot of code in those call paths as well: parsing of arguments, setting up call frame reflection objects, and more. Add all this together and there is no chance for the JVM to see both the lock and unlock instructions in the same compilation unit. Compared to the situation in the JRuby implementation where they are in the same compilation unit before any in-lining.
</p>
<p>
Having un-optimized locks make a huge difference for applications running on the JVM. This together with the fact that JRuby is more optimized in general, accounts for most of the difference in execution time for these examples. If we could fix this, we would get a substantial improvement of performance in Jython.
</p>
<p>
For further details on <i>how</i> we intend to improve this situation in Jython, you are very welcome to attend my presentation on "<a href="http://www.europython.eu/talks/talk_abstracts/index.html#talk65">A better Python for the JVM</a>" at EuroPython, this <a href="http://www.europython.eu/talks/timetable/#day3">Tuesday (June 30, 2009) at 15:30</a>. I will also continue posting here on specific improvements and plans, so <a href="http://twitter.com/thobe">stay tuned on Twitter</a> or subscribe to my feed.
</p>
<p>
<span style="font-style:italic; font-weight: bold; color:red;">
Disclaimers:
</span>
The understanding of a JVM in this post is mostly based on the Hotspot JVM. Other JVMs might work slightly different, but the basic understanding should be at least similar.
<br />
The descriptions of both Jython and JRuby are somewhat simplified, the synchronization in JRuby is for example even slightly better optimized than what I have outlined here, but the full description would make the post overly complicated. The essentials are still the same.
<br />
In my presentation at JavaOne some numbers suffered from classic micro benchmark errors, ask me if you want to know more about that.
</p>Tobiashttp://www.blogger.com/profile/15796529762063980134noreply@blogger.com0tag:blogger.com,1999:blog-3940568014938333882.post-58796238917484123212009-06-26T12:30:00.000+02:002009-06-26T12:30:45.592+02:00Five years later<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvon8WaS1tyGcg3TWEthZRgypFq0_BpO-XOQp3ghN3AhxZXeanYceeR-bmtkcr6jFlw2c0ubfz9QYZG1kiAlpRPSeS0yc3lVraapYWvxCrbTkxpjeUlhWAYowuGNVrY74Dm6U4LZ8uPnCV/s1600-h/Musketeer.png" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvon8WaS1tyGcg3TWEthZRgypFq0_BpO-XOQp3ghN3AhxZXeanYceeR-bmtkcr6jFlw2c0ubfz9QYZG1kiAlpRPSeS0yc3lVraapYWvxCrbTkxpjeUlhWAYowuGNVrY74Dm6U4LZ8uPnCV/s320/Musketeer.png" alt="Me with a large beard and a musketeers outfit" title="Me as a musketeer" /></a>
<p>
Five years ago I was in a committee responsible for greeting new students in <a href="http://www.d.lintek.liu.se/">computer science and engineering</a> at <a href="http://www.liu.se">my university</a>. We organized activities and parties, and made sure that the new students got their studies up to a pace that would help them through their education. We did all of this while putting on a theater act lasting throughout the entire greeting period of two weeks, for which I grew a large beard.
</p>
<p>
At about this time of the year there was a team building exercise where we were supposed to draw a picture of how we imagined our lives five years from then. I drew the view of a city skyline from a high floor hotel room. My description was that I imagined myself traveling a lot for work. That I worked on interesting, cutting edge software projects around the world. And that I made good money doing so, not really spending that much, since I didn't really see anything I needed to spend money on when most of my time would be spent working.
</p>
<p>
Three weeks ago when I looked out towards the San Francisco bay from my 25th floor hotel room at the <a href="http://www.intercontinentalsanfrancisco.com/">Intercontinental</a> where I stayed during <a href="http://journal.thobe.org/2009/06/javaone-2009-from-my-perspective.html">this years JavaOne</a>, I remembered that drawing. The view from my room was a lot like the view in the drawing. The more I thought about it, the more I realized that the vision I had five years ago was reality now.
</p>
<p>
The thing I love the most about this is of course that the part about working on cool, exiting, cutting edge projects is true. Both of the projects that get the main part of my attention fit into this category. <a href="http://neo4j.org">Neo4j</a> is an up and coming database where I have the privilege to be doing tool and integration work, as well as advanced data persistence programming. <a href="http://jython.org/">Jython</a> gives me an opportunity to work on advanced JVM technology and cutting edge techniques for optimizing code. All together a good mix of technologies, challenges and people.
</p>
<p>
One part of my vision, I'm sad to say, has yet to come true. I am not making as much money as I expected. In fact trips and conferences are rather expensive. But apart from that I spend about what I expected on other things. This is a minor detail however, I'd much rather work on projects I enjoy than be bored with a high salary.
</p>
<p>
I also expected that I would be single, since I didn't expect anyone to put up with my traveling, working and being away a lot of the time. I am very happy that I got this prediction wrong. My girlfriend Sara is wonderful, and I am blessed to be sharing my life with her.
</p>Tobiashttp://www.blogger.com/profile/15796529762063980134noreply@blogger.com3888 Howard St, San Francisco, CA 94103, USA37.7817477 -122.404611137.777507699999994 -122.4119066 37.7859877 -122.3973156tag:blogger.com,1999:blog-3940568014938333882.post-52324008057467417602009-06-25T20:57:00.002+02:002009-07-15T01:49:40.974+02:00Jython 2.5 - Why you should upgrade<p>
Last weekend I spent some time with the rest of the Neo4j team at Neo Technology for our Code Camp weekend. Among the things I worked on was our <a href="http://components.neo4j.org/neo4j.py/">Python bindings for Neo4j</a>. They obviously work with the <a href="http://fwierzbicki.blogspot.com/2009/06/jython-250-final-is-out.html">recently released Jython 2.5</a>, but also fairly well with CPython 2.5 (or later), with the current caveat that you have to install <a href="http://jpype.sourceforge.net/">JPype</a> on your own (I am working on replacing JPype with something that is maintained and more light weight, and works with Python 3.x). I also want these Python bindings to work with the previous version of Jython (2.2), for those who for some reason cannot update. So for the first time in a while I used a version of Jython that was not the very latest. This is my summary of that experience.
</p>
<p>
When comparing Jython 2.5 with Jython 2.2 it suddenly becomes obvious how much work the Jython team (including myself) have put into making this a fully compatible implementation of the Python programming language. The fact that Jython now runs Django, Pylons, SQLAlchemy, and a ton of other things is genuinely impressive. And I am more than a little proud for the part I played in making this happen, even if it was only a small part. Jython has, with the step from 2.2 to 2.5, graduated from being a system for scripting Java applications with Python to a system on which you can build full applications.
</p>
<p>
The first thing I missed when testing Neo4j with Jython 2.2 was <a href="http://pypi.python.org/pypi/virtualenv">virtualenv</a>. I run so many different unstable applications and libraries on different versions of Python and Jython that it's inevitable to mess up on a daily basis. A lot of these have different dependencies, and dependencies on conflicting versions of the same thing and other horrible constraints. And of course I patch and rebuild my Jython at the same time as well, meaning that all libraries I've installed directly into the Jython dist directory gets wiped (I've lost a lot of work this way). In all these situations virtualenv is a saving angel, and it doesn't work with Jython 2.2. (If you have not tried virtualenv yet, do so now! I was a late user, but it was love at first use.) <i>This is a pain - <b>Upgrade to Jython 2.5</b></i>.
</p>
<p>
The second thing I was screaming after in Jython < 2.5 was of course easy-install. If not for any other reason to install virtualenv. How about distutils? - Nope. Jython 2.2 has no pleasant way of installing libraries for you - you are on your own. Fail. The same is true for the Python bindings for Neo4j, if you use Jython 2.2 it will still download the required Java libraries for you, and you will be able to use it, but installing it into your <code>sys.path</code> is up to you. <i>This is a pain - <b>Upgrade to Jython 2.5</b></i>.
</p>
<p>
I lied before. The first thing I missed was the Python 2.5 syntax. In particular the with statement. This was in fact the reason I joined the Jython project in the first place, to get Python 2.5 syntax support for Jython (and I succeeded in prototyping it during that first Google Summer of Code in 2007). Just consider the following two pieces of code, and I think you will get my point:
</p>
<pre name="code" class="python">
with neo.transaction:
with_node = neo.node(language="Python", feature="with statement")
example__node = neo.node(language="Python", example="Using the with statement")
example_node.USES(with_node)
</pre>
<pre name="code" class="python">
tx = neo.transaction.begin()
try:
try:
try_node = neo.node(language="Python", feature="try block")
example__node = neo.node(language="Python", example="Not using the with statement")
example_node.USES(try_node)
except:
tx.failure()
raise
else:
tx.success()
finally:
tx.finish()
</pre>
<p>
It should be obvious which one is beautiful and which one is not. The with statement is one nice piece of syntactic sugar. <i>Not having it is a pain - <b>Upgrade to Jython 2.5</b></i>.
</p>
<p>
Ok, so there is still a lot of work to be done on Jython. Mainly on performance. The focus up until now has been compatibility, and we've done a good job at that. In the next major version of Jython you should expect a substantial improvement of performance. Not that performance is bad now. But we know that we can do better. More on that in a later post.
</p>Tobiashttp://www.blogger.com/profile/15796529762063980134noreply@blogger.com1Hans Michelsensgatan, 21120 Malmo, Sweden55.6121327 13.001698555.6060732 12.9871075 55.618192199999996 13.0162895tag:blogger.com,1999:blog-3940568014938333882.post-10160556801636331012009-06-18T22:42:00.000+02:002009-06-18T22:42:39.845+02:00JavaOne 2009 from my perspective<p>It has been a week since I got back to Sweden after my trip to San Francisco for what I am sad to say was my worst JavaOne so far. Don't get me wrong, JavaOne was good. There were a lot of good sessions, and as always it was nice to meet my friends in the community. But the two previous years were better. Much of it can be blamed on the economy. And the rest can be blamed on me. To a large extent the reason I didn't get the JavaOne experience I would have liked to was because I was too busy. I had a deadline waiting for me on a project at work when I got home, and had to work quite a lot during my JavaOne week. But the fact stands, JavaOne was a lot smaller this year, only about 9000 attendees, and you notice that.</p>
<p>These are the highlight from the sessions I attended at CommunityOne and then JavaOne:</p><ul>
<li><b>Beyond impossible: How JRuby has evolved the Java Platform</b> by <a href="http://blog.headius.com/">Charles Nutter</a>. Charles is a kickass developer and has become a really good speaker as well. It's amazing what he has done with JRuby, and this talk was a summary of the highlights from that. Including things such as the fact that the JRuby team have implemented their own regular expressions library and posix layer for doing system calls from the JVM. All of which adds up to making JRuby the fastest complete implementation of Ruby.</li>
<li><b>Return of the Puzzlers: Schlock and Awe</b> by Joshua Bloch and Neal Gafter. All I can say is that all the digging around on the low levels of the JVM that I have done has payed off, I had the right answer for 7 out of 7 Puzzlers, much better than the first time I attended a Java Puzzlers session.</li>
<li><b>Toward a Renaissance VM</b> by <a href="http://blogs.sun.com/jrose/">John Rose</a>. John talked about Invokedynamic and Method Handles. It was all familiar stuff, but a good introduction for the odd people who do not spend most of their time implementing languages for the JVM.</li>
<li><b>Meet the Java Posse</b>. I've known the guys of the Java Posse for a while, but this is the first time that I was not doing something completely different during their BoF at JavaOne. Not much to say, <a href="http://javaposse.com/">listen to the podcast</a>.</li>
<li><b>JSR 292 Cookbook</b> mostly by John Rose. This is what I think was the eye opening session about Invokedynamic and MethodHandles for most people. Hands on examples, showing all of the JSR 292 goodness in action. More people should have attended. Everyone not familiar with JSR 292 on beforehand that I talked to after the session realized that it is useful for far more things than just implementing languages.</li>
<li><b>How to write a distributed garbage collector</b> by Ari Zilka and Saravanan Subbiah from <a href="http://www.terracotta.org/">Terracotta</a>. This was the last session of the last day and probably <i>the best session at JavaOne</i> this year. Ari and Saravanan talked about how garbage is detected and collected in the distributed environment that is Terracotta when all references to an object could be from a different node. Very interesting stuff, I need to take a deeper look at Terracotta at some point soon.</li>
</ul>
<p>There were also some noteworthy sessions that I missed while I was working:</p>
<ul>
<li><b>Java Platform Concurrency Gotchas</b> by <a href="http://tech.puredanger.com">Alex Miller</a>. A smart guy talking about interesting stuff, too bad I missed that one.</li>
<li><b>The Ghost in the Virtual Machine: A Reference to References</b> by <a href="http://crazybob.org/">Bob Lee</a>. I have a feeling that there are still a few things that can be done using Weak, Soft and Phantom references, that I don't know about yet. I would also have liked to hear what he had to say about <a href="http://en.wikipedia.org/wiki/Ephemeron">Ephemerons</a>. Are they coming to the JVM soon btw?</li>
</ul>
<p>Then there are the two presentations I gave. A BoF on Interface Injection entitled "Language Interoperability on the JVM Made Simple" and a Technical session entitled "Exploiting Concurrency with Dynamic Languages".</p>
<h2>Language Interoperability on the JVM Made Simple</h2>
<p>Since this was a Birds of a Feather session I divided it into two main sections: The first half I gave an introduction to interface injection, the status of the implementation and some ideas as to what you can do with it. The second half of the session was spent on discussions about what people want from interface injection, how they want to use it and how it should work.</p>
<p>The short summary is:</p>
<ul>
<li>I have implemented the injection mechanism.</li>
<li>I have designed a proposal API for how the injection process should work on the Java level.</li>
<li>I have wired up interface injection to invokeinterface, there are still a few more places that would need to be able to call the injection mechanism.</li>
<li>The Reflection API for injected interfaces is still unspecified.</li>
<li>A lot of people want only explicit injection of interfaces. That is for interfaces to only be injected when an inject() method is invoked in some API. This can be emulated by setting a flag in the injector when this method is invoked, and unset it at the end of this method.</li>
<li>There was also some concern about the fact that methods on an injected interface gets implemented automatically by methods with the same name and signature that exist in the target class. I agree that it would be better if interface implementations could be separate for each interface in the class file format. But I don't think this problem will be as big as people might fear since the methods have to match <i>exactly</i> on return type as well as parameter types.</li>
</ul>
<p>The staff in room 102/103 was really helpful, they offered to rearrange the chairs and tables to better host the discussion I wanted to encourage about interface injection. This can not be said about the staff in room 104 where I had my second presentation. Although being very friendly, they were not very professional. First of all my session was supposed to be recorded, but after the session I was informed that they had forgotten to record the slides, and transitions. I gave them my slides on a memory stick and they said that they were going to match it to the best of their ability then send it to me for review, I have yet to hear from them. On top of that some members of the staff were constantly talking behind the stage, leaving me very distracted. Highly unprofessional of them. I was so surprised by this that I didn't even tell them to shut up.</p>
<h2>Exploiting Concurrency with Dynamic Languages</h2>
<p>I spent too much time at JavaOne preparing for my second talk about how languages other than the Java Programming Language on the JVM lend themselves better to expressing concurrent programs. I really should have prepared the talk much more <i>before</i> I left Sweden. I believe that I could have done a better presentation. The staff in the room is to blame to a large extent for the fact that I didn't feel satisfied by my performance, but half of the blame is on me. Although, the few reviews that I've heard have been good. Basically that the topic was good, that I had interesting ideas and made good points, but that it was too short, although with good Q&A. A fair review in my opinion, it will be interesting to see the review cards when they get sent out to the speakers.</p>
<p>What is more interesting however is what I was able to conclude in my talk. It should come as a surprise to no one that closures make it much easier to express concurrent programs, and to encapsulate best practices and patterns in libraries. Other higher level constructs are helpful here as well, please have a look at <a href="http://www.slideshare.net/thobe/exploiting-concurrency-with-dynamic-languages">my slides</a> for reference. I also found that Jython has a lot of room for improvement, but more on that in another blog post.</p>
<div style="width:425px;text-align:left" id="__ss_1605269"><object style="margin:0px" width="425" height="355"><param name="movie" value="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=dynalangconcurrency-090618152804-phpapp01&stripped_title=exploiting-concurrency-with-dynamic-languages" /><param name="allowFullScreen" value="true"/><param name="allowScriptAccess" value="always"/><embed src="http://static.slidesharecdn.com/swf/ssplayer2.swf?doc=dynalangconcurrency-090618152804-phpapp01&stripped_title=exploiting-concurrency-with-dynamic-languages" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="355"></embed></object></div>Tobiashttp://www.blogger.com/profile/15796529762063980134noreply@blogger.com0tag:blogger.com,1999:blog-3940568014938333882.post-44000679480835439982009-05-14T09:19:00.003+02:002009-05-14T11:05:02.701+02:00Twitter: #dontfixreplies<p>
Recently <a href="http://twitter.com/">Twitter</a> changed their policy for which messages to show in the regular stream of messages to each user. Previously users could choose if they wanted to see @replies (messages that start with an @ sign followed by a user name) or not. With the recent change this is no longer a configuration option, but all users now <span style="font-style: italic;">don't</span> see @replies directed to people they don't follow. The motivation for this is of course that these messages really are directed at someone else, that you are not interested in.</p>
<p>
A lot of people <a href="http://twitter.com/#search?q=%23fixreplies">got upset with this</a>, with the main motivation being that @replies is a good way to find interesting connections and people.</p>
<p>
I did not notice the change until everyone (yes, everyone) started ranting about wanting it to change back to the old behavior, since I had turned off @replies a long time ago. <span style="font-style: italic;">Why did I turn it off?</span> Because a lot of people use Twitter as <span style="font-style: italic;">yet another IM service</span>, and those messages have a very low <a href="http://en.wikipedia.org/wiki/Signal-to-noise_ratio">signal to noise ratio</a> to me. Before I turned off @replies I got about 3 times as many tweets in my stream, and more than 90% of these were not interesting to me. Even with my current stream without @replies there is more noise than signal, but the ratio is such that I <span style="font-style: italic;">can find</span> the tweets I'm interested in.</p>
<p>
One interesting thing that I noticed with the recent change was that peoples twittering mentality changed (This applies only to the people I follow, a tech heavy crowd. I have not done, nor do I intend to do, forther investigation of the phenomenon). All of the sudden the signal to noise ratio got much better. I was away from twitter for about a day, a little over 20 hours. When I got back I had less than 100 new tweets in my stream. Normally 20 hours yield more than 120 tweets. There were also more interesting links than usual, I have 13 tabs open with things I want to read. I.e. almost 15% of the tweets contained links that looked interesting, compared to the usual 5%. A lot of the other tweets were interesting to, I estimate the noise ratio to be about 30% for this period of time. Interestingly the tweets that fall into the noise category are mostly either rants about how bad it is to not get @replies or from the past few hours when people started add stuff before their @replies to force them to be visible.</p>
<p>
Conclusion: Twitter, please keep the current behavior for @replies. Or if it changes back, make the current behavior default (I'm not unreasonable).</p>
<p style="color: rgb(153, 0, 0);">
<span style="font-weight: bold;">Disclaimer:</span> I haven't done the actual statistical calculations for this. It would be interesting to do, and post a few charts, but there are a lot of interesting things to do, and I don't have time to do all of them. Therefore the figures in this post are just from the general feeling I get from my Twitter stream.</p>Anonymousnoreply@blogger.com1tag:blogger.com,1999:blog-3940568014938333882.post-91119476328240784262009-04-22T17:35:00.005+02:002009-04-22T22:48:26.530+02:00Python-style generators in Scheme<p>I have finally gotten back to working on interface injection in OpenJDK. As before I get a few breaks when I wait for stuff to compile. These breaks are too long to just sit around and wait, but too short to context switch to another project. So what I end up doing instead are small hacks. And since I, once again, have decided to try and post stuff here more regularly, I decided to write a post about a hack I did yesterday.</p><p>A long time ago I tried to implement Python style generators in Scheme, and obviously I am a better programmer now since I was able to just sit down and write it.</p><p>The aim is to be able to write code like this:</p><pre><code>(<span style="color: rgb(0, 0, 153);">define-generator</span> (<span style="color: rgb(0, 102, 0);">iterate</span> lst)<br /> (<span style="color: rgb(0, 0, 153);">define</span> (<span style="color: rgb(0, 102, 0);">iter</span> lst)<br /> (<span style="color: rgb(0, 0, 153);">if</span> (not (null? lst))<br /> (<span style="color: rgb(0, 0, 153);">begin</span> (<span style="color: rgb(0, 0, 153);">yield</span> (car lst))<br /> (iter (cdr lst)))))<br /> (<span style="color: rgb(0, 102, 0);">iter</span> lst))</code></pre><p>And then use such a generator in a Python-like for-loop, like so:</p><pre><code>(<span style="color: rgb(0, 0, 153);">for</span> (element (iterate '(a b c))<br /> (<span style="color: rgb(0, 0, 153);">print</span> element))</code></pre><p>The code that enables this is at it's core two syntax definitions. The generator definition syntax, and the for-loop syntax:</p><pre><code>(<span style="color: rgb(0, 0, 153);">define-syntax</span> <span style="color: rgb(0, 102, 0);">explicit-generator-definition</span><br /> (<span style="color: rgb(0, 0, 153);">syntax-rules</span> (<span style="color: rgb(0, 102, 0);">yield</span>)<br /> ((<span style="color: rgb(0, 102, 0);">explicit-generator-definition</span> (<span style="color: rgb(0, 102, 0);">yield</span> exit) params . body)<br /> (<span style="color: rgb(0, 0, 153);">lambda</span> params<br /> (<span style="color: rgb(0, 0, 153);">lambda</span> (done loop)<br /> (<span style="color: rgb(0, 0, 153);">define</span> (exit . values)<br /> (<span style="color: rgb(0, 0, 153);">call/cc</span> (<span style="color: rgb(0, 0, 153);">lambda</span> (next) (<span style="color: rgb(0, 0, 153);">apply</span> loop (<span style="color: rgb(0, 0, 153);">cons</span> next values)))))<br /> . body)))))<br /><br />(<span style="color: rgb(0, 0, 153);">define-syntax</span> <span style="color: rgb(0, 102, 0);">for</span><br /> (<span style="color: rgb(0, 0, 153);">syntax-rules</span> (<span style="color: rgb(0, 102, 0);">break continue</span>)<br /> <span style="color: rgb(204, 51, 204);">; multiple valued generator version</span><br /> ((<span style="color: rgb(0, 102, 0);">for</span> (<span style="color: rgb(0, 102, 0);">break</span> breaker) (<span style="color: rgb(0, 102, 0);">continue</span> continuation)<br /> ((variables ...) generator) . body)<br /> (<span style="color: rgb(0, 0, 153);">call/cc</span><br /> (<span style="color: rgb(0, 0, 153);">lambda</span> (breaker) (generator breaker<br /> (<span style="color: rgb(0, 0, 153);">lambda</span> (continuation variables ...)<br /> . body)))))<br /> <span style="color: rgb(204, 51, 204);">; single valued generator version</span><br /> ((<span style="color: rgb(0, 102, 0);">for</span> (<span style="color: rgb(0, 102, 0);">break</span> breaker) (<span style="color: rgb(0, 102, 0);">continue</span> continuation)<br /> (variable generator) . body)<br /> (<span style="color: rgb(0, 0, 153);">call/cc</span><br /> (<span style="color: rgb(0, 0, 153);">lambda</span> (breaker) (generator breaker<br /> (<span style="color: rgb(0, 0, 153);">lambda</span> (continuation variable)<br /> . body)))))<br /> <span style="color: rgb(204, 51, 204);">; enable ignoring break and continue</span><br /> ((<span style="color: rgb(0, 102, 0);">for</span> (<span style="color: rgb(0, 102, 0);">continue</span> continuation) (<span style="color: rgb(0, 102, 0);">break</span> breaker) (target ...) . body)<br /> (<span style="color: rgb(0, 102, 0);">for</span> (<span style="color: rgb(0, 102, 0);">break</span> breaker) (<span style="color: rgb(0, 102, 0);">continue</span> continuation) (target ...) . body))<br /> ((<span style="color: rgb(0, 102, 0);">for</span> (<span style="color: rgb(0, 102, 0);">break</span> breaker) (targets ...) . body)<br /> (<span style="color: rgb(0, 102, 0);">for</span> (<span style="color: rgb(0, 102, 0);">break</span> breaker) (<span style="color: rgb(0, 102, 0);">continue</span> ignored) (targets ...) . body))<br /> ((<span style="color: rgb(0, 102, 0);">for</span> (<span style="color: rgb(0, 102, 0);">continue</span> continuation) (targets ...) . body)<br /> (<span style="color: rgb(0, 102, 0);">for</span> (<span style="color: rgb(0, 102, 0);">break</span> ignored) (<span style="color: rgb(0, 102, 0);">continue</span> continuation) (targets ...) . body))<br /> ((<span style="color: rgb(0, 102, 0);">for</span> (<span style="color: rgb(0, 102, 0);">targets</span> ...) . body)<br /> (<span style="color: rgb(0, 102, 0);">for</span> (<span style="color: rgb(0, 102, 0);">break</span> _break) (<span style="color: rgb(0, 102, 0);">continue</span> _continue) (targets ...) . body))))</code></pre><p>As you can see the loop construct has the ability to break the loop and jump to the next iteration by explicitly naming the break and continue continuations. The fact that these have to be named explicitly is a good thing since it enables you to define different names for the break/continue continuations for different loops when you nest loops. You can of course name them break and continue, respectively, like so:</p><pre><code>(<span style="color: rgb(0, 0, 153);">for</span> (<span style="color: rgb(0, 0, 153);">break</span> break) (<span style="color: rgb(0, 0, 153);">continue</span> continue) (element (iterate '(a b c))<br /> (<span style="color: rgb(0, 0, 153);">print</span> element))</code></pre><p>The generator definition also needs the yield continuation to be named explicitly. This is usually not what you want, so instead we define a convenience syntax for defining generators in the form of a "e;unhygienic"e; macro (this is the syntax used for unhygienic macros by most scheme implementations, PLT scheme that I used for example):</p><pre><code>(<span style="color: rgb(0, 0, 153);">define-macro</span> (<span style="color: rgb(0, 102, 0);">generator</span> arguments . body)<br /> `(explicit-generator-definition (<span style="color: rgb(0, 0, 153);">yield</span> <span style="color: rgb(0, 102, 0);">yield</span>) ,arguments ,@body))<br /><br />(<span style="color: rgb(0, 0, 153);">define-macro</span> (<span style="color: rgb(0, 102, 0);">define-generator</span> signature . body)<br /> `(define ,(<span style="color: rgb(0, 0, 153);">car</span> signature)<br /> (explicit-generator-definition (<span style="color: rgb(0, 0, 153);">yield</span> <span style="color: rgb(0, 102, 0);">yield</span>) ,(<span style="color: rgb(0, 0, 153);">cdr</span> signature) ,@body)))</code></pre><p>These macros was what prevented me from completing this when I first tried a few years ago. I started with them, and wanted them to be portable across all Scheme implementations, and unhygienic macros are not defined in the R5RS standard. What I should have done is what I did now, start with the interesting stuff and add just a little implementation dependent code for the final touches.</p><p>Now we can use these macros to define a range function that behaves in about the same way as the xrange function in Python:</p><pre><code>(<span style="color: rgb(0, 0, 153);">define-generator</span> (<span style="color: rgb(0, 102, 0);">range</span> . params)<br />(<span style="color: rgb(0, 0, 153);">define</span> (<span style="color: rgb(0, 102, 0);">range</span> start stop step)<br /> (<span style="color: rgb(0, 0, 153);">if</span> (and (> start stop) (> step 0))<br /> (<span style="color: rgb(0, 0, 153);">values</span> start (<span style="color: rgb(0, 0, 153);">lambda</span> (current) (<= current stop)) (- step))<br /> (<span style="color: rgb(0, 0, 153);">values</span> start (lambda (current) (>= current stop)) step)))<br />(<span style="color: rgb(0, 0, 153);">call-with-values</span><br /> <span style="color: rgb(204, 51, 204);">; parse the variables based on the number of them</span><br /> (<span style="color: rgb(0, 0, 153);">lambda</span> () (<span style="color: rgb(0, 0, 153);">cond</span><br /> <span style="color: rgb(204, 51, 204);">; 0 arguments -></span><br /> ((null? params)<br /> <span style="color: rgb(204, 51, 204);">; infinite generator from 0</span><br /> (values 0 (lambda (current) #f) 1))<br /> <span style="color: rgb(204, 51, 204);">; 1 argument: count -></span><br /> ((null? (cdr params))<br /> <span style="color: rgb(204, 51, 204);">; generator with count elements, starting at 0</span><br /> (range 0 (car params) 1))<br /> <span style="color: rgb(204, 51, 204);">; 2 arguments: start and stop -></span><br /> ((null? (cddr params))<br /> <span style="color: rgb(204, 51, 204);">; generator over [start, stop[</span><br /> (range (car params) (cadr params) 1))<br /> <span style="color: rgb(204, 51, 204);">; 3 arguments: start, stop and step -></span><br /> ((null? (cdddr params))<br /> <span style="color: rgb(204, 51, 204);">; generator over [start, stop[ with a given step length</span><br /> (range (car params) (cadr params) (caddr params)))<br /> <span style="color: rgb(204, 51, 204);">; more arguments - error (by passing too few arguments to internal range</span><br /> (#t (values))))<br /> (<span style="color: rgb(0, 0, 153);">lambda</span> (start stop? step)<br /> (<span style="color: rgb(0, 0, 153);">do</span> ((current start (+ current step)))<br /> ((stop? current))<br /> (<span style="color: rgb(0, 0, 153);">yield</span> current)))))</code></pre><p>As a final touch we add a convenience syntax for nested for-loops as well:</p><pre><code>(<span style="color: rgb(0, 0, 153);">define-syntax</span> <span style="color: rgb(0, 102, 0);">for*</span><br />(<span style="color: rgb(0, 0, 153);">syntax-rules</span> (<span style="color: rgb(0, 102, 0);">break continue</span>)<br /> <span style="color: rgb(204, 51, 204);">; base case - one generator - expands to single for</span><br /> ((<span style="color: rgb(0, 102, 0);">for*</span> (((<span style="color: rgb(0, 102, 0);">break</span> breaker) (<span style="color: rgb(0, 102, 0);">continue</span> continuation) targets ...)) . body)<br /> (<span style="color: rgb(0, 0, 153);">for</span> (<span style="color: rgb(0, 102, 0);">break</span> breaker) (<span style="color: rgb(0, 102, 0);">continue</span> continuation) (targets ...) . body))<br /> ((<span style="color: rgb(0, 102, 0);">for*</span> (((<span style="color: rgb(0, 102, 0);">continue</span> continuation) (<span style="color: rgb(0, 102, 0);">break</span> breaker) targets ...)) . body)<br /> (<span style="color: rgb(0, 0, 153);">for</span> (<span style="color: rgb(0, 102, 0);">break</span> breaker) (<span style="color: rgb(0, 102, 0);">continue</span> continuation) (targets ...) . body))<br /> ((<span style="color: rgb(0, 102, 0);">for*</span> (((<span style="color: rgb(0, 102, 0);">break</span> breaker) targets ...)) . body)<br /> (<span style="color: rgb(0, 0, 153);">for</span> (<span style="color: rgb(0, 102, 0);">break</span> breaker) (targets ...) . body))<br /> ((<span style="color: rgb(0, 102, 0);">for*</span> (((<span style="color: rgb(0, 102, 0);">continue</span> continuation) targets ...)) . body)<br /> (<span style="color: rgb(0, 0, 153);">for</span> (<span style="color: rgb(0, 102, 0);">continue</span> continuation) (targets ...) . body))<br /> ((<span style="color: rgb(0, 102, 0);">for*</span> ((targets ...)) . body)<br /> (<span style="color: rgb(0, 0, 153);">for</span> (targets ...) . body))<br /> <span style="color: rgb(204, 51, 204);">; recursive case - more than one generator - expands to nested for*</span><br /> <span style="color: rgb(204, 51, 204);">; for* is used instead of for to avoid repeating break and continue cases</span><br /> ((<span style="color: rgb(0, 102, 0);">for*</span> (first more ...) . body)<br /> (<span style="color: rgb(0, 102, 0);">for*</span> (first) (<span style="color: rgb(0, 102, 0);">for*</span> (more ...) . body)))))</code></pre><p>To understand how the generators work let's look at what a generator definition and for-loop construct expands to:</p><pre><code><span style="color: rgb(204, 51, 204);">; A generator expands to a function that accepts the defined parameters</span><br />(<span style="color: rgb(0, 0, 153);">lambda</span> params<br /> <span style="color: rgb(204, 51, 204);">; The generator function, when called, returns a function to which</span><br /> <span style="color: rgb(204, 51, 204);">; the for-construct passes the break continuation and a closure</span><br /> <span style="color: rgb(204, 51, 204);">; representing the body of the loop</span><br /> (<span style="color: rgb(0, 0, 153);">lambda</span> (done loop)<br /> <span style="color: rgb(204, 51, 204);">; yield is defined as a function that takes arbitrary parameters</span><br /> (<span style="color: rgb(0, 0, 153);">define</span> (exit . values)<br /> <span style="color: rgb(204, 51, 204);">; when invoked it retrieves the current continuation to be</span><br /> <span style="color: rgb(204, 51, 204);">; able to resume then calls the loop body with the continuation</span><br /> <span style="color: rgb(204, 51, 204);">; and the passed in parameters as arguments</span><br /> (<span style="color: rgb(0, 0, 153);">call/cc</span> (<span style="color: rgb(0, 0, 153);">lambda</span> (next) (<span style="color: rgb(0, 0, 153);">apply</span> loop (<span style="color: rgb(0, 0, 153);">cons</span> next values)))))<br /> <span style="color: rgb(204, 51, 204);">; The actual body of the generator is defined by the user,</span><br /> <span style="color: rgb(204, 51, 204);">; this executes arbitrary code and calls yield.</span><br /> . body))<br /><br /><span style="color: rgb(204, 51, 204);">; The for-construct starts by retrieving the current conitunuation</span><br /><span style="color: rgb(204, 51, 204);">; this is used for breaking the loop</span><br />(<span style="color: rgb(0, 0, 153);">call/cc</span><br /> <span style="color: rgb(204, 51, 204);">; It then calls the generator with two arguments:</span><br /> <span style="color: rgb(204, 51, 204);">; the break continuation and the body of the loop.</span><br /> (<span style="color: rgb(0, 0, 153);">lambda</span> (breaker) (generator breaker<br /> <span style="color: rgb(204, 51, 204);">; The body of the loop accepts two arguments:</span><br /> <span style="color: rgb(204, 51, 204);">; the continue continuation and the argument that was passed to yield.</span><br /> (<span style="color: rgb(0, 0, 153);">lambda</span> (continuation variable)<br /> <span style="color: rgb(204, 51, 204);">; All it does is execute the body</span><br /> . body))))</code></pre><p>Happy Hacking!</p>Tobiashttp://www.blogger.com/profile/15796529762063980134noreply@blogger.com1tag:blogger.com,1999:blog-3940568014938333882.post-75687160310313875382008-12-22T13:15:00.003+01:002008-12-22T15:01:55.765+01:00On Iterator composition in Java<p>In <a href="http://journal.thobe.org/2008/12/on-double-checked-locking-in-java.html">my last blog post</a> I mentioned that I would like to see library support for manipulating iterators included in the JDK. I for one, and I know that more people than me, have a set of iterator manipulation classes that I bring along from project to project, usually by copy/paste. This post is an elaboration on what the things are that I think should be included in the JDK, all communicated through code (The actual implementation is not as interesting as the API, and thus left as an exercise for the reader):</p><code><pre><span style="color: rgb(0, 0, 153); font-weight: bold;">package</span> <span style="color: rgb(0, 102, 0);">java.util</span>;<br /><br /><span style="color: rgb(0, 0, 153); font-weight: bold;">import</span> <span style="color: rgb(0, 102, 0);">java.util.iter.*</span>;<br /><br /><span style="color: rgb(0, 0, 153); font-weight: bold;">public final class</span> <span style="color: rgb(0, 102, 0);">Iterators</span> {<br /> <span style="color: rgb(0, 0, 153); font-weight: bold;">public</span> <<span style="color: rgb(0, 102, 0);">E</span>> <span style="color: rgb(0, 102, 0);">Iterable</span><<span style="color: rgb(0, 102, 0);">E</span>> empty() { ... }<br /> <span style="color: rgb(0, 0, 153); font-weight: bold;">public</span> <<span style="color: rgb(0, 102, 0);">S</span>,<span style="color: rgb(0, 102, 0);">T</span>> <span style="color: rgb(0, 102, 0);">Iterable</span><<span style="color: rgb(0, 102, 0);">T</span>> convert(<span style="color: rgb(0, 102, 0);">Iterable</span><<span style="color: rgb(0, 102, 0);">S</span>> iterable, <span style="color: rgb(0, 102, 0);">Converter</span><<span style="color: rgb(0, 102, 0);">S</span>,<span style="color: rgb(0, 102, 0);">T</span>> converter) { ... }<br /> <span style="color: rgb(0, 0, 153); font-weight: bold;">public</span> <<span style="color: rgb(0, 102, 0);">T</span>> <span style="color: rgb(0, 102, 0);">Iterable</span><<span style="color: rgb(0, 102, 0);">T</span>> upCast(<span style="color: rgb(0, 102, 0);">Iterable</span><<span style="color: rgb(0, 102, 0);">?</span> <span style="font-weight: bold; color: rgb(0, 0, 153);">extends</span> <span style="color: rgb(0, 102, 0);">T</span>> iterable) { ... }<br /> <span style="color: rgb(0, 0, 153); font-weight: bold;">public</span> <<span style="color: rgb(0, 102, 0);">T</span>> <span style="color: rgb(0, 102, 0);">Iterable</span><<span style="color: rgb(0, 102, 0);">T</span>> downCast(<span style="color: rgb(0, 102, 0);">Class</span><<span style="color: rgb(0, 102, 0);">T</span>> target, <span style="color: rgb(0, 102, 0);">Iterable</span><<span style="color: rgb(0, 102, 0);">?</span>> iterable) { ... }<br /> <span style="color: rgb(0, 0, 153); font-weight: bold;">public</span> <<span style="color: rgb(0, 102, 0);">T</span>> <span style="color: rgb(0, 102, 0);">Iterable</span><<span style="color: rgb(0, 102, 0);">T</span>> append(<span style="color: rgb(0, 102, 0);">Iterable</span><<span style="color: rgb(0, 102, 0);">T</span>>... iterables) { ... }<br />}<br /><br /><span style="color: rgb(204, 51, 204);">// These classes should preferably be reused from somewhere.</span><br /><span style="font-weight: bold; color: rgb(0, 0, 153);">package</span> <span style="color: rgb(0, 102, 0);">java.util.iter</span>;<br /><br /><span style="color: rgb(0, 0, 153); font-weight: bold;">public interface</span> <span style="color: rgb(0, 102, 0);">Converter</span><<span style="color: rgb(0, 102, 0);">S</span>,<span style="color: rgb(0, 102, 0);">T</span>> {<br /> <span style="color: rgb(0, 102, 0);">T</span> convert(<span style="color: rgb(0, 102, 0);">S</span> source);<br />}<br /><br /><span style="color: rgb(0, 0, 153); font-weight: bold;">public interface</span> <span style="color: rgb(0, 102, 0);">Filter</span><<span style="color: rgb(0, 102, 0);">E</span>> {<br /> <span style="color: rgb(0, 102, 0);">boolean</span> accept(<span style="color: rgb(0, 102, 0);">E</span> element);<br />}</pre></code><p>And some sample uses:</p><code><pre><span style="font-weight: bold; color: rgb(0, 0, 153);">package</span> <span style="color: rgb(0, 102, 0);">org.thobe.example</span>;<br /><br /><span style="color: rgb(0, 0, 153); font-weight: bold;">import</span> <span style="color: rgb(0, 102, 0);">java.util.Iterators</span>;<br /><span style="font-weight: bold; color: rgb(0, 0, 153);">import</span> <span style="color: rgb(0, 102, 0);">java.util.iter.*</span>;<br /><br /><span style="font-weight: bold; color: rgb(0, 0, 153);">class</span> <span style="color: rgb(0, 102, 0);">UsesIteratorComposition</span> {<br /> <span style="font-weight: bold; color: rgb(0, 0, 153);">private class</span> <span style="color: rgb(0, 102, 0);">Something</span> {}<br /> <span style="color: rgb(0, 0, 153); font-weight: bold;">private class</span> <span style="color: rgb(0, 102, 0);">Source</span> <span style="font-weight: bold; color: rgb(0, 0, 153);">extends</span> <span style="color: rgb(0, 102, 0);">Something</span> {}<br /> <span style="font-weight: bold; color: rgb(0, 0, 153);">private class</span> <span style="color: rgb(0, 102, 0);">Target</span> { Target(<span style="color: rgb(0, 102, 0);">Source</span> s) {} }<br /><br /> <span style="color: rgb(0, 102, 0);">Iterable</span><<span style="color: rgb(0, 102, 0);">Source</span>> source = <span style="color: rgb(0, 102, 0);">Iterators</span>.empty();<br /><br /> <span style="color: rgb(0, 102, 0);">void</span> conversions() {<br /> <span style="color: rgb(0, 102, 0);">Iterable</span><<span style="color: rgb(0, 102, 0);">Target</span>> target = <span style="color: rgb(0, 102, 0);">Iterators</span>.convert(source, <span style="font-weight: bold; color: rgb(0, 0, 153);">new</span> <span style="color: rgb(0, 102, 0);">Converter</span><<span style="color: rgb(0, 102, 0);">Source</span>,<span style="color: rgb(0, 102, 0);">Target</span>>() {<br /> <span style="color: rgb(0, 102, 0);">Target</span> convert(<span style="color: rgb(0, 102, 0);">Source</span> source) {<br /> <span style="font-weight: bold; color: rgb(0, 0, 153);">return new</span> <span style="color: rgb(0, 102, 0);">Target</span>(source);<br /> }<br /> });<br /> }<br /><br /> <span style="color: rgb(0, 102, 0);">void</span> casts() {<br /> <span style="color: rgb(0, 102, 0);">Iterable</span><<span style="color: rgb(0, 102, 0);">Something</span>> something = <span style="color: rgb(0, 102, 0);">Iterators</span>.upCast(source);<br /> <span style="color: rgb(0, 102, 0);">Iterable</span><<span style="color: rgb(0, 102, 0);">Source</span>> back = <span style="color: rgb(0, 102, 0);">Iterators</span>.downCast(<span style="color: rgb(0, 102, 0);">Source</span>.<span style="color: rgb(0, 0, 153); font-weight: bold;">class</span>, something);<br /> }<br /><br /> <span style="color: rgb(0, 102, 0);">void</span> append() {<br /> <span style="color: rgb(0, 102, 0);">Iterable</span><<span style="color: rgb(0, 102, 0);">Source</span>> source = <span style="color: rgb(0, 102, 0);">Iterators</span>.append(source, <span style="color: rgb(0, 0, 153); font-weight: bold;">new</span> <span style="color: rgb(0, 102, 0);">ArrayList</span><<span style="color: rgb(0, 102, 0);">Source</span>>(){<br /> {<br /> add(<span style="color: rgb(0, 0, 153); font-weight: bold;">new</span> <span style="color: rgb(0, 102, 0);">Source</span>());<br /> add(<span style="font-weight: bold; color: rgb(0, 0, 153);">new</span> <span style="color: rgb(0, 102, 0);">Source</span>());<br /> }<br /> });<br /> }<br />}</pre></code>Tobiashttp://www.blogger.com/profile/15796529762063980134noreply@blogger.com4tag:blogger.com,1999:blog-3940568014938333882.post-4505598760941168162008-12-17T13:16:00.004+01:002008-12-17T13:56:29.233+01:00On Double Checked Locking in Java<p>You have probably all heard/seen <a href="http://www.cs.umd.edu/%7Epugh/java/memoryModel/DoubleCheckedLocking.html">the “Double-Checked Locking is Broken” declaration</a>. And if you haven't I'll just tell you that before the improved memory model of Java 5, double checked locking didn't work as expected. Even in Java 5 and later there are things you need to consider to get it to work properly, and even more things to consider if you want it to perform well.</p><p>Joshua Bloch suggests in his book Effective Java, and keeps repeating in several presentations that there is one way to implement double checked locking and that this code snippet should be copied to every place where it's needed. I disagree. Not with the part about there only being one way of doing it, but with the copying part. In my eyes this is perfect use case for an abstract class:</p><code></code><pre><span style="color: rgb(0, 0, 153); font-weight: bold;">package</span> <span style="color: rgb(0, 102, 0);">java.util.concurrent</span>;<br /><br /><span style="font-weight: bold; color: rgb(0, 0, 153);">public abstract class</span> <span style="color: rgb(0, 102, 0);">LazyLoaded</span><<span style="color: rgb(0, 102, 0);">T</span>> {<br /> <span style="color: rgb(0, 0, 153); font-weight: bold;">private</span><span style="font-weight: bold;"> </span><span style="color: rgb(255, 0, 0); font-weight: bold;">volatile</span> <span style="color: rgb(0, 102, 0);">T</span> value;<br /> <span style="color: rgb(204, 102, 204);">// Get the value, guard the loading by double checked locking</span><br /> <span style="color: rgb(0, 0, 153); font-weight: bold;">public final</span> <span style="color: rgb(0, 102, 0);">T</span> getValue() {<br /> <span style="color: rgb(0, 102, 0);">T</span> result = value;<br /> <span style="color: rgb(0, 0, 153); font-weight: bold;">if</span> (result == null) { <span style="color: rgb(204, 102, 204);">// First check</span><br /> <span style="color: rgb(255, 0, 0); font-weight: bold;">synchronized</span> (this) { <span style="color: rgb(204, 102, 204);">// Lock</span><br /> result = value;<br /> <span style="color: rgb(0, 0, 153); font-weight: bold;">if</span> (result == null) { <span style="color: rgb(204, 102, 204);">// Second check</span><br /> result = loadValue();<br /> value = result;<br /> }<br /> }<br /> }<br /> <span style="color: rgb(0, 0, 153); font-weight: bold;">return</span> result;<br /> }<br /> <span style="color: rgb(0, 0, 153); font-weight: bold;">protected abstract</span> <span style="color: rgb(0, 102, 0);">T</span> loadValue();<br />}</pre><p>This class would then be used like this:</p><code></code><pre><span style="font-weight: bold; color: rgb(0, 0, 153);">package</span> <span style="color: rgb(0, 102, 0);">org.thobe.example</span>;<br /><br /><span style="font-weight: bold; color: rgb(0, 0, 153);">import</span> <span style="color: rgb(0, 102, 0);">java.util.concurrent.LazyLoaded</span>;<br /><br /><span style="color: rgb(0, 0, 153); font-weight: bold;">class</span> <span style="color: rgb(0, 102, 0);">UsesDoubleCheckedLocking</span> {<br /> <span style="color: rgb(204, 102, 204);">// The generics even makes it read well:</span><br /> <span style="font-weight: bold; color: rgb(0, 0, 153);">private final</span> <span style="color: rgb(0, 102, 0);">LazyLoaded</span><<span style="color: rgb(0, 102, 0);">Something</span>>() {<br /> <span style="font-weight:bold;">@Override</span> <span style="color: rgb(0, 0, 153); font-weight: bold;">protected</span> <span style="color: rgb(0, 102, 0);">Something</span> loadValue() {<br /> <span style="color: rgb(0, 0, 153); font-weight: bold;">return new</span> <span style="color: rgb(0, 102, 0);">Something</span>();<br /> }<br /> };<br /> <span style="color: rgb(204, 51, 204);">// ... </span><span style="font-style: italic; color: rgb(204, 51, 204);">your</span><span style="color: rgb(204, 102, 204);"> code will probably do something useful here ...</span><br />}</pre><p>Just expressing my opinion. I hope someone important reads this and makes sure that it gets included in Java7. And while you're at it, make sure we get some standardized ways of composing <span style="font-weight:bold;">Iterable</span>/<span style="font-weight:bold;">Iterator</span>s as well...</p>Tobiashttp://www.blogger.com/profile/15796529762063980134noreply@blogger.com4tag:blogger.com,1999:blog-3940568014938333882.post-86996604057600462442008-12-05T15:05:00.000+01:002008-12-05T15:09:10.849+01:00About types in Neo4j<p>When I first started using <a href="http://neo4j.org/">Neo4j</a> I wondered "Why is there a RelationshipType and no NodeType?", and as more people are introduced to Neo4j I find that this is a quite common question. And of course an obvious question in the strongly typed single inheritance world of Java. This post was inspired by a <a href="http://lists.neo4j.org/pipermail/user/2008-October/000848.html">discussion</a> on the <a href="https://lists.neo4j.org/mailman/listinfo/user">Neo4j mailing list</a>.<br /></p><p>The answer to the question is to be found in the name <span style="font-style: italic;">RelationshipType</span>, it means no more than than the name implies. In particular it does not mean <span style="font-style: italic;">data type</span>. What you are wishing for when you ask for a node type is a way of specifying what properties a node or relationship has, based on its type. Neo4j does not provied any mechanism for this at the core layer. There is a meta model component available that gives you data types for nodes and relationships with verification and all of that if you would like it, but you need to explicitly turn it on.</p><p><span style="font-weight: bold;">So what are </span><span style="font-style: italic; font-weight: bold;">RelationshipType</span><span style="font-weight: bold;">s then?</span> A relationship type is a navigational feature of Neo4j. It is used to implement what is known in graph theory as <span style="font-style: italic;">edge-labeled multigraphs</span>. This feature makes it a whole lot easier to navigate through a graph that represents application data. Adding similar labels to nodes would not provide any navigational benefit, which is why Neo4j does not implement such a feature.</p><p>It is well worth noticing the <span style="font-style: italic;">RelationshipType</span> can be used to implement data types for both relationships <span style="font-weight: bold;">and</span> nodes. In a way that allows nodes to have multiple (union) data types.<br />The way that you implement this in Neo4j is that the data type of a node is determined by the <span style="font-style: italic;">RelationshipType</span> of the relationship is was reached through. A <span style="font-style: italic;">n<sub>1</sub></span> has reached through a relationship <span style="font-style: italic;">r<sub>1</sub></span> with the <span style="font-style: italic;">RelationshipType</span> <span style="font-style: italic;">T<sub>a</sub></span> is said to have the node data type <span style="font-style: italic;">D</span><sub style="font-style: italic;">a</sub>, while the same node <span style="font-style: italic;">n<sub>1</sub></span> reached through another relationship <span style="font-style: italic;">r<sub>2</sub></span> with the <span style="font-style: italic;">RelationshipType</span> <span style="font-style: italic;">T<sub>b</sub></span> is said to have the node data type <span style="font-style: italic;">D</span><sub style="font-style: italic;">b</sub>. Although it is the same node the application logic will treat it completely different, accessing different properties (with or without overlap). This is a very powerful feature, and not as easy to implement using a label for the nodes to determine the data type of the node.</p><p>The <a href="http://wiki.neo4j.org/content/Design_Guide">design guide for Neo4j</a> recommends that domain model objects are implemented by wrapping nodes and relationships, and that the wrapper class is determined by the type of the traversed relationship as mentioned above. To make this even more simple the <a href="http://neo4j.org/">Neo4j community</a> have developed a few components that automate this process. Most notably there is <a href="http://components.neo4j.org/neo-weaver/"><span style="font-style: italic;">neo-weaver</span></a> that automatically implements an interface or abstract class by the means of getting and setting properties on a node/relationship or by traversing relationships.</p>Happy Hacking!Tobiashttp://www.blogger.com/profile/15796529762063980134noreply@blogger.com1tag:blogger.com,1999:blog-3940568014938333882.post-8536105366763480302008-11-20T13:10:00.004+01:002009-09-09T15:20:00.324+02:00Call frame introspection on the JVM<p>One of the pain points of implementing Python on top of the JVM, and in my opinion the worst pain point, is the call frame introspection of Python, and how it is abused throughout Python frameworks. There is in fact a library function <code>sys._getframe()</code> that returns the frame of the current function, or a deeper frame in the call stack if passed an integer representing the depth.</p><p>For Jython this mean that we always have to keep a heap allocated call frame object around for each function invocation, and do all access to local variables through this object. The performance implications of this is absolutely terrible. So any improvement to call frame management would greatly improve performance.</p><p>Thinking about how to better implement call frame objects in Jython I thought <span style="font-style: italic;">“the JVM already manages call frames, and using a debugger I am able to get access to all the aspects of the JVM call frame that I am interested in to construct a Python frame.”</span></p><p>My first idea on how to implement such a thing was to get under the hood of the JVM and add these capabilities there. Said and done, I checked out OpenJDK and started to patch it to my needs. This actually got me to a working prototype, apart from the fact that I failed to tell the garbage collector that I created extra references to some objects, so they ended up not being equal to null but in every other sense behaving like null. I never tracked down where the problem in my code was, since I found another solution. While I was working on this I found code in the JVM that did things that were surprisingly similar to what I was doing. All of this code was located in a subsystem called <span style="font-style: italic;">JVMTI</span>. I looked further into the documentation of the <span style="font-style: italic;">JVM Tooling Interface</span>, and found that it had been around since JVMv1.5 (commonly referred to as Java 5). This was great news! If I could create a JVM frame introspection library using JVMTI I would have a library that works for all the JVM versions that we target for the next release of Jython.</p><p>It took a while before I actually started implementing the frame introspection library, there were other tasks with higher priority, I read up the documentation of JVMTI and there was also the issue with the build process for JNI-libraries being much more painful than for the nice Java stuff I'm used to, since JNI is C code. The last problem I solved over a weekend two weeks ago by implementing a generic make file and an ant task that feeds make with the required parameters of that makefile. This took me a full weekend, since my makefile skills were never that great to begin with, and even rustier than my C skills. I got it to work for my Mac though, and have yet to test it on other platforms (<span style="text-decoration: line-through;">Linux will probably be tested and working before the weekend</span>). This ant task is hosted on Kenai: <a href="https://kenai.com/svn/jvm-frame-introspect~svn/jnitask/">https://kenai.com/svn/jvm-frame-introspect~svn/jnitask/</a>.</p><p>Armed with a good way of building JNI libraries I met up with <a href="http://blog.headius.com/">Charles Nutter</a> in Malmö, where he was <a href="http://www.oredev.org/topmenu/program/trackcoollanguages/charlesolivernutter.4.3efb083311ac562f9fe80008869.html">speaking</a> at the <a href="http://www.oredev.org/">Øredev conference</a>, for three days. We managed to get the library working while hacking at various coffee shops in Malmö, it should still be polished a bit but I've published it <strike>in my personal svn repository</strike> on Kenai: <a href="https://kenai.com/svn/jvm-frame-introspect~svn/frame/">https://kenai.com/svn/jvm-frame-introspect~svn/frame/</a> for everyone who wants to take a look. Trying it out should be as simple as:<br /><code>svn co https://kenai.com/svn/jvm-frame-introspect~svn/frame/ javaframe<br />cd javaframe<br />ant test</code><br />The expected output is a few printouts of stack frame content.</p><p>The call frame introspection library gives you access to:</p><ul><li>Access to one call frame at a given depth.</li><li>Access to the entire stack of call frames for the current thread.</li><li>Access the stack of call frames (without local variables) for any set of threads, or all threads, in the JVM.<br /></li><li>Get the reflected method object that the frame is a representation for.<br /></li><li>Local variables (if this information is added to the class file by javac)<br /><ul><li>The number of local variables.</li><li>The names of the local variables.</li><li>The signatures of the local variables.</li><li>The values of the (currently live) local variables.</li><li>Locals may be accessed either by name or by index.<br />There is also a <code>getThis()</code> method as a shorthand for getting the first local variable, or null if the method is static.<br /></li></ul></li><li>Get the current value of the instruction pointer.<br /></li><li>Get the current line number (if this information is added to the class file by javac).</li></ul><p><span style="font-style: italic; color: rgb(255, 0, 0);">Update 2008-11-23:</span> I've added support for getting stack traces of call frame snapshots from multiple threads in the JVM. The <span style="font-style: italic;">JVMTI</span> guarantees that the stack traces of all threads are captured at the exact same point of execution. There are method calls for getting traces for a given set of threads or for all JVM threads. These methods do not provide access to the local variables in the frames, since there is no way (apart from suspending the threads) to guarantee that the frame depth is the same at the point of capturing the stack trace as at the point of acquiring the local variables.<br />I've also made sure that the build scripts work under Linux as well as Mac OS X, and added licensing information (I use the MIT License for this). Also, by request, cleaned up the paragraphs of this entry.</p><p>It would be great to get input from my peers on what more information you would like to access from the call frames. The Java bytecode of the method perhaps?<br />Charles and I also talked about what else we can use JVMTI for, and he was quite enthusiastic about the ability to walk the entire Java object graph, for implementing the objectspace feature of Ruby. One idea would be to write a library that brings the entire functionality of JVMTI to the Java level. The only problem with this would be that a lot of the JVMTI operations don't make much sense unless they are invoked in conjunction with other operations, and that many of the operations are not callback safe, meaning that we cannot allow the execution of arbitrary Java code in between the JVMTI operations. But it should be possible to create an abstraction with a more Java-esque API that performs multiple JVMTI operations at the JNI level.</p><p>Another interesting aspect of using JVMTI from Java code is that we can prototype a lot of the functionality that we want the JVM to expose to us directly, and thereby <span style="font-style: italic;">vote with code </span>on what we want the JVM of the future to look like.</p><p>I hope you will find this useful!</p><p style="color: red">Update: I have move this project to <a href="http://kenai.com/projects/jvm-frame-introspect">Kenai</a>, the links have been updated accordingly.</p>Tobiashttp://www.blogger.com/profile/15796529762063980134noreply@blogger.com3tag:blogger.com,1999:blog-3940568014938333882.post-59233162780796979252008-09-11T23:25:00.004+02:002008-09-11T23:46:59.053+02:00Meta post: Querying languages versus APIsThis is a <a href="http://journal.thobe.org/2008/09/meta-post-introducing-concept-of-meta.html">meta post</a> about the differences between having a querying language and an API for accessing a database. I want to analyze the pros and cons of both approaches. I believe there are benefits of both models, and that they both have weaknesses.<br />These are my initial ideas on what to discuss in the post:<br /><ul><li>An API provides a type safe way of operating with the data model.</li><li>An API cannot expose query injection weaknesses.</li><li>Queries to in a query languages are handled completely internally in the engine, and can therefore be optimized more easily.<br /></li></ul>Please comment with references on querying languages and your opinion on the pros and cons of the two approaches.Tobiashttp://www.blogger.com/profile/15796529762063980134noreply@blogger.com0tag:blogger.com,1999:blog-3940568014938333882.post-43402067795026167642008-09-11T21:05:00.004+02:002008-09-11T23:10:41.315+02:00Meta post: Introducing the concept of meta postsI am introducing a new kind of post in this journal. Meta posts. I have a list of posts that I want to write, each of these are going to be long, and take a while to write. Therefore I thought it would be easier for me, and lead to better posts, if I could get comments on them before I write them. That way I can find out what aspects of the topic to discuss as well as get some references that might be useful for the post.<br /><br />These meta posts will carry the tag <a href="http://journal.thobe.org/search/label/meta%20post">meta post</a> and I will greatly appreciate any comment on these posts. One of the key features of the meta posts is that I will keep them short, which is why this post ends here.Tobiashttp://www.blogger.com/profile/15796529762063980134noreply@blogger.comtag:blogger.com,1999:blog-3940568014938333882.post-55395714119316864102008-07-14T16:56:00.005+02:002008-07-23T10:53:53.680+02:00My JVM wishlist, pt. 1 - Interface injectionIf you've been following what's going on in the JVM-languages community you have probably stumbled across <a href="http://blogs.sun.com/jrose/">John Roses blog</a>. One of his entries was about <a href="http://blogs.sun.com/jrose/entry/interface_injection_in_the_vm">interface injection</a>. In short wordings <a href="http://openjdk.java.net/projects/mlvm/subprojects.html#InterfaceInjection">interface injection</a> is the ability to at runtime add an interface to a class that was not precompiled as implementing that interface.<br />Interfaces are injected at one of 3 situations:<br /><ul><li>When a method of the interface is invoked on an object.</li><li>When an <code>instanceof</code> check for the interface is performed on an object.</li><li>When the interface is queried for via reflection (I can see this working with <a href="http://java.sun.com/javase/6/docs/api/java/lang/Class.html#isAssignableFrom%28java.lang.Class%29"><code>Class#isAssignableFrom</code></a>, but I have my doubts when it comes to <a href="http://java.sun.com/javase/6/docs/api/java/lang/Class.html#getInterfaces%28%29"><code>Class#getInterfaces</code></a>, although I'm sure someone smart will be able to solve this without having to know about all injectable interfaces beforehand).</li></ul>When any of these occur a class can either have the interface implemented already (in the regular way) or a special static injection method on the interface class is invoked. It is up to this injection method to determine if the given class can implement the interface or not. If it determines that the class can implement the interface any missing methods have to be supplied at that time. John suggests that these methods are to be supplied as <a href="http://blogs.sun.com/jrose/entry/method_handles_in_a_nutshell">method handles</a>. Since <a href="http://openjdk.java.net/projects/mlvm/subprojects.html#MethodHandles">method handles</a>, according to the <a href="http://jcp.org/aboutJava/communityprocess/edr/jsr292/index.html">EDR of the InvokeDynamic proposal</a> (<a href="http://jcp.org/en/jsr/detail?id=292">JSR 292</a>), can be curried this would make it possible to attach an extra state object to the returned method handle, or to return a different implementation of the interface depending on the class they are injected to. Once an injection method of a specific interfaced has been invoked for a specific class, the injection method will never be invoked for that class again, meaning that once a class has been found to not implement an interface, that information will be final, and once an implementation of an interface has been supplied, this implementation can never be changed. This is important since it will allow the VM to perform all optimizations, such as inlining, as before.<br /><br /><span style="font-weight: bold;font-size:130%;" >What can this be used for?</span><br />As a language implementer on the Java platform I think interface injection would be a blessing. In fact I think it is the one thing that would simplify the implementation of languages on the JVM the most. Any language probably has a base interface (as Java has <code>java.lang.Object</code> and Jython has <code>org.python.core.PyObject</code>), let's be unbiased and call it "<code>MyLangObject</code>" for the sake of the continued discussion. There are two things that make the Java platform great:<br /><ol><li>There are a lot of really good toolkits an libraries implemented for the Java platform.</li><li>There are a lot of great languages for the Java platform in which even more great libraries and toolkits will be developed.</li></ol>Therefore, if you are implementing a language for the Java platform you would want to interact with all of these libraries and toolkits. Problem is that most of them haven't been designed with your language in mind, and they shouldn't be. If <code>MyLangObject</code> was an injectable interface all you would need to do to be able to integrate with any object from another language would be to just interact with it through the <code>MyLangObject</code> interface, and the injection mechanism would take care of the rest.<br />The injection mechanism could even be used with the classes within your language. Instead of having a base class supplying the default implementation of the methods of <code>MyLangObject</code> you could let the injection method return the default implementation for your methods.<br />Or why not use interface injection to support function invocation with different argument counts. Each function in your language would implement a set of <code>call</code> methods, one for each argument count it can be invoked with. Your language would then have a set of injectable <code>Callable</code> interfaces one for each argument count that any function in your language can be invoked with, each with only one <code>call</code> method, with the appropriate number of arguments. These interfaces could be generated at runtime if your language supports runtime code generation. The default implementation of the call method in each <code>Callable</code> interface would of course raise an exception, since the function obviously doesn't support that argument count if it doesn't implement the appropriate method.<br />Interface injection really does provide a huge set of opportunities.<br /><br /><span style="font-weight: bold;font-size:130%;" >How could interface injection implement a Meta Object Protocol?</span><br />There is a great project initialized by Attila Szegedi of the JVM-languages comminuty to create <a href="http://dynalang.sourceforge.net/">a common meta object protocol</a> (MOP) for all languages on the JVM. With interface injection this would be a simple task.<br /><ol><li>Let all objects in your language (that supports the MOP) implement the (probably not injectable) interface <code>java.dyn.SupportsMetaObjectProtocol</code>. An interface with only one method:<br /><code>java.dyn.MetaObjectProtocol getMetaObjectProtocol();</code><br />This would return the implementation of the <code>java.dyn.MetaObjectProtocol</code> interface for your particular language.</li><li>The <code>java.dyn.MetaObjectProtocol</code> contains methods for getting method handles for each dynamic language construct that the community have agreed to be a good common construct, such as getters and setters for subscript operations. These method handles would come from the actual implementation of them for your particular language, and would therefore benefit from every imaginable optimization you have cooked up for your language.</li><li>When the main interface of my language is being injected into a class from your language it finds that your class implements <code>java.dyn.SupportsMetaObjectProtocol</code> and uses that to get the method handles for all dynamic language constructs supported by my language, rebinding them them to the method names used in my language.</li></ol>And as simple as that interface injection has been used to implement a common ground for all languages on the Java platform with <span style="font-weight: bold;">absolutely no overhead</span>. I'm not saying that this is <span style="font-style: italic;">the way</span> to implement a meta object protocol for the Java platform, I am just suggesting <span style="font-style: italic;">one way</span> to do it, someone a lot smarter than me might come up with a much better implementation.<br /><br />To sum things up: I can't wait until the JVM supports interface injection.<br /><br /><span style="font-style: italic;">Edit</span>: this post has also been <a href="http://java.dzone.com/articles/my-jvm-wishlist-pt-1-interface">re-posted on Javalobby</a>.Tobiashttp://www.blogger.com/profile/15796529762063980134noreply@blogger.com2tag:blogger.com,1999:blog-3940568014938333882.post-61584838345562465512008-07-10T15:01:00.004+02:002008-07-10T16:50:34.763+02:00The state of the advanced compilerFirst a disclaimer:<br /><blockquote>When I say that I will blog regularly, obviously you should not trust me!<br />I have realized that I don't want to get better at blogging regularly, since I kind of think it's boring and diverts my focus from the more important stuff, the code. But this does not mean that I will not blog more frequently in the future, I might do that, all I am saying is that I will never promise to do more blogging.</blockquote>Even if I am not blogging on it, the advanced Jython compiler is making progress, just not as fast as I would like it to... So the current state of things in the advanced branch of Jython is that I have pluggable compilers working, so that I can switch which compiler Jython should use at any given time. This enables me to test things more easily, and to evolve things more gradualy.<br />I am still revising my ideas about the intermediate representation of code, and it is still mostly on paper. My current thinking is that perhaps the intermediate representation should be less advanced than I first had planed. I will look more at how PyPy does this, and then let the requirements of the rest of the compiler drive the need for the IR.<br />An important change in diraction was made this week. This came from discussion with my mentor, Jim Baker, and from greater insight into how PyPy and Psyco works, form listening to the brilliant people behind these projects at EuroPython. I had originally intended the advanced compiler to do most of it's work up front, and get rid of PyFunctionTable from the Jython code base. The change in direction is the realization that this might not be the best aproach. A better aproach would be to have the advanced compiler work as a JIT optimizer, optimizing on actual observed types, which will probably give us a greater performance boost. This also goes well with the idea that I have always intended of having more specialized code object representations for different kinds of code.<br />The way responsibilities will be divided in between object kinds in the call chain is:<br /><ul><li>Code objects contain the actual code body that gets executed by something.</li><li>Function objects contain references to the code objects. This starts out with a single reference to a general code object, then as time progresses, the function gets hit by different types, which will trigger an invocation on the advanced compiler that will create a specialized version of code, that will also be stored in the function for use when that particular signature is encountered in the future.<br />The functions also contain the environment for use in the code. This consists of the closure variables of the function, and the global scope used in the function.<br /></li><li>Frame objects provide the introspection capabilities into running code as needed by the locals() and globals() functions, and pdb and similar tools. There should be a couple of different frame implementations:<br /><ul><li>Full frames. These contain all of function state. Closure variables, locals, the lot. These are typically used with TableCode implementations of code.<br /></li><li>Generator frames. These are actually divided into two objects. One generator state object, that (as the name suggests) contain the state of the generator. This is most of what a regular frame contains, except the previous frame in the call stack. The other object is an object that supports contains the previous frame in the call stack, and wraps generator state object to provide the frame interface.<br /></li><li>Lazy frames. These are frames that contains almost nothing. Instead they query the running code for their state. I hope to be able to have them access the actual JVM stack frames for this, in which case they will be really interesting.<br /></li></ul>The function object should be responsible for handling the life cycle of the frame objects, but I have not entierly worked out if the creation of the frame object should be up to the code object or the function object. The code object will know exactly wich implementation to choose, but then again, we might want to have different function implementations as well, so it might make sense to the entire responsibility of frames to functions.<br /></li><li>Global scope objects. The global scope could be a regular dictionary. But if we have a special type for globals (that of course supports the dictionary interface) we can have code objects observing the globals for changes to allow some more aggressive optimizations, such as replacing Python loops (over <code>range</code>) with Java loops (with an int counter), so that the JVM JIT can perform all of its loop unrolling magic on it.<br /></li><li>Class objects. These are always created at run time, unlike regular JVM classes which are created at compile time. Since classes are defined by what the locals dictionary looks like when the class definition body terminates it is quite hard to determine the actual class, as created at "define time", will look like. Although in most cases we can statically determine what most of the class will look like.<br /></li><li>Class setup objects. These are to class objects what code objects are to. These contain the code body that defines a class but also a pre-compiled JVM class that contains what the compiler has determined the interface of the class to be.<br />Both class objects and class setup object are fairly far into the future though, and will not be part of the initial release of the advanced compiler. They might in fact never be, if I find that there is a better way of doing things before I get there.</li></ul>The other interesting change that the advanced compiler project will introduces, after these specialized call path objects are of course the actual optimizations that they enable:<br /><ul><li>Type specializations.<br /><ul><li>Primitive type unpacking. This is of course the first, most simple, and most basic type specialization. When we detect that a specific object (often) is of a primitive number type and used extensively, we can generate a version where the number is unpacked from the containing object, and the operations on the number are compiled as primitive number operations.</li><li>Direct invocation of object types. When we detect more coplicated object oriented types we can determine the actual type of the object and find the actual method body for the method we are invoking and insert direct linkage to that body instead of going through the dispatch overhead.<br /></li></ul></li><li>Inlining of builtins. When we detect that a highly used builtin function is extensively used we can inline the body of that builtin, or invoke the builtin function directly without dispatch overhead.<br /></li><li>Expression inlining. Some expressions, in particular generator expressions, imply a lot of over head, since they generate hidden functions, that are only invoked localy, quite often at the same place as they are defined. In this case we can immediatley inline the actual action of the expression. So that for exampel a Python expression such as this:<br /><code>string = ", ".join(str(x) for x in range(14, 23))</code><br />Could be transformed into the following Java snippet:<br /><code>StringBuilder _builder = new StringBuilder();<br />for (int _i = 14; _i < 22; _i++) {<br /> _builder.append(_i);<br /> _builder.append(", ");<br />}<br />_builder.append(22);<br />string = _builder.toString();</code><br />Or in this particular case, perhaps even constant folded...<br /></li><li>Loop transformation. As in the case above the loop over the Python iterator range can be transformed to a regular JVM loop. This might just be a special case of builtin inlining though.</li></ul>The combination of the abstraction and indirection in between the function object and code object and these optimizations are exactly the powerful tool that we need to be able to do really interesting optimistic optimizations while maintaining the ability to back out of such decisions, should they prove to have been too optimistic. All in all providing Jython with a fair improvement in execution speed.<br /><br />So that's a summery on what the current work in progress is. If have i look into my magic 8-ball and try and predict the future I would say that one interesting idea would be to have the compiler be able to persist the optimized code versions so that the next time the program is executed, the previous optimizations are already there. This would in fact be a good way of supporting the "test driven oprimizations" that you might have heard Jim and/or me rant about. So there is defenetly cool stuff going on. I cant wait to write the code, and get it out there, which is why I hereby terminate this blogging session in favour of some serious hacking!Tobiashttp://www.blogger.com/profile/15796529762063980134noreply@blogger.com0tag:blogger.com,1999:blog-3940568014938333882.post-773475327208724602008-07-10T14:32:00.006+02:002008-07-10T17:05:38.677+02:00EuroPython 2008So I've spent 3 days on EuroPython 2008 in Vilnius, Lithuania with my colleges <a href="http://zyasoft.com/pythoneering/">Jim</a> and <a href="http://fwierzbicki.blogspot.com/">Frank</a>, <a href="http://www.sauria.com/blog/">Ted from Sun</a> and of course a lot of other people in the European Python community. Most notably we've spent a fair amount of time talking to the <a href="http://codespeak.net/pypy/dist/pypy/doc/home.html">PyPy</a> group. Since they are mostly based in Europe they didn't have a large presence at PyCon.<br />Jim and I did two talks together. The first one was a tutorial about various ways of manipulating and compiling code in Python, and how to do that while staying compatible across Python implementations. The second was a presentation about the internals of Jython, showing how similar Jython is to CPython internally, and walking through how you go about supporting some of the cool features of Python. We also managed to sneak in some slides about our <a href="http://journal.thobe.org/2008/07/state-of-advanced-compiler.html">ongoing work with the advanced compiler</a> and where that will take us (more on that in a later post). From where I was sitting it seemed people were interested in what we were talking about, and I think our presentations were fairly well received.<br />Yesterday we had a meeting with the PyPy group resulting in a <a href="http://morepypy.blogspot.com/2008/07/ep2008-pypy-meets-jython.html">list of tasks</a> on which our two teams are to collaborate. I think this was very interesting and I believe both sides will get substantial benefits from this effort. It is also my hope that this list will not be complete, but that we will find more interesting tasks to collaborate around after the completion of these tasks.<br />The most important task for us at the moment is to get ctypes ported to Jython and the JVM. This is important for the PyPy team as well since it will make their JVM back end more complete. The way we were outlining it the implementation would be a JNI bridge to _rawffi, the part of ctypes that is implemented in C, and then use the same Python level implementation of the API as PyPy does. Another way of doing it would of course be to use JNA, but I actually think the JNI path might be more maintainable in this case, since PyPy still needs the C version of _rawffi for pypy-c.<br />Personally I am very interested in the PyPy JIT, and I think that my work on the advanced Jython compiler could be very useful for the PyPy team when they start their effort on a JVM back end of their JIT. I also think that I can use a lot of the ideas they have implemented in their JIT project in the advanced compiler.<br />I will not go into the entire list of collaboration points, since the PyPy blog post liked above does a good job there, but I would also like to mention the effort of sharing test cases, which I think is highly important.<br />At the moment Jim and I are in the PyPy sprint room here at EP, and we just have some blogging to do before we get our hands dirty with code.Tobiashttp://www.blogger.com/profile/15796529762063980134noreply@blogger.com1tag:blogger.com,1999:blog-3940568014938333882.post-37285122392247277552008-07-08T10:07:00.007+02:002008-07-23T13:49:35.167+02:00Simple stuff with import hooks in PythonYesterday Jim an I had a tutorial session at EuroPython about dynamic compilation in Python. We brought up the topics of import hooks (<a href="http://www.python.org/dev/peps/pep-0302/">PEP 302</a>) since we have successfully used them as an opportunity to create code dynamically. The code example we demonstrated for that was one of the actual import hooks that we had used in a Jython setup. Even if it was no more than 80 lines of code, it might not have been the most accessible example. A few people asked me afterwords if I had a more simple example, so by public request, here is a simple meta_path hook that prevents the user from importing some modules.<br /><pre><code><span style="color: rgb(153, 51, 153); font-weight: bold;">import</span> sys<br /><br /><span style="color: rgb(153, 51, 153); font-weight: bold;">class</span> <span style="color: rgb(51, 102, 255);">Restriction</span>(<span style="color: rgb(153, 51, 153);">object</span>):<br /> __forbidden = <span style="color: rgb(153, 51, 153);">set</span>()<br /><span style="color: rgb(153, 51, 153);"><span style="color: rgb(153, 51, 153); font-weight: bold;"> @</span>classmethod</span><br /><span style="color: rgb(153, 51, 153); font-weight: bold;"> def</span> <span style="color: rgb(51, 102, 255);">add</span>(<span style="color: rgb(153, 51, 153);">cls</span>, module_name):<br /><span style="color: rgb(153, 51, 153);"> cls</span>.__forbidden.add(module_name)<br /><span style="font-weight: bold; color: rgb(153, 51, 153);"> def</span> <span style="color: rgb(51, 51, 255);">find_module</span>(<span style="color: rgb(153, 51, 153);">self</span>, module_name, package_path):<br /><span style="color: rgb(153, 51, 153); font-weight: bold;"> if</span> package_path: <span style="color: rgb(153, 51, 153); font-weight: bold;">return</span><br /><span style="color: rgb(153, 51, 153); font-weight: bold;"> if</span> module_name in <span style="color: rgb(153, 51, 153);">self</span>.__forbidden:<br /><span style="color: rgb(153, 51, 153); font-weight: bold;"> return</span> <span style="color: rgb(153, 51, 153);">self</span><br /><span style="color: rgb(153, 51, 153); font-weight: bold;"> def</span> <span style="color: rgb(51, 51, 255);">load_module</span>(<span style="color: rgb(153, 51, 153);">self</span>, module_name):<br /><span style="font-weight: bold; color: rgb(153, 51, 153);"> raise</span> <span style="color: rgb(153, 51, 153);">ImportError</span>(<span style="color: rgb(0, 153, 0);">"Restricted"</span>)<br /><br />sys.meta_path.append(Restriction())<br />add = Restriction.add<br /><span style="color: rgb(153, 51, 153); font-weight: bold;">del</span> Restriction<br /></code></pre>If we walk through this class from the top we first of all have a set containing the names of the modules that we will not allow the user to import and a method for adding modules to that set.<br />The next method, <code style="color: rgb(51, 51, 255);">find_module</code>, is the method invoked by the import subsystem. It is responsible for locating the requested module and return an object capable of loading it. The arguments passed to <code style="color: rgb(51, 51, 255);">find_module</code> are the fully qualified module name and, if the module is a sub module of a package, the path where that package was found. If the module cannot be found one should either return <code style="color: rgb(153, 51, 153);">None</code> or raise an <code style="color: rgb(153, 51, 153);">ImportError</code>. This is a handy way to delegate to the next import hook or the default import behavior. If it returns a loader object on the other hand, no other import mechanism will be attempted, which of course is useful in this case. So in this implementation we return <code style="color: rgb(153, 51, 153);">self</code> if the module name is found to be the one of the modules that we want to prevent the user from importing.<br />The loader object should implement the method <code style="color: rgb(51, 51, 255);">load_module</code>, which takes only one argument, the fully qualified module name and returns the corresponding module or raises an <code style="color: rgb(153, 51, 153);">ImportError</code> if the import fails. In this case we know that <code style="color: rgb(51, 51, 255);">load_module</code> is only ever invoked if we want the import to fail, therefore we always raise an <code style="color: rgb(153, 51, 153);">ImportError</code>.<br />There really isn't much more to it. It should be noted however that this isn't a good way to implement security restrictions in Python, since it is possible for any user code to remove the import hook from <code>sys.meta_path</code>, but I still think it makes for a good introductory example to import hooks.<br /><br />Happy hacking!Tobiashttp://www.blogger.com/profile/15796529762063980134noreply@blogger.com0tag:blogger.com,1999:blog-3940568014938333882.post-8419808068922509472008-05-05T04:54:00.002+02:002008-05-05T05:35:50.803+02:00Jython call frames and Dynamic Languages SymposiumThe call for paper deadline for Dynamic Languages Symposium was last friday (the 25th), I was hard at work the week up until the 25th, working on different implementations of how to represent call frame objects. Sadly I didn't manage to get all the results we wanted and write a paper about it before the CFP deadline for DLS. It was still good work though, I have a few different implementations going now. I still need to do some more testing, benchmarking and comparisons on the different implementations before I publish the results of that here. After that I was swamped with work during the weekend, and then I went to San Francisco for JavaOne, and didn't get reliable Internet connection until now. This sums up why I didn't publish any blog post last week. Besides these updates there isn't more interesting news for this blog post. There will be more to write about on Friday when Jim and I have done our JavaOne presentation. I will post then with an update on how it went. Over and out.Tobiashttp://www.blogger.com/profile/15796529762063980134noreply@blogger.com0