The comment says "Creates and spawns an isolate that shares the same code as the current isolate."
This isn't true. What it should say (at least in the case with the VM, I have not tried AOT) is "Creates and spawns an isolate that rereads the same entrypoint as the current isolate."
If I write a Dart script that overwrites itself while running and then calls Isolate.spawn I can see it running the new version of the file I wrote, which is not the same code as the calling isolate is running.
Am I correct to assume this is a memory optimization to avoid keeping parse state that we don't need and therefore needing to reread the original file? This does incur a heavy performance penalty when running the original script from source.
The documentation is correct, the implementation is not correct. This has been an old bug against the VM that has not been addressed (see the issues that @nex3 points to above).
"In the special circumstances when two isolates share the same code and are running in the same process (e.g. isolates created via Isolate.spawn), it is also possible to send object instances (which would be copied in the process). This is currently only supported by the dartvm. For now, the dart2js compiler only supports the restricted messages described above"
This dictates that isolates spawned using Isolate.spawn should share the same code.
@a-siva - will we have the resources to fix the implementation soon? If not, should we add a note to the doc calling out the bug on the VM? As it stands with the documentation it's very confusing.
This also came up in https://github.com/dart-lang/sdk/issues/31698
I'm working on fixing this right now.