reimplement string with shared_ptr instead of concrete macro #263
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.
Motivation
Concrete struct (defined as
CONCRETE
and*_rep
) is used as reference counter. For example, typeT
is implemented as two class:T_rep
handles properties of given type, andT
handles reference and counter of references toT_rep
. WhenT
is assigned, only reference is assigned with counter in/decreased.std::shared_ptr
plays a similar role like concrete struct, and maintain a spin lock on reference count, thus avoid data-race in parallel execution environments. However, the spin lock drags performance down when code is executed in a single thread serially.Works
string
withshared_ptr
shared_ptr
Performance
Table below shows performance measured by
string_bench
.Before
construct string
equality of string
compare string
slice string
concat string
append string
is quoted
After
construct string
equality of string
compare string
slice string
concat string
append string
is quoted