News for hackers. Stuff that matters.
Re:Crafting Interpreters: Strings by munificent on Monday September 24, 2018 @01:21PM (#18059499)
Author here! Happy to answer questions, take feedback, receive criticism.

Waves awkwardly, shuffles feet.

Re:Crafting Interpreters: Strings by smueller1234 on Monday September 24, 2018 @01:40PM (#18059665)
Hey Bob. Still a big fan of this project. Thank you!

This chapter introduces not only strings but the concept of more complex value types that use an auxiliary struct. There's an anecdote related to that which you might find amusing - and maybe a bit appalling.

In the implementation of perl, there's multiple kinds of integer value type in order to support it's complex implicit casting semantics and additional edge cases. To pick the least wonky, there's plain integer values, "IVs", and those which have a stringified representation of the int attached for caching the expensive conversion, "PVIVs". The former store the int in the primary value struct. The latter use an auxiliary struct with extra space.

Now, the reason for all this prep is that in order to make the two behave the same, and not introduce a lot of branches to low level code, perl always uses the indirect access to the integer value (essentially "x->aux.intval"). This works because for basic integer values, it initializes the aux pointer in the main struct to point to the right place in memory before the location of the main struct such that this access always points at the right slot of the main struct. Madness! So much fun!

(A better explanation is at https://medium.com/booking-com-development/how-we-spent-two-...)

Re:Crafting Interpreters: Strings by _old_dude_ on Monday September 24, 2018 @02:16PM (#18059956)
Shenandoah, one of the GCs in Java uses a similar trick, each time there is an access to a field, instead of directly accessing to it, the VM access to a pointer at the top of the structure that point just to the address below itself, so each field is read using a double indirection. This allows the application to run when the GC move the objects (during the evacuation phase), because each time the GC relocate an object, it also changes the top pointer in the old region to reference the new region. The GC also rewrites the pointers inside the stacks by replacing the pointer to the old region by the value of the top pointer, so the pointers in the stack now reference the new region too.
Re:Crafting Interpreters: Strings by smueller1234 on Monday September 24, 2018 @04:17PM (#18061035)
Ah, and I'd always wanted to read up on that and never prioritized it. Thank you!

This might be a naive question, but how do you do all that redirection under concurrent accesses? Clearly that amount of rewiring isn't going to be atomic? Or am I misunderstand and the only change that needs to be atomic is the top-of-structure redirection?

Re:Crafting Interpreters: Strings by _old_dude_ on Monday September 24, 2018 @05:29PM (#18061622)
As you said, only the top pointer need a CAS.

If you want more info, the is a good presentation by Christine Flood about Shenandoah at FOSDEM 2017 https://archive.fosdem.org/2017/schedule/event/shenandoah/

Re:Crafting Interpreters: Strings by munificent on Monday September 24, 2018 @01:43PM (#18059685)
This is fascinating!
Re:Crafting Interpreters: Strings by smueller1234 on Monday September 24, 2018 @01:51PM (#18059760)
Is that code for nausea inducing? ;)

The perl implementation is full of this sort of stuff. Some corners are hardly penetrable because of it. I'll be visiting the offices in MSN and KIR/SEA next week. Happy to rant over coffee or a drink some night if timing works out.

Re:Crafting Interpreters: Strings by Serow225 on Monday September 24, 2018 @01:52PM (#18059774)
FYI stray period at the end of your link breaks it :)
Re:Crafting Interpreters: Strings by smueller1234 on Monday September 24, 2018 @01:56PM (#18059801)
Fixed, thank you!
Re:Crafting Interpreters: Strings by benhoyt on Monday September 24, 2018 @01:54PM (#18059784)
I'm curious what benchmarking / research you've done on how much difference it makes to use a compact value representation (like the NaN tagging used in Wren, or using the low bits of a pointer to specify object type). Is it just a couple of percent perf improvement, or quite a large amount?

Also, either with a "compact" representation (Wren) or a less compact one like Lox, have you tried storage small strings in the object itself (<= 7 bytes for a 64-bit object size, or <= 15 bytes for two 64-bit words), and how much difference would that make?

Re:Crafting Interpreters: Strings by munificent on Monday September 24, 2018 @02:08PM (#18059881)
> Is it just a couple of percent perf improvement, or quite a large amount?

This is a really really hard question to answer with any generality. Especially when you talk about percentage of perf improvements, then the performance of the rest of the system directly impacts the gain you see from changing the value representation.

At the time that I implemented it in Wren, using NaN tagging made about a 10% improvement in the handful of microbenchmarks I was using at the time, on my machine. You can toggle NaN tagging on and off with a #define, so it's worth checking the difference today on different benchmarks and on top of the other changes I've made to Wren since then.

The last I checked, Wren spent a large fraction of time in instruction decoding and dispatch (not uncommon for a bytecode VM). In an interpreter where that's more efficient (because of a JIT, threaded code, superinstructions, peephole optimizations, etc.), the value representation will probably make a larger difference.

> have you tried storage small strings in the object itself (<= 7 bytes for a 64-bit object size, or <= 15 bytes for two 64-bit words), and how much difference would that make?

I haven't, though I've briefly toyed with it. With Wren, I'm trying really hard to keep the implementation small and simple, so I try to avoid special-case optimizations like this that add a lot of complexity.

It might be worth it, but I'm not sure. It's a hard call to make. Each optimization or little feature on its own seems small, but when you add them all up, it can be the difference between an approachable, hackable codebase, and a big sprawling one.

Re:Crafting Interpreters: Strings by krylon on Monday September 24, 2018 @01:36PM (#18059627)
Thank you so much for all the effort! I can only imagine - vaguely - how much hard work must go into writing such a book.
Re:Crafting Interpreters: Strings by munificent on Monday September 24, 2018 @01:46PM (#18059702)
Here's the log I keep which tracks what I did every day I worked on it (except for a few months in the beginning when the project was still kind of nebulous):

https://github.com/munificent/craftinginterpreters/blob/mast...

Re:Crafting Interpreters: Strings by krylon on Tuesday September 25, 2018 @12:42PM (#18067938)
Wow, that looks like even more work than I had imagined. I hope you enjoy writing that book as much as I enjoy reading it!

> 2018/03/29 - pancakes

:)

Re:Crafting Interpreters: Strings by steveklabnik on Monday September 24, 2018 @02:29PM (#18060071)
As always, this series is fantastic. Thank you so much for writing it.
Re:Crafting Interpreters: Strings by munificent on Monday September 24, 2018 @03:40PM (#18060758)
You're welcome! I'm still planning to read the Rust book when I get some time. Other people who care about technical writing like you are an inspiration.
Re:Crafting Interpreters: Strings by blitmap on Monday September 24, 2018 @07:46PM (#18062465)
There are people who put out books for money, and I can see you are not one of them. I've really enjoyed the passion illustrated in your continually updated series.

(Bravo)

Re:Crafting Interpreters: Strings by z3phyr on Monday September 24, 2018 @01:42PM (#18059680)
Are the 1st and 2nd parts mutually related? Is it ok if I start reading from the Bytecode VM part?

PS: I am enjoying the design and writing. Kudos sir!

Re:Crafting Interpreters: Strings by munificent on Monday September 24, 2018 @01:45PM (#18059694)
They're pretty loosely-coupled. The bytecode VM part assumes you are familiar with the language itself, which is described in an introductory chapter [1]. It also assumes you're familiar with most of the basic structure of an interpreter and the concepts around parsing and stuff, since those are introduced when we do the tree-walk interpreter. If you know those already, you can probably skip straight to the bytecode VM. You can always consult earlier chapters if you get stuck.

[1]: http://www.craftinginterpreters.com/the-lox-language.html

Re:Crafting Interpreters: Strings by z3phyr on Monday September 24, 2018 @01:46PM (#18059710)
Thank you very much! This is what I will do!
Re:Crafting Interpreters: Strings by Waterluvian on Tuesday September 25, 2018 @01:02AM (#18063734)
No question, just a big thank you. I followed along the first half and wrote my own Lox using Python. I learned a ton about a domain I would never have and may never use professionally. But writing my own interpreter helped me understand Python and love the magic of programming all that much more.