aboutsummaryrefslogtreecommitdiffstats
path: root/src/qml/qml/v8/qv8sequencewrapper_p_p.h
diff options
context:
space:
mode:
authorJoão Abecasis <joao.abecasis@nokia.com>2012-03-07 23:17:54 +0100
committerQt by Nokia <qt-info@nokia.com>2012-03-09 21:26:40 +0100
commit3c94c7eb7e622051677eb319f6a79e5edb8e2a12 (patch)
treeecead298d8152cf6676c217b20213f71bf5b66c6 /src/qml/qml/v8/qv8sequencewrapper_p_p.h
parent8fd3c019169c45fbc6c8f29b69dc4c17fc8e4187 (diff)
Don't grow container when desired size is known
QList<Type>::reserve() is used upfront to allocate necessary memory in a one go. This tells us straight away whether allocation is possible at all and reduces re-allocations and consequent memory copies. This also has the side effect that no spare memory is allocated, also allowing up to (and including) INT_MAX elements to actually be stored in the underlying QList, as long as enough memory is available to satisfy the allocation request and subsequent fill. The qqmlecmascript::sequenceConversionIndexes was changed to not attempt INT_MAX allocations as, given enough memory and virtual address space, that might succeed but take a really long time. Change-Id: I4b0c965e9c23be78874343a70d7c155933c80903 Reviewed-by: Chris Adams <christopher.adams@nokia.com> Reviewed-by: Kent Hansen <kent.hansen@nokia.com>
Diffstat (limited to 'src/qml/qml/v8/qv8sequencewrapper_p_p.h')
-rw-r--r--src/qml/qml/v8/qv8sequencewrapper_p_p.h25
1 files changed, 14 insertions, 11 deletions
diff --git a/src/qml/qml/v8/qv8sequencewrapper_p_p.h b/src/qml/qml/v8/qv8sequencewrapper_p_p.h
index 9d519809fa..eebc40eeed 100644
--- a/src/qml/qml/v8/qv8sequencewrapper_p_p.h
+++ b/src/qml/qml/v8/qv8sequencewrapper_p_p.h
@@ -265,6 +265,7 @@ static QString convertUrlToString(QV8Engine *, const QUrl &v)
static QVariant toVariant(QV8Engine *e, v8::Handle<v8::Array> array, uint32_t length, bool *succeeded) \
{ \
SequenceType list; \
+ list.reserve(length); \
for (uint32_t ii = 0; ii < length; ++ii) { \
list.append(ConversionFromV8fn(e, array->Get(ii))); \
} \
@@ -334,14 +335,15 @@ static QString convertUrlToString(QV8Engine *, const QUrl &v)
/* according to ECMA262r3 we need to insert */ \
/* undefined values increasing length to newLength. */ \
/* We cannot, so we insert default-values instead. */ \
+ QT_TRY { \
+ c.reserve(newCount); \
+ } QT_CATCH (std::bad_alloc &exception) { \
+ generateWarning(engine, QString(QLatin1String(exception.what()) \
+ + QLatin1String(" during length set"))); \
+ return; /* failed; don't write back any result. */ \
+ } \
while (newCount > count++) { \
- QT_TRY { \
- c.append(DefaultValue); \
- } QT_CATCH (std::bad_alloc &exception) { \
- generateWarning(engine, QString(QLatin1String(exception.what()) \
- + QLatin1String(" during length set"))); \
- return; /* failed; don't write back any result. */ \
- } \
+ c.append(DefaultValue); \
} \
} else { \
/* according to ECMA262r3 we need to remove */ \
@@ -382,15 +384,16 @@ static QString convertUrlToString(QV8Engine *, const QUrl &v)
/* according to ECMA262r3 we need to insert */ \
/* the value at the given index, increasing length to index+1. */ \
QT_TRY { \
- while (signedIdx > count++) { \
- c.append(DefaultValue); \
- } \
- c.append(elementValue); \
+ c.reserve(signedIdx + 1); \
} QT_CATCH (std::bad_alloc &exception) { \
generateWarning(engine, QString(QLatin1String(exception.what()) \
+ QLatin1String(" during indexed set"))); \
return v8::Undefined(); /* failed; don't write back any result. */ \
} \
+ while (signedIdx > count++) { \
+ c.append(DefaultValue); \
+ } \
+ c.append(elementValue); \
} \
/* write back. already checked that object is non-null, so skip that check here. */ \
if (objectType == QV8SequenceResource::Reference) \