-
Notifications
You must be signed in to change notification settings - Fork 220
Use StringView and Span in Player #2293
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
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
797b8c2
Add StringView to Player
mateofio d8f4c1d
Add Span from liblcf
mateofio 28a1682
Add ReplacePlaceholders benchmark
mateofio 7459617
Utils replacePlaceHolders - use StringView
mateofio cacd559
Add Utils MakeArray and MakeVector
mateofio c9f8ab5
ReplacePlaceholders - optimize using span
mateofio 713233f
Use StringView starts_with and ends_with
mateofio 7c807d4
Upper and Lower Case use StringView
mateofio b015bcc
Decode functions - use StringView
mateofio 82b8330
Utils Tokenize and ForEachLine: use StringView
mateofio b4561af
Font::GetSize() - use StringView
mateofio 5322ac0
WordWrap: use StringView
mateofio 2dd4993
Text::Draw functions - use StringView
mateofio 0144971
Bitmap::TextDraw - use StringView
mateofio 7624fa5
Optimize Window_Name
mateofio 0c74513
AsyncHandler - use StringView
mateofio a22859e
Cache: Support StringView
mateofio e635250
Window_BattleMessage - StringView
mateofio 4097688
Cache: use ToString
mateofio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| #include <benchmark/benchmark.h> | ||
| #include <font.h> | ||
| #include <rect.h> | ||
| #include <bitmap.h> | ||
| #include <text.h> | ||
| #include <pixel_format.h> | ||
| #include <cache.h> | ||
|
|
||
| static void BM_ReplacePlaceholders(benchmark::State& state) { | ||
| for (auto _: state) { | ||
| Utils::ReplacePlaceholders("One night is %V %U", | ||
| Utils::MakeArray('V', 'U'), | ||
| Utils::MakeSvArray("Rest", "Do not Rest")); | ||
| } | ||
| } | ||
|
|
||
| BENCHMARK(BM_ReplacePlaceholders); | ||
|
|
||
| BENCHMARK_MAIN(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Question: So, a string view is always used when the target function does not take ownership on the string?
Uh oh!
There was an error while loading. Please reload this page.
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.
I wish I could tell you yes, replace all
const std::string&withStringView. Unfortunately this is C++ so there is always one big caveat.StringViewis not null terminated, so if at the end of your call stack you end up calling a C library which requires null terminatedconst char*, you'll end up having to make a copy so you can get a new string with a null byte at the end.Aside from that caveat, yes use
StringViewwhere-ever you need a non-owning reference. Not only is it a generic adapter but the calling convention used is faster (see below).Spandoes not have this caveat, fortunately so you can use it anywhere you'd pass a vector by reference.Performance Note
const string&if you think about it in C terms, is essentially a double pointerconst char**. The reference is a pointer to the string object, which contains another pointer to the bytes.When you pass a
StringViewyou're passing aconst char*, removing one level of indirection. In some cases this can help the optimizer with it's aliasing analysis to produce faster code.The same rule applies to
vectorandSpan.