Skip to content
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

reimplement string with shared_ptr instead of concrete macro #263

Closed
wants to merge 6 commits into from

Conversation

jingkaimori
Copy link
Contributor

@jingkaimori jingkaimori commented Feb 13, 2024

Motivation

Concrete struct (defined as CONCRETE and *_rep) is used as reference counter. For example, type T is implemented as two class: T_rep handles properties of given type, and T handles reference and counter of references to T_rep. When T 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

  • rewrite string with shared_ptr
  • add standard allocator for shared_ptr

Performance

Table below shows performance measured by string_bench.

Before

ns/op op/s err% total benchmark
83.29 12,006,624.83 0.7% 0.01 construct string
7.61 131,490,532.01 2.7% 0.01 equality of string
22.99 43,492,682.93 0.5% 0.01 compare string
70.59 14,165,431.64 4.5% 0.01 slice string
120.95 8,268,132.84 0.2% 0.06 concat string
39.24 25,481,767.96 4.2% 0.03 append string
12.55 79,671,371.62 3.5% 0.01 is quoted

After

ns/op op/s err% total benchmark
192.94 5,183,072.68 3.7% 0.01 construct string
21.47 46,582,660.07 0.9% 0.01 equality of string
59.28 16,868,608.16 2.1% 0.01 compare string
130.40 7,668,699.19 1.8% 0.01 slice string
214.34 4,665,550.51 1.8% 0.10 concat string
68.27 14,648,365.31 4.9% 0.04 append string
30.65 32,629,029.70 1.3% 0.02 is quoted

@jingkaimori jingkaimori requested a review from da-liii February 13, 2024 10:20
@da-liii
Copy link
Contributor

da-liii commented Feb 13, 2024

@PikachuHy Could you spare some time to help us review this pr?

@jingkaimori jingkaimori marked this pull request as draft February 14, 2024 14:43
@jingkaimori
Copy link
Contributor Author

Because of unnecessary spin lock, std::shared_ptr will not be used in lolly, see #326

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants