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

[8_12] improve strategy of expanding string for performance #341

Closed jingkaimori closed 3 months ago

jingkaimori commented 3 months ago

This pr ensure the layout consistency between array and string, by make member pointer appears at the same position of class, so that union inside tree will not be broken.

331

class string{
int n;
int a_N;
char* a;
//...
}

template<class T>
class array<T>{
int n;
T* a;
//...
}

this pr

class string{
int n;
char* a;
int a_N;
//...
}

template<class T>
class array<T>{
int n;
T* a;
//...
}

Performance

Before

ns/op op/s err% total benchmark
28.76 34,766,499.89 1.9% 0.23 construct string
2.15 465,519,808.15 1.1% 0.24 equality of string
12.29 81,372,418.85 3.4% 0.24 equality of larger string
3.67 272,834,596.28 1.0% 0.24 compare string
6.77 147,721,592.92 1.4% 0.24 compare larger string
15.25 65,576,665.94 0.2% 0.24 slice string
25.84 38,706,819.98 0.6% 0.24 slice string with larger range
26.77 37,362,109.49 1.9% 0.24 concat string
14.44 69,248,803.63 2.7% 0.23 append string
3.95 253,250,191.20 0.7% 0.24 hash of string
31.15 32,099,101.32 0.5% 0.24 hash of larger string
2.45 408,268,298.94 1.1% 0.24 is quoted

After

ns/op op/s err% total benchmark
33.28 30,048,811.93 0.5% 0.24 construct string
2.10 476,358,528.82 1.9% 0.24 equality of string
12.61 79,290,902.83 3.3% 0.24 equality of larger string
3.69 271,309,979.88 1.5% 0.24 compare string
6.98 143,291,461.49 1.6% 0.24 compare larger string
15.86 63,070,424.48 1.1% 0.23 slice string
22.09 45,270,914.17 0.3% 0.24 slice string with larger range
25.83 38,707,374.61 2.1% 0.24 concat string
5.74 174,232,028.58 2.3% 0.22 append string
3.42 292,593,737.92 0.6% 0.24 hash of string
33.60 29,760,571.57 1.2% 0.24 hash of larger string
2.35 424,756,682.83 0.8% 0.24 is quoted
da-liii commented 3 months ago

bench的结果在Pull Request的描述里面贴一下吧

da-liii commented 3 months ago

依据PR里面的描述,前三项的性能退化了。

da-liii commented 3 months ago

在第三次提交下,性能应该有改善,需要一个新的性能对比,来判断性能上是否提升或者降低。

jingkaimori commented 3 months ago

需要一个新的性能对比,来判断性能上是否提升或者降低。

已更新在pr的说明中

da-liii commented 3 months ago

slice/concat/append 变快了,别的基本上差不多,略有变慢

da-liii commented 3 months ago

这是之前的代码:https://github.com/XmacsLabs/lolly/commit/30170ac5c62cd75ad7805b6735a95b55094c9a45

da-liii commented 3 months ago

在PR描述或者代码里面写一下,这个pr和 https://github.com/XmacsLabs/lolly/pull/331 有什么区别,如何做到避免内存问题的