What gives Smalltalk the ability to do image persistence, and why can't languages like Ruby/Python serialize themselves?

Deadly 提交于 2020-01-22 10:35:08

问题


In smalltalk, you're able to save the state of the world into an image file. I assume this has to do with Smalltalk's ability to "serialize" itself -- that is, objects can produce their own source code.

1) Is this an accurate understanding?

2) What is the challenge in adding this ability to modern languages (non-lisp, obviously)?

3) Is "serialization" the right word? What's the correct jargon?


回答1:


It's much simpler than "serializing". A Smalltalk image is simply a snapshot of the object memory. It takes the whole RAM contents (after garbage collecting) and dumps it into a file. On startup, it loads that snapshot from disk into RAM and continues processing where it left off. There are some hooks to perform special actions on snapshot and when resuming, but basically this is how it works.

(added: see Lukas Renggli's comment below for a crucial design choice that makes it so simple compared to other environments)




回答2:


Extending upon Bert Freudenberg’s excellent answer.

1) Is this (ie object's ability to serialize their own source code) an accurate understanding?

Nope. As Bert pointed out a Smalltalk image is simply a memory snapshot. The single-source-of-truth of both Smalltalk objects and Smalltalk programs is their memory representation. This is a huge difference to other languages, where programs are represented as text files.

2) What is the challenge in adding this ability to modern languages (non-lisp, obviously)?

Technically, bootstrapping an application from a memory snapshot should be possible for most languages. If I am not mistaken there are solutions that use this approach to speedup startup times for Java applications. You'd have to agree on a canonical memory representation though and you'd need to make care to reinitialize native resources upon restarting the program. For example, in Smalltalk, open files and network connecting are reopened. And also, there's a startup hook to fix the endianness of numbers.

3) Is "serialization" the right word? What's the correct jargon?

Hibernation is the term.




回答3:


Crucial difference is that Smalltalk treats Program just as bunch of objects. And IDE is just a bunch of editors editing those objects, so when you save-load image, all your code is there just as when you left off.

For other languages it could be possible to do so, but I guess there would be more fiddling, depending on how much reflection there is. In most other languages, reflection comes as add-on, or even afterthought, but in Smalltalk it is in the heart of the system.




回答4:


This happens already in a way when you put your computer to sleep, right? The kernel writes running programs to disk and loads them up again later? Presumably the kernel could move a running program to a new machine over a network, assuming same architecture on the other end? Java can serialized all objects also because of the JVM, right? Maybe the hurdle is just architecture implying varied memory layouts?

Edit: But I guess you're interested in using this functionality from the program itself. So I think it's just a matter of implementing the feature in the Python/Ruby interpreter and stdlib, and having some kind of virtual machine if you want to be able to move to a different hardware architecture.



来源:https://stackoverflow.com/questions/13424027/what-gives-smalltalk-the-ability-to-do-image-persistence-and-why-cant-language

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!