Bug 79125 - CachedScriptSourceProvider::getRange can unnecessarily make a full copy of the source
Summary: CachedScriptSourceProvider::getRange can unnecessarily make a full copy of th...
Status: RESOLVED DUPLICATE of bug 81466
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebCore JavaScript (show other bugs)
Version: 528+ (Nightly build)
Hardware: All All
: P2 Normal
Assignee: Yong Li
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-02-21 10:38 PST by Yong Li
Modified: 2012-05-07 11:50 PDT (History)
7 users (show)

See Also:


Attachments
the patch (1.78 KB, patch)
2012-02-21 10:53 PST, Yong Li
pnormand: commit-queue-
Details | Formatted Diff | Diff
Try again (1.78 KB, patch)
2012-02-21 11:05 PST, Yong Li
no flags Details | Formatted Diff | Diff
another try (1.72 KB, patch)
2012-02-21 12:53 PST, Yong Li
no flags Details | Formatted Diff | Diff
updated (1.77 KB, patch)
2012-02-21 13:09 PST, Yong Li
no flags Details | Formatted Diff | Diff
More (2.85 KB, patch)
2012-02-22 08:27 PST, Yong Li
no flags Details | Formatted Diff | Diff
new patch (2.33 KB, patch)
2012-05-03 10:06 PDT, Yong Li
darin: review-
buildbot: commit-queue-
Details | Formatted Diff | Diff
use substring() now (2.71 KB, patch)
2012-05-04 08:58 PDT, Yong Li
no flags Details | Formatted Diff | Diff
substring() (2.70 KB, patch)
2012-05-04 09:00 PDT, Yong Li
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Yong Li 2012-02-21 10:38:46 PST
JSC::UString getRange(int start, int end) should share the existing StringImpl when it is asked to return the full range.

patch is forthcoming
Comment 1 Yong Li 2012-02-21 10:53:50 PST
Created attachment 127998 [details]
the patch
Comment 2 Philippe Normand 2012-02-21 10:57:44 PST
Comment on attachment 127998 [details]
the patch

Attachment 127998 [details] did not pass gtk-ews (gtk):
Output: http://queues.webkit.org/results/11562167
Comment 3 Early Warning System Bot 2012-02-21 11:00:34 PST
Comment on attachment 127998 [details]
the patch

Attachment 127998 [details] did not pass qt-ews (qt):
Output: http://queues.webkit.org/results/11562168
Comment 4 Yong Li 2012-02-21 11:05:37 PST
Created attachment 128001 [details]
Try again
Comment 5 Yong Li 2012-02-21 12:51:53 PST
Comment on attachment 128001 [details]
Try again

Actually I can make it better
Comment 6 Yong Li 2012-02-21 12:53:30 PST
Created attachment 128026 [details]
another try
Comment 7 Yong Li 2012-02-21 13:09:49 PST
Created attachment 128031 [details]
updated
Comment 8 Yong Li 2012-02-22 08:27:00 PST
Created attachment 128219 [details]
More

Prevent CachedScript from resetting m_script when its string buffer is still referenced by others because doing so won't save any memory but cause extra memory usage if scritp() is called on the CachedScript afterwards.
Comment 9 Yong Li 2012-02-23 06:54:00 PST
Comment on attachment 128219 [details]
More

The change in CachedScript may be unnecessary and also uncomplete.
Comment 10 Yong Li 2012-02-23 06:54:27 PST
Comment on attachment 128031 [details]
updated

Go back to this simple change
Comment 11 Yong Li 2012-05-03 10:06:06 PDT
Created attachment 140039 [details]
new patch
Comment 12 Eric Seidel (no email) 2012-05-03 10:10:47 PDT
Comment on attachment 140039 [details]
new patch

Don't we have a String::substring() or StringImpl::substring() call which would do this optimization for us?
Comment 13 Build Bot 2012-05-03 10:33:40 PDT
Comment on attachment 140039 [details]
new patch

Attachment 140039 [details] did not pass mac-ews (mac):
Output: http://queues.webkit.org/results/12606210
Comment 14 Yong Li 2012-05-03 10:39:52 PDT
(In reply to comment #12)
> (From update of attachment 140039 [details])
> Don't we have a String::substring() or StringImpl::substring() call which would do this optimization for us?

Yes we do. My previous patch uses substringSharingImpl(). But I feel it is a little bit confusing because it hints that, no matter where the substring is, it would always share the original buffer. That might or might not be good, because the CachedScript will destroy decoded script. Actually substringSharingImpl() creates new copy if the substring is not the full string. But the name isn't so accurate.
Comment 15 Darin Adler 2012-05-03 15:57:28 PDT
Comment on attachment 140039 [details]
new patch

View in context: https://bugs.webkit.org/attachment.cgi?id=140039&action=review

> Source/WebCore/bindings/js/CachedScriptSourceProvider.h:59
>              ASSERT(start + length <= this->length());
>  
>              String script = m_cachedScript->script();
> +
> +            if (!start && length == script.length())
> +                return stringToUString(script);
> +
>              return JSC::UString(StringImpl::create(script.impl(), start, length));

This whole thing can be done as a one-liner, without all those assertions, using the substring function, which already correctly optimizes the case of a range covering the entire string.

    return stringToUString(m_cachedScript->script().substring(start, end - start));

> Source/WebCore/bindings/js/StringSourceProvider.h:54
>              ASSERT(length >= 0);
>              ASSERT(start + length <= this->length());
>  
> +            if (!start && length == m_source.length())
> +                return stringToUString(m_source);
> +
>              return JSC::UString(StringImpl::create(m_source.impl(), start, length));

Same here:

    return stringToUString(m_source.substring(start, end - start));
Comment 16 Yong Li 2012-05-04 08:57:30 PDT
(In reply to comment #15)
> (From update of attachment 140039 [details])
> View in context: https://bugs.webkit.org/attachment.cgi?id=140039&action=review
> 
> > Source/WebCore/bindings/js/CachedScriptSourceProvider.h:59
> >              ASSERT(start + length <= this->length());
> >  
> >              String script = m_cachedScript->script();
> > +
> > +            if (!start && length == script.length())
> > +                return stringToUString(script);
> > +
> >              return JSC::UString(StringImpl::create(script.impl(), start, length));
> 
> This whole thing can be done as a one-liner, without all those assertions, using the substring function, which already correctly optimizes the case of a range covering the entire string.
> 
>     return stringToUString(m_cachedScript->script().substring(start, end - start));
> 


Hm... Didn't realize substring() can do that since some day.

substring() also does sanity checks. so no need for those asserts now. patch is forthcoming
Comment 17 Yong Li 2012-05-04 08:58:05 PDT
Created attachment 140246 [details]
use substring() now
Comment 18 Yong Li 2012-05-04 09:00:43 PDT
Created attachment 140247 [details]
substring()
Comment 19 Yong Li 2012-05-07 11:49:53 PDT
Comment on attachment 140247 [details]
substring()

Actually the issue has been fixed with the latest code:

http://trac.webkit.org/changeset/111358
Bug 81466
Comment 20 Yong Li 2012-05-07 11:50:24 PDT

*** This bug has been marked as a duplicate of bug 81466 ***