[JSONSerialization] Introduce ScalarReferenceTraits

For ScalarTraits, a buffer was always created on the heap to which the
scalar string value was written just to be copied to the output buffer
again. In case the value already exists in a memory buffer it is way
cheaper to avoid the heap allocation and copy it straight to the output
buffer.
This commit is contained in:
Alex Hoppen
2018-06-01 13:12:07 -07:00
parent 48eb400a93
commit 07b449bbd5
5 changed files with 117 additions and 31 deletions

View File

@@ -33,20 +33,39 @@ static void *DontSerializeNodeIdsUserInfoKey = &DontSerializeNodeIdsUserInfoKey;
/// Serialization traits for SourcePresence.
template <>
struct ScalarEnumerationTraits<syntax::SourcePresence> {
static void enumeration(json::Output &out, syntax::SourcePresence &value) {
out.enumCase(value, "Present", syntax::SourcePresence::Present);
out.enumCase(value, "Missing", syntax::SourcePresence::Missing);
struct ScalarReferenceTraits<syntax::SourcePresence> {
static StringRef stringRef(const syntax::SourcePresence &value) {
switch (value) {
case syntax::SourcePresence::Present:
return "\"Present\"";
case syntax::SourcePresence::Missing:
return "\"Missing\"";
}
}
static bool mustQuote(StringRef) {
// The string is already quoted. This is more efficient since it does not
// check for characters that need to be escaped
return false;
}
};
/// Serialization traits for swift::tok.
template <>
struct ScalarEnumerationTraits<tok> {
static void enumeration(Output &out, tok &value) {
struct ScalarReferenceTraits<tok> {
static StringRef stringRef(const tok &value) {
switch (value) {
#define TOKEN(name) \
out.enumCase(value, #name, tok::name);
case tok::name: return "\"" #name "\"";
#include "swift/Syntax/TokenKinds.def"
default: llvm_unreachable("Unknown token kind");
}
}
static bool mustQuote(StringRef) {
// The string is already quoted. This is more efficient since it does not
// check for characters that need to be escaped
return false;
}
};

View File

@@ -80,13 +80,24 @@ namespace json {
/// Serialization traits for SyntaxKind.
template <>
struct ScalarEnumerationTraits<syntax::SyntaxKind> {
static void enumeration(Output &out, syntax::SyntaxKind &value) {
out.enumCase(value, "Token", syntax::SyntaxKind::Token);
out.enumCase(value, "Unknown", syntax::SyntaxKind::Unknown);
struct ScalarReferenceTraits<syntax::SyntaxKind> {
static StringRef stringRef(const syntax::SyntaxKind &value) {
switch (value) {
case syntax::SyntaxKind::Token:
return "\"Token\"";
case syntax::SyntaxKind::Unknown:
return "\"Unknown\"";
% for node in SYNTAX_NODES:
out.enumCase(value, "${node.syntax_kind}", syntax::SyntaxKind::${node.syntax_kind});
case syntax::SyntaxKind::${node.syntax_kind}:
return "\"${node.syntax_kind}\"";
% end
}
}
static bool mustQuote(StringRef) {
// The string is already quoted. This is more efficient since it does not
// check for characters that need to be escaped
return false;
}
};

View File

@@ -405,11 +405,20 @@ struct ObjectTraits<syntax::TriviaPiece> {
/// Serialization traits for TriviaKind.
template <>
struct ScalarEnumerationTraits<syntax::TriviaKind> {
static void enumeration(Output &out, syntax::TriviaKind &value) {
struct ScalarReferenceTraits<syntax::TriviaKind> {
static StringRef stringRef(const syntax::TriviaKind &value) {
switch (value) {
% for trivia in TRIVIAS:
out.enumCase(value, "${trivia.name}", syntax::TriviaKind::${trivia.name});
case syntax::TriviaKind::${trivia.name}:
return "\"${trivia.name}\"";
% end
}
}
static bool mustQuote(StringRef) {
// The string is already quoted. This is more efficient since it does not
// check for characters that need to be escaped
return false;
}
};
} // namespace json