-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[libSyntax] Add a reference counted version of OwnedString #18677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,117 +22,89 @@ | |
|
||
#include "llvm/ADT/IntrusiveRefCntPtr.h" | ||
#include "llvm/ADT/StringRef.h" | ||
#include "llvm/Support/TrailingObjects.h" | ||
|
||
using llvm::StringRef; | ||
|
||
namespace swift { | ||
|
||
enum class StringOwnership { | ||
/// An OwnedString holds a weak reference to the underlying string storage | ||
/// and will never attempt to free it. | ||
Unowned, | ||
|
||
/// An OwnedString has its own copy of the underlying string storage and | ||
/// will free the storage upon its destruction. | ||
Copied, | ||
}; | ||
|
||
/// Holds a string - either statically allocated or dynamically allocated | ||
/// and owned by this type. | ||
class OwnedString { | ||
const char *Data; | ||
size_t Length; | ||
StringOwnership Ownership = StringOwnership::Unowned; | ||
|
||
void release() { | ||
if (Ownership == StringOwnership::Copied) | ||
free(const_cast<char *>(Data)); | ||
} | ||
/// An owner that keeps the buffer of a ref counted \c OwnedString alive. | ||
class TextOwner final : public llvm::ThreadSafeRefCountedBase<TextOwner>, | ||
public llvm::TrailingObjects<TextOwner, char> { | ||
TextOwner(StringRef Text) { | ||
std::uninitialized_copy(Text.begin(), Text.end(), | ||
getTrailingObjects<char>()); | ||
} | ||
|
||
void initialize(const char* Data, size_t Length, StringOwnership Ownership) { | ||
this->Length = Length; | ||
this->Ownership = Ownership; | ||
if (Ownership == StringOwnership::Copied && Data) { | ||
char *substring = static_cast<char *>(malloc(Length + 1)); | ||
assert(substring && "expected successful malloc of copy"); | ||
public: | ||
static TextOwner *make(StringRef Text) { | ||
auto size = totalSizeToAlloc<char>(Text.size()); | ||
void *data = ::operator new(size); | ||
return new (data) TextOwner(Text); | ||
} | ||
|
||
memcpy(substring, Data, Length); | ||
substring[Length] = '\0'; | ||
const char *getText() const { return getTrailingObjects<char>(); } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any specific reasons to put There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It allows us to allocate the storage with a single allocation instead of two which should be less overhead. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, OK. The patch LGTM! |
||
}; | ||
|
||
this->Data = substring; | ||
} | ||
else | ||
this->Data = Data; | ||
} | ||
OwnedString(const char* Data, size_t Length, StringOwnership Ownership) { | ||
initialize(Data, Length, Ownership); | ||
} | ||
public: | ||
OwnedString(): OwnedString(nullptr, 0, StringOwnership::Unowned) {} | ||
/// The text this owned string represents | ||
StringRef Text; | ||
|
||
OwnedString(const char *Data, size_t Length): | ||
OwnedString(Data, Length, StringOwnership::Copied) {} | ||
/// In case of a ref counted string an owner that keeps the buffer \c Text | ||
/// references alive. | ||
llvm::IntrusiveRefCntPtr<TextOwner> OwnedPtr; | ||
|
||
OwnedString(StringRef Str) : OwnedString(Str.data(), Str.size()) {} | ||
OwnedString(StringRef Text, llvm::IntrusiveRefCntPtr<TextOwner> OwnedPtr) | ||
: Text(Text), OwnedPtr(OwnedPtr) {} | ||
|
||
OwnedString(const char *Data) : OwnedString(StringRef(Data)) {} | ||
public: | ||
OwnedString() : OwnedString(/*Text=*/StringRef(), /*OwnedPtr=*/nullptr) {} | ||
|
||
OwnedString(const OwnedString &Other): | ||
OwnedString(Other.Data, Other.Length, Other.Ownership) {} | ||
/// Create a ref counted \c OwnedString that is initialized with the text of | ||
/// the given \c StringRef. | ||
OwnedString(StringRef Str) : OwnedString(makeRefCounted(Str)) {} | ||
|
||
OwnedString(OwnedString &&Other): Data(Other.Data), Length(Other.Length), | ||
Ownership(Other.Ownership) { | ||
Other.Data = nullptr; | ||
Other.Ownership = StringOwnership::Unowned; | ||
} | ||
/// Create a ref counted \c OwnedString that is initialized with the text of | ||
/// the given buffer. | ||
OwnedString(const char *Str) : OwnedString(StringRef(Str)) {} | ||
|
||
OwnedString& operator=(const OwnedString &Other) { | ||
if (&Other != this) { | ||
release(); | ||
initialize(Other.Data, Other.Length, Other.Ownership); | ||
} | ||
return *this; | ||
/// Create an \c OwnedString that references the given string. The | ||
/// \c OwnedString will not take ownership of that buffer and will assume that | ||
/// the buffer outlives its lifetime. | ||
static OwnedString makeUnowned(StringRef Str) { | ||
return OwnedString(Str, /*OwnedPtr=*/nullptr); | ||
} | ||
|
||
OwnedString& operator=(OwnedString &&Other) { | ||
if (&Other != this) { | ||
release(); | ||
this->Data = Other.Data; | ||
this->Length = Other.Length; | ||
this->Ownership = Other.Ownership; | ||
Other.Ownership = StringOwnership::Unowned; | ||
Other.Data = nullptr; | ||
/// Create an \c OwnedString that keeps its contents in a reference counted | ||
/// buffer. The contents of \p Str will be copied initially and are allowed to | ||
/// be disposed after the \c OwnedString has been created. | ||
static OwnedString makeRefCounted(StringRef Str) { | ||
if (Str.empty()) { | ||
// Copying an empty string doesn't make sense. Just create an unowned | ||
// string that points to the empty string. | ||
return makeUnowned(Str); | ||
} else { | ||
llvm::IntrusiveRefCntPtr<TextOwner> OwnedPtr(TextOwner::make(Str)); | ||
return OwnedString(StringRef(OwnedPtr->getText(), Str.size()), | ||
std::move(OwnedPtr)); | ||
} | ||
return *this; | ||
} | ||
|
||
OwnedString copy() const { | ||
return OwnedString(Data, Length, StringOwnership::Copied); | ||
} | ||
|
||
/// Returns the length of the string in bytes. | ||
size_t size() const { | ||
return Length; | ||
} | ||
size_t size() const { return Text.size(); } | ||
|
||
/// Returns true if the length is 0. | ||
bool empty() const { | ||
return Length == 0; | ||
} | ||
bool empty() const { return size() == 0; } | ||
|
||
/// Returns a StringRef to the underlying data. No copy is made and no | ||
/// ownership changes take place. | ||
StringRef str() const { | ||
return StringRef { Data, Length }; | ||
} | ||
StringRef str() const { return Text; } | ||
|
||
bool operator==(const OwnedString &Right) const { | ||
return str() == Right.str(); | ||
} | ||
|
||
~OwnedString() { | ||
release(); | ||
} | ||
}; | ||
|
||
} // end namespace swift | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -162,9 +162,9 @@ template <> struct MappingTraits<swift::RC<swift::RawSyntax>> { | |
StringRef nodeIdString; | ||
in.mapRequired("id", nodeIdString); | ||
unsigned nodeId = std::atoi(nodeIdString.data()); | ||
value = | ||
swift::RawSyntax::make(tokenKind, text, leadingTrivia, trailingTrivia, | ||
presence, /*Arena=*/nullptr, nodeId); | ||
value = swift::RawSyntax::make( | ||
tokenKind, swift::OwnedString::makeRefCounted(text), leadingTrivia, | ||
trailingTrivia, presence, /*Arena=*/nullptr, nodeId); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about make There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, you want to control Owned/Unowned in call site. OK then. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, that was my idea behind it. |
||
} else { | ||
swift::SyntaxKind kind; | ||
in.mapRequired("kind", kind); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -454,7 +454,8 @@ struct MappingTraits<swift::syntax::TriviaPiece> { | |
% else: | ||
StringRef text; | ||
in.mapRequired("value", text); | ||
return swift::syntax::TriviaPiece(kind, text); | ||
return swift::syntax::TriviaPiece( | ||
kind, swift::OwnedString::makeRefCounted(text)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto for |
||
% end | ||
break; | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should these be
NUL
-terminated?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since
StringRef
already contains a length, there is no need toNUL
terminate them.