Bug 1577161 - Limit length of filename saved for profiler. r=jandem

When profiling scripts with large numbers of dynamic Functions and
complex URLs the profiler overhead can severely skew results. This puts
a cap on the filename length we send to profiler to reduce the impact of
this when using data urls.

Move the definition of strnlen from CTypes into util/Text.h and rename
it to js_strnlen.

Differential Revision: https://phabricator.services.mozilla.com/D43836

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Ted Campbell 2019-10-30 12:51:57 +00:00
Родитель 4519e50d89
Коммит 088a8104e4
3 изменённых файлов: 17 добавлений и 15 удалений

Просмотреть файл

@ -47,6 +47,7 @@
#include "js/UniquePtr.h"
#include "js/Utility.h"
#include "js/Vector.h"
#include "util/Text.h"
#include "util/Unicode.h"
#include "util/Windows.h"
#include "vm/JSContext.h"
@ -2999,17 +3000,6 @@ void IntegerToString(IntegerType i, int radix,
}
}
template <class CharType>
static size_t strnlen(const CharType* begin, size_t max) {
for (size_t i = 0; i < max; i++) {
if (begin[i] == '\0') {
return i;
}
}
return max;
}
// Convert C binary value 'data' of CType 'typeObj' to a JS primitive, where
// possible; otherwise, construct and return a CData object. The following
// semantics apply when constructing a CData object for return:
@ -7761,7 +7751,7 @@ static bool ReadStringCommon(JSContext* cx, InflateUTF8Method inflateUTF8,
case TYPE_signed_char:
case TYPE_unsigned_char: {
char* bytes = static_cast<char*>(data);
size_t length = strnlen(bytes, maxLength);
size_t length = js_strnlen(bytes, maxLength);
// Determine the length.
UniqueTwoByteChars dst(
@ -7784,7 +7774,7 @@ static bool ReadStringCommon(JSContext* cx, InflateUTF8Method inflateUTF8,
case TYPE_unsigned_short:
case TYPE_char16_t: {
char16_t* chars = static_cast<char16_t*>(data);
size_t length = strnlen(chars, maxLength);
size_t length = js_strnlen(chars, maxLength);
result = JS_NewUCStringCopyN(cx, chars, length);
break;
}

Просмотреть файл

@ -37,6 +37,16 @@ template <typename CharT>
extern const CharT* js_strchr_limit(const CharT* s, char16_t c,
const CharT* limit);
template <typename CharT>
static MOZ_ALWAYS_INLINE size_t js_strnlen(const CharT* s, size_t maxlen) {
for (size_t i = 0; i < maxlen; ++i) {
if (s[i] == '\0') {
return i;
}
}
return maxlen;
}
extern int32_t js_fputs(const char16_t* s, FILE* f);
namespace js {

Просмотреть файл

@ -290,9 +290,11 @@ UniqueChars GeckoProfilerRuntime::allocProfileString(JSContext* cx,
hasName = true;
}
// Calculate filename length.
// Calculate filename length. We cap this to a reasonable limit to avoid
// performance impact of strlen/alloc/memcpy.
constexpr size_t MaxFilenameLength = 200;
const char* filenameStr = script->filename() ? script->filename() : "(null)";
size_t filenameLength = strlen(filenameStr);
size_t filenameLength = js_strnlen(filenameStr, MaxFilenameLength);
// Calculate line + column length.
bool hasLineAndColumn = false;