Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Pikascript: An ultra-lightweight Python engine that can run in 4Kb of RAM (github.com/pikastech)
163 points by watchdogtimer on May 19, 2022 | hide | past | favorite | 25 comments


Interesing project, but I wish it would show me more about what doesn't work.

The bottom of the readme indicates the list of control flow instructions it supports, but it would be convenient to have in the table the ones it doesn't as well: yield, yield from, with, try/excep, match, lambda...

Does it support "else" in loops? Does it support unpacking?

Hard to know how much Python does this Python does.

Good luck to the project though.


> The bottom of the readme indicates the list of control flow instructions it supports, but it would be convenient to have in the table the ones it doesn't as well

Totally true. Some people would like to contribute to the project, and knowing what's lacking is a good way to make people know how to help.


For those who find this interesting, be sure to check out MicroPython:

http://micropython.org/

It’s super fun to use the built in interpreter over serial connection with say a Raspberry Pi Pico.


MicroPython's authors call it a "subset of python3", and that is a fairly large subset - all the familiar data structures, co-routines, module import, filesystem I/O, FFI, ...; Interpreter is much smaller than CPython and can run on a microcontroller with 256KB code/flash + 16KB RAM.

Another step down is snek, where complete interpreter code can fit in 64KB. Although, this is a much more constrained subset of python (for example - yes dictionaries, no list comprehensions): https://sneklang.org/

PikaScript appear to be even more compact at 32KB code (and is even smaller subset of the language)


There was also PyMite, for 8 bit microcontrollers with 64 KiB of program memory (flash) and 4 KiB of RAM. It supported a subset of the Python 2.5 syntax and could execute a subset of the Python 2.5 bytecodes.

https://wiki.python.org/moin/PyMite


But is it really Python if it doesn't have dictionaries? I'd argue No - they're the fundamental data structure of the language (like tables in Lua, arrays in FORTRAN, etc.).

Pikascript could be useful, but it seems to me it's either a castrated Python, or more positively, a minimalist embedded scripting environment with Python-like syntax. But it's not Python.


How does python even work without dictionaries? This is one of those rare times I find the word `fundamental` to be a bit understated. I mean, a class is just fancy syntactic sugar around a dict.


When you're dealing with low-level compiler tool-chains like this, it's more accurate to say they translate a subset of python syntax (or the AST) into a lower level language (either machine code, or an ASM), so the dependence on dictionaries as a implementation detail of classes/objects (or really anything else that relies on dict) isn't really there (some things may break, but that's because they rely on a particular CPython implementation detail). Well, outside of dicts themselves. Having worked with a similar system, it's really better to think of it like embedded C and write code imperatively (and no dict, just arrays/lists).


I don't see how you could support dictionaries with only 4k of memory. They're awesomely convenient but not very space efficient. Normally that doesn't matter because there's plenty of memory, but in this case it makes a lot of sense.

But yeah so much of the standard library relies on dictionaries, so it wouldn't feel very pythonic.


hooray!


Not sure I could tell a castrated Python from an uncastrated Python except by the look on its face.


This got me wondering what other scripting languages folks use in embedded, low memory setups. I found this about Lua: [1]

[1] https://nicksypark.medium.com/the-feasibility-of-the-embedde...


I think it depends on how hard-core you want to go. Fennel runs in 2K of ram https://news.ycombinator.com/item?id=31029478

Picbit ran in 256bytes, it's descendent is slightly larger https://github.com/stamourv/picobit

Staapl compiles scheme to Forth, to also run on the PIC18 https://www.concatenative.org/wiki/view/Staapl

Of course there is also Sectorlisp, the smallest language out there in 436bytes, 64k ram https://justine.lol/sectorlisp2/


There's JavaScript/ECMAScript via Moddable's XS engine: https://github.com/Moddable-OpenSource/moddable

"A typical microcontroller used with the Moddable SDK has about 45 KB of free memory, 1 MB of Flash ROM, and runs at 80 MHz. The Moddable SDK uses many different techniques, both at build time and at run time, to work efficiently on these devices."


Small "k" for kilo and big "b" for byte.


I was always under the impression that k is 1000 and K is 1024 ... same as with g, G or m and M. Full ack on b and B, though. Maybe the engine is indeed only 512B in RAM utilization?

Edit: Turns out the official project's claim is indeed 4KB so the title is just bogus.


According to the official definitions:

1kb = 1000 bits

1Kib = 1024 bits

1kB = 1000 bytes

1KiB = 1024 bytes

Obviously in practice nobody uses the decimal ones.


Transfer speeds of physical interfaces (PCIe, Ethernet, DDR, HDMI, DisplayPort, USB, SATA, whatever) are usually decimal.


Among other things, hard drive labeled capacities are noteworthy for using the decimal definitions.


Hah! That's a major pet peeve of mine.

So, anyone who uses Windows has probably noticed at some point that "GB" "MB" and "KB" are the power-of-2 variants. Whenever I see one of those tech YouTubers say the formatted capacity of a 1 "TB" drive is 931 "GB" I just cringe. Most storage vendors use the power-of-10 definitions and Windows uses the power-of-2 definitions. 10^12 bytes / 2^30 = 931.32, so no, formatting your drive does not make 7% of the space just disappear.


I am aware of k VS Ki but often see K as a shorthand for Ki.


Upvote. Stupid and really cool at the same time haha.

A nice migration path for existing Python apps to eventually be rewritten in leaner languages.


Cool. So you can use Python on ZX Spectrum


Since I can never keep it straight...

Pika: cute fluffy bunnymouse

Pica: the desire to eat inedible stuff


I wonder if this works in webassembly?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: