r/ProgrammingLanguages • u/sleepydevxd • Jun 17 '26
V8 Engine Feedback Vector
Hello everyone,
Recently, I'm looking into v8 JavaScript Engine and found out about FeedBack Vector, which I want to investigate more about it in order to understand how the Engine assigns type at runtime after being interpreted by Ignition.
Although I tried to compile the v8 source code and it was able to run a simple script on my machine, I can't seem to be able to get the information regarding Feedback Vector and the data inside it.
So far, I have tried to use some promising flags that are available:
+ --log-feedback-vector
+ --maglev-print-feedback
+ --invocation-count-for-feedback-allocation=1
+ --no-lazy-feedback-allocation
None of them are working - no output to the terminal after I ran it.
I followed this (old and maybe outdated) article:
- An Introduction to Speculative Optimization in V8
With the same code, I can not retrieve the same BinaryOp which I believe have changed after many updates. I want to avoid any "natives syntax", in general, but even when I included it (e.g. %DebugPrint(add);), it does not seem to give me the information that I wanted like in the article.
My goal is to analyse JavaScript's V8 bytecode and output the correct possible types of variables (similar to what Mytype do). So if I can have another way to work around this, it would be very appreciated!
I don't know if this is the right place to ask these kind of question. Therefore, I'm sorry in advanced if this caused any confusion.
Thank you everyone for your time.
View all comments
Show parent comment
6
u/awoocent Jun 18 '26
So the big divide is that V8 and other speculative optimizers aren't figuring out the types based on the source. JavaScript is a dynamic language so you can't prove the types of variables or return types in advance. If you want to try and infer the types as best as you can, that's one thing - it is indeed possible to statically analyze some types from the bytecode, and by making aggressive assumptions, you can get fairly good performance from static analysis alone - this video is a pretty great explanation of a fairly good example of one. Honestly, you should just watch this video, and you can skip the rest of my comment - but I'll write up my answer anyway.
The big problem is functions and objects, and JavaScript basically makes these impossible to statically predict. Consider:
There are loads of possible outcomes from this
obj[0]operation. Off the top of my head, we might be:0from an object.0from the prototype of the object.0and running arbitrary code.And none of this involves the possibility of multiple types of
objhaving different sets of fields, with different layouts.So basically, the conclusion optimizers reached early on is that static type analysis is basically useless. You can't start from JS source, or the bytecode (which is broadly analogous to JS operations - not typed), and be able to figure out enough types to generate fast code. Instead, we do profiling and speculation.
Profiling is what I think V8 is referring to with the feedback vector - I am not a V8 expert though, so I might be wrong. But generally, in addition to the bytecode, a JS engine will also allocate some space for different profiles. A profile will typically correspond to a specific operation - in the above, we might have an operation for the access
obj[0]. When we're running this operation in the interpreter, since we don't expect the interpreter to be fast anyway, we take a little extra time to store information about what types of values this operation is operating on - for example, we might try and check ifobjis always an array.We then use this profiling information to speculate, but crucially, speculation is not the same as just inferring types, because we don't actually know the types. We just know that most of the time, this operation sees a certain type of input. So for
obj[0], we might know thatobjwas an array the first 10,000 times we ran that operation, meaning we can generate a faster array access, instead of a slower field access using a dictionary (this is a simplification of how objects work in JS implementations, but not an impossible case).Because V8 is a JIT compiler, it can wait until these profiles are full of data from a certain number of iterations before it tries to guess any types - this is the first difference from static analysis, relatively little type inference is done from the source code, instead it's based on collected data and if that collected data is missing then V8 may simply choose not to optimize.
The other big difference, and this is really the primary difference that separates speculation from static analysis, is that we allow these assumptions to be wrong. Whenever the optimizer generates specialized code, for example assuming
objis an array and generating an array access, it has to be preceded by some check ensuring thatobjis actually an array. The check itself is pretty cheap, but what happens if it fails? Usually, the first step is bailing out to the interpreter - if V8 assumedobjwas an array, but it actually turns out to be an object, V8 immediately exits the optimized function and rearranges the stack to what the interpreter expects. If this happens enough times, by some heuristic, V8 will actually throw away the compiled function and replace it with a new one, based on the newer profiles that now includeobjnot being an array. These cross-tier jumps are extremely complicated and not often dealt with outside of industry JIT compilers, if you'd like to research them the most common terms are "deoptimization" and "on-stack replacement" (aka "OSR").I've been aiming to summarize roughly what speculation is here, and if it's what you're hoping to investigate, I would encourage you to research it further - the WebKit blog, and in particular this post is a pretty accessible resource for it I think. But, while speculation is kind of like type inference, I want to reiterate - it's really a very different technique, and is more like chaining together a lot of local, dynamically collected information than analyzing the semantics of a program ahead-of-time. And it is basically useless without the ability to give up and replace the compiled code with a different version - this is fine for V8, but it's a pretty gnarly capability that most compilers never deal with. If you are hoping to learn about V8 specifically, I would encourage you to look at small example programs across the different tiers - look at the code, look at the formatted IR, and see where checks are emitted or what type information was inferred. I am not knowledgeable enough about V8 to know the exact command-line flags, but this post seems to have some.
If you are instead hoping to do static analysis/type inference for JS programs, which is what it sounds like you're doing - well, on some level, it's just not possible. JavaScript does not have enough information available from the source to really infer a lot of useful type information. But you can still try, and figuring out how much type information you can detect from a source is an interesting project. Watch the video I linked earlier about the Hop JS engine for inspiration, maybe. But I would not really recommend looking at V8 or any other JS engine for this purpose, they are doing something fundamentally different, and while speculation is genuinely a very successful technique for JavaScript semantics, it's very very complicated and hard to get right - basically nobody does it outside of industry JS VMs. You should only get into it if you are specifically really hoping to get into JIT compilation for dynamic languages.