-
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
Conversation
@swift-ci Please smoke test |
6a34c28
to
f38e33e
Compare
After this change, where do we use the |
f38e33e
to
f9e417c
Compare
@swift-ci Please smoke test |
assert(substring && "expected successful malloc of copy"); | ||
public: | ||
static TextOwner *make(StringRef Text) { | ||
auto size = totalSizeToAlloc<char>(Text.size()); |
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 to NUL
terminate them.
include/swift/Basic/OwnedString.h
Outdated
/// \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.data(), /*OwnedPtr=*/nullptr); |
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.
This is going to lose the length, which might be incorrect.
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.
Whoops. That carried over from the old implementation. Fixed now.
include/swift/Basic/OwnedString.h
Outdated
return makeUnowned(Str); | ||
} else { | ||
llvm::IntrusiveRefCntPtr<TextOwner> OwnedPtr(TextOwner::make(Str)); | ||
return OwnedString(StringRef(OwnedPtr->getText(), Str.size()), OwnedPtr); |
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.
Nitpick: using std::move
here will avoid a retain/release.
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.
Good point. Thanks!
We cannot use unowned strings for token texts of incrementally parsed syntax trees since the source buffer to which reused nodes refer will have been freed for reused nodes. Always copying the token text whenever OwnedString is passed is too expensive. A reference counted copy of the string allows us to keep the token's string alive across incremental parses while eliminating unnecessary copies.
f9e417c
to
ac512d4
Compare
@swift-ci Please smoke test |
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 comment
The reason will be displayed to describe this comment to others. Learn more.
How about make RawSyntax::make()
to receive StringRef
, then construct OwnedString
in it?
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, that was my idea behind it.
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
Ditto for TriviaPiece
(receive StringRef
).
|
||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any specific reasons to put char*
as a trailing object instead of a regular member?
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.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
ah, OK. The patch LGTM!
LGTM now! |
We cannot use unowned strings for token texts of incrementally parsed syntax trees since the source buffer to which reused nodes refer will have been freed for reused nodes. Always copying the token text whenever
OwnedString
is passed is too expensive. A reference counted copy of the string allows us to keep the token's string alive across incremental parses while eliminating unnecessary copies.