Can/Does V8 use the same function code for multiple Function objects?

Showing 1-5 of 5 messages
Can/Does V8 use the same function code for multiple Function objects? T.J. Crowder 12/11/11 6:10 AM
Hi all,

Probably an easy question for anyone who's gotten into the internals
of V8 to answer: Can/Does V8 use the same function code for multiple
Function objects?

For example, consider:

* * * *
// Constructor function creating Thingy objects
function Thing() {
    var privateData, morePrivateData;

    this.doSomething = doSomething;

    function doSomething() {
        // Lots of code here, presumably using privateData and
morePrivateData
    }
}
* * * *

Now suppose I create 1,000 `Thingy` objects via `new Thingy()` 1,000
times. Naturally, that will involve creating 1,000 `doSomething`
Function objects (you never know, I might do `this.doSomething.foo =
"bar";`, so the Function objects *must* be different), each with its
link back to the execution context in which it was created, etc.

But does V8 reuse the *code* of the `doSomething` function across
those 1,000 Function objects? Just setting up the context before
jumping into it? Or is all the code related to `doSomething`
duplicated in memory 1,000 times? (Which would seem un-V8-like. :-) )

I could try to establish this empirically, by creating a bunch of
`Thingy` objects with big `doSomething` functions and seeing how much
memory is used vs. creating a bunch of `Thingy` objects with very
small `doSomething` functions, but I'd prefer not to infer it if
someone happens to know off-hand.

Thanks in advance,
--
T.J. Crowder
Independent Software Engineer
tj / crowder software / com
www / crowder software / com

Re: [v8-users] Can/Does V8 use the same function code for multiple Function objects? Kevin Millikin 12/12/11 12:44 AM
Yes, the code is normally shared.

regards, Kevin
Re: Can/Does V8 use the same function code for multiple Function objects? T.J. Crowder 12/19/11 1:10 AM
Thanks Kevin! Useful to know. Do you happen to have any idea whether
any of the other engines do this as well? I only ask because sometimes
when you're deeply involved in building something like this, you end
up knowing a fair bit about how the other guy does it... :-)

(Sorry for the late reply, I asked GGroups to notify me of replies,
and it still shows that it will, it just...didn't. Odd.)

Best,

-- T.J.

On Dec 12, 8:44 am, Kevin Millikin <kmilli...@chromium.org> wrote:
> Yes, the code is normally shared.
>
> regards, Kevin

Re: [v8-users] Re: Can/Does V8 use the same function code for multiple Function objects? Kevin Millikin 12/19/11 3:11 AM
Hi T.J.,

I don't really know for sure what any other JS engine does, but I would be quite surprised if they duplicated code for distinct function objects created from the same site in the source text.
Re: Can/Does V8 use the same function code for multiple Function objects? T.J. Crowder 12/19/11 3:30 AM
Thanks again Kevin. I'm not so sure I'd be surprised if the old
JScript in IE8 and earlier duplicated them. :-) I may build a test
case to find out.

But good to know about V8.

Cheers,

-- T.J.