XmacsLabs / lolly

lolly: A user-friendly C++ library
https://xmacslabs.github.io/lolly/
GNU General Public License v3.0
10 stars 6 forks source link

reimplement string with shared_ptr instead of concrete macro #263

Closed jingkaimori closed 5 months ago

jingkaimori commented 8 months ago

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

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
da-liii commented 8 months ago

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

jingkaimori commented 5 months ago

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