Skip to content

Latest commit

 

History

History
458 lines (376 loc) · 17.1 KB

File metadata and controls

458 lines (376 loc) · 17.1 KB

estr

简单易用的 高性能 C 语言 动态字串。基本特性:

  • 简单易用的 API
  • 极高的格式化效率 (使用 stb_snprintf 1.06)
  • 自动创建字串

与 redis SDS 的不同之处

  • 对于基本操作 API, SDS 一般返回新的 动态字串 handle,estr 则返回 写入的长度
  • SDS 传入的 handle 为 handle 本身, estr API 一般传入 handle 的指针

来源

  • 核心部分来自 redis SDS
  • 格式化部分来自 stb_snprintf.c

关于 stb_snprintf.c 本项目内部做了相关修改,添加了一些宏开关,用以取消一些优化,因为那些优化在现今来看效率并不高,特别是关于 字串拷贝 这一部分,现在的 memcpy 内部使用了更优的指令集,具有更高的效率

目录

static int estr_basic_api_tutorial()
{
    estr s = 0;     // init from 0 or null

    //
    // wrt API always write from the beginning
    // cat API write continued
    //
    estr_wrtS(s, "Hello" );  puts(s);       // Hello
    estr_catS(s, ", estr");  puts(s);       // Hello, estr

    estr_wrtS(s, "clear" );  puts(s);       // clear

    // or you can show the detail of estr
    estr_show(s);                           // (estr: e08 5/22):[clear]    // 5 is len, 22 is cap

    puts("----");

    // basic API                                     // DATA            NOTE
    estr_wrtS(s, "str");            estr_show(s);    // [str]           write cstr
    estr_wrtB(s, "123\0567", 7);    estr_show(s);    // [123\0567]      write bin data
    estr_wrtW(s, "  wordd123");     estr_show(s);    // [wordd]         write a word
    estr_wrtL(s, " line\nline2");   estr_show(s);    // [ line]         write a line
    estr_wrtT(s, "abcde", 'e');     estr_show(s);    // [abcd]          write to the end char
    estr_wrtP(s, "%s", "fmt");      estr_show(s);    // [fmt]           write fmt(like sprintf)
    estr_wrtP(s, "%0*d", 4, 1);     estr_show(s);    // [0001]          write fmt(like sprintf)
    estr_wrtF(s, "%s", "efmt");     estr_show(s);    // [efmt]          write fmt(simple internal)

    puts("----");

    // free it after using it
    estr_free(s);
    s = 0;          // you need to set s to 0 by you self if needed
    estr_show(s);

    return ETEST_OK;
}
==================== OUTPUT ===================
Hello
Hello, estr
clear
(estr: e08 5/22):[clear]
----
(estr: e08 3/22):[str]
(estr: e08 7/22):[123.7 ]
(estr: e08 5/22):[wordd]
(estr: e08 5/22):[ line]
(estr: e08 4/22):[abcd]
(estr: e08 3/22):[fmt]
(estr: e08 4/22):[0001]
(estr: e08 4/22):[efmt]
----
(estr: nullptr)

estr 的基本操作 API 主要有 两部分,wrtcat,例如:

estr_wrtS()
estr_catS()

其中 wrt 表示重写,cat 表示追加

基本操作和用法请查看 快速入门

estr 为动态创建版,当传入的 estr 空间不足时,会自动分配新的空间进行写入,所以对于 estr 的 API,大部分实际上传入的是 estr 指针,当发生 realloc 时,自动更新 estr;

sstr 为另一个版本,s 意为 safestack using,它不会自动分配新空间,当空间不足时,不进行写入,返回一个 负值,表示写入失败。

sstr 不会分配新空间,有两种方式可以使用它:

  • 直接使用 estr 的 handle

    {
        estr e = estr_new(4); estr_show(e);
    
        sstr_wrtS(e, "12345678");  estr_show(e);    // will write failed
        sstr_wrtS(e, "1234");      estr_show(e);    // will write ok
    
        estr_free(e);
    }
    ==================== OUTPUT ===================
    (estr: e08 0/4):[]
    (estr: e08 0/4):[]
    (estr: e08 4/4):[1234]
    
  • 使用一个 buf 初始化 sstr

    {
        char buf[16];
    
        // buf 头部有一部分会被内部使用
        sstr s = sstr_init(buf, 8); estr_show(s);
    
        sstr_wrtS(s, "12345678");   estr_show(s);    // will write failed
        sstr_wrtS(s, "1234");       estr_show(s);    // will write ok
    }
    ==================== OUTPUT ===================
    (sstr: e08 0/4):[]
    (sstr: e08 0/4):[]
    (sstr: e08 4/4):[1234]
    

因为 estr 内部格式化部分使用的就是 stb_sprintf,所以理论上来说 estr 的效率不可能比 stb_sprintf 还高,estr 内部已经尽量减少其它操作带来的影响

==================== EBENCHMARK =====================
estr_wrtP
=====================================================
OPRT      OPRTS SCALE      COST       TPO         OPS 
-------- ------ ----- --------- --------- ----------- 
%s(10)   500000    14   24.07ms   48.15ns 20769294.67
%s(4096) 500000  4095   88.83ms  177.67ns  5628475.58
%d       500000    10   20.45ms   40.90ns 24452269.17
%ld(10)  500000    14   21.56ms   43.13ns 23186792.80
%ld(19)  500000    23   26.40ms   52.81ns 18937241.98
%.2f     500000     5   44.52ms   89.05ns 11229898.48
%.32f    500000   158   65.52ms  131.03ns  7631723.55


==================== EBENCHMARK =====================
sstr_wrtP
=====================================================
OPRT      OPRTS SCALE      COST       TPO         OPS 
-------- ------ ----- --------- --------- ----------- 
%s(10)   500000    14   18.02ms   36.04ns 27748487.71
%s(4096) 500000  4095   85.73ms  171.46ns  5832400.15
%d       500000    10   19.44ms   38.88ns 25718841.62
%ld(10)  500000    14   21.06ms   42.12ns 23739435.95
%ld(19)  500000    23   24.87ms   49.74ns 20104543.63
%.2f     500000     5   43.41ms   86.82ns 11518614.08
%.32f    500000   158   61.44ms  122.88ns  8138285.75


==================== EBENCHMARK =====================
estr_wrtF
=====================================================
OPRT      OPRTS SCALE      COST       TPO         OPS 
-------- ------ ----- --------- --------- ----------- 
%s(10)   500000    14   14.60ms   29.20ns 34241884.67
%s(4096) 500000  4095   86.83ms  173.66ns  5758444.76
%S(10)   500000    14   13.96ms   27.92ns 35811488.33
%S(4096) 500000  4095   35.49ms   70.98ns 14089269.61
%i(10)   500000    14   21.72ms   43.44ns 23020257.83
%I(19)   500000    23   30.74ms   61.48ns 16264923.07


==================== EBENCHMARK =====================
sprintf
=====================================================
OPRT      OPRTS SCALE      COST       TPO         OPS 
-------- ------ ----- --------- --------- ----------- 
%s(10)   500000    14   35.78ms   71.57ns 13972334.78
%s(4096) 500000  4095  112.69ms  225.37ns  4437108.43
%d       500000    10   38.10ms   76.19ns 13124393.00
%ld(10)  500000    14   44.43ms   88.85ns 11254670.69
%ld(19)  500000    23   52.84ms  105.67ns  9463423.87
%.2f     500000     5   70.57ms  141.15ns  7084762.09
%.32f    500000   158 2228.97ms 4457.94ns   224318.76


==================== EBENCHMARK =====================
snprintf
=====================================================
OPRT      OPRTS SCALE      COST       TPO         OPS 
-------- ------ ----- --------- --------- ----------- 
%s(10)   500000    14   35.76ms   71.52ns 13981711.92
%s(4096) 500000  4095  110.88ms  221.76ns  4509298.17
%d       500000    10   37.94ms   75.88ns 13178008.54
%ld(10)  500000    14   44.15ms   88.30ns 11324771.81
%ld(19)  500000    23   53.28ms  106.56ns  9384384.38
%.2f     500000     5   70.25ms  140.49ns  7117843.01
%.32f    500000   158 2254.69ms 4509.38ns   221760.17


==================== EBENCHMARK =====================
stbsp_sprintf
=====================================================
OPRT      OPRTS SCALE      COST       TPO         OPS 
-------- ------ ----- --------- --------- ----------- 
%s(10)   500000    14   15.82ms   31.64ns 31605562.58
%s(4096) 500000  4095  428.77ms  857.53ns  1166137.24
%d       500000    10   15.84ms   31.68ns 31565656.57
%ld(10)  500000    14   17.17ms   34.35ns 29115471.96
%ld(19)  500000    23   21.87ms   43.74ns 22864459.48
%.2f     500000     5   38.98ms   77.96ns 12826761.76
%.32f    500000   158   58.33ms  116.65ns  8572359.28


==================== EBENCHMARK =====================
stbsp_snprintf
=====================================================
OPRT      OPRTS SCALE      COST       TPO         OPS 
-------- ------ ----- --------- --------- ----------- 
%s(10)   500000    14   25.89ms   51.77ns 19316206.30
%s(4096) 500000  4095  449.58ms  899.16ns  1112149.12
%d       500000    10   16.77ms   33.55ns 29808036.25
%ld(10)  500000    14   19.07ms   38.13ns 26226068.71
%ld(19)  500000    23   22.85ms   45.69ns 21885669.26
%.2f     500000     5   40.24ms   80.48ns 12424829.78
%.32f    500000   158   63.19ms  126.39ns  7912143.56
==================== EBENCHMARK =====================
estr_wrtP
=====================================================
OPRT      OPRTS SCALE      COST       TPO         OPS 
-------- ------ ----- --------- --------- ----------- 
%s(10)   500000    14   21.44ms   42.89ns 23318065.60
%s(4096) 500000  4095   99.51ms  199.03ns  5024373.69
%d       500000    10   19.35ms   38.71ns 25835630.21
%ld(10)  500000    14   22.31ms   44.61ns 22415936.76
%ld(19)  500000    23   30.40ms   60.81ns 16445004.99
%.2f     500000     5   66.81ms  133.61ns  7484272.33
%.32f    500000   158  101.95ms  203.90ns  4904422.23


==================== EBENCHMARK =====================
sstr_wrtP
=====================================================
OPRT      OPRTS SCALE      COST       TPO         OPS 
-------- ------ ----- --------- --------- ----------- 
%s(10)   500000    14   15.53ms   31.06ns 32191815.84
%s(4096) 500000  4095   90.78ms  181.56ns  5507822.99
%d       500000    10   18.83ms   37.66ns 26555329.73
%ld(10)  500000    14   20.72ms   41.44ns 24129512.16
%ld(19)  500000    23   28.67ms   57.34ns 17438440.91
%.2f     500000     5   65.42ms  130.83ns  7643452.39
%.32f    500000   158   96.46ms  192.91ns  5183656.54


==================== EBENCHMARK =====================
estr_wrtF
=====================================================
OPRT      OPRTS SCALE      COST       TPO         OPS 
-------- ------ ----- --------- --------- ----------- 
%s(10)   500000    14   12.28ms   24.56ns 40709113.68
%s(4096) 500000  4095   88.85ms  177.70ns  5627357.76
%S(10)   500000    14   10.48ms   20.97ns 47693854.29
%S(4096) 500000  4095   49.66ms   99.31ns 10069248.64
%i(10)   500000    14   18.82ms   37.64ns 26568006.55
%I(19)   500000    23   28.50ms   56.99ns 17546653.57


==================== EBENCHMARK =====================
sprintf
=====================================================
OPRT      OPRTS SCALE      COST       TPO         OPS 
-------- ------ ----- --------- --------- ----------- 
%s(10)   500000    14   38.90ms   77.81ns 12851917.96
%s(4096) 500000  4095 1265.61ms 2531.23ns   395064.94
%d       500000    10   35.34ms   70.69ns 14147086.58
%ld(10)  500000    14   38.06ms   76.11ns 13138362.14
%ld(19)  500000    23   45.77ms   91.53ns 10925354.59
%.2f     500000     5  155.75ms  311.49ns  3210369.36
%.32f    500000   158 3298.29ms 6596.57ns   151593.87


==================== EBENCHMARK =====================
snprintf
=====================================================
OPRT      OPRTS SCALE      COST       TPO         OPS 
-------- ------ ----- --------- --------- ----------- 
%s(10)   500000    14   43.52ms   87.03ns 11489859.52
%s(4096) 500000  4095 1239.15ms 2478.31ns   403501.22
%d       500000    10   35.97ms   71.93ns 13901610.02
%ld(10)  500000    14   37.68ms   75.37ns 13268293.93
%ld(19)  500000    23   48.07ms   96.15ns 10400710.46
%.2f     500000     5  157.41ms  314.81ns  3176516.99
%.32f    500000   158 3291.72ms 6583.45ns   151896.10


==================== EBENCHMARK =====================
stbsp_sprintf
=====================================================
OPRT      OPRTS SCALE      COST       TPO         OPS 
-------- ------ ----- --------- --------- ----------- 
%s(10)   500000    14   15.57ms   31.15ns 32103798.52
%s(4096) 500000  4095  566.33ms 1132.66ns   882874.86
%d       500000    10   14.51ms   29.02ns 34453138.43
%ld(10)  500000    14   17.52ms   35.03ns 28543625.45
%ld(19)  500000    23   23.31ms   46.63ns 21447254.74
%.2f     500000     5   65.18ms  130.36ns  7670974.24
%.32f    500000   158  101.44ms  202.88ns  4928947.79


==================== EBENCHMARK =====================
stbsp_snprintf
=====================================================
OPRT      OPRTS SCALE      COST       TPO         OPS 
-------- ------ ----- --------- --------- ----------- 
%s(10)   500000    14   18.65ms   37.30ns 26809943.29
%s(4096) 500000  4095  596.73ms 1193.47ns   837895.10
%d       500000    10   18.26ms   36.52ns 27385617.26
%ld(10)  500000    14   20.86ms   41.73ns 23963817.70
%ld(19)  500000    23   29.30ms   58.59ns 17066376.57
%.2f     500000     5   67.41ms  134.82ns  7417306.82
%.32f    500000   158  105.06ms  210.13ns  4759058.84
==================== EBENCHMARK ====================
estr_wrtP
====================================================
OPRT      OPRTS SCALE     COST       TPO         OPS
-------- ------ ----- -------- --------- -----------
%s(10)   500000    14  22.00ms   44.00ns 22727479.34
%s(4096) 500000  4095 134.00ms  268.00ns  3731359.99
%d       500000    10  18.15ms   36.29ns 27552915.37
%ld(10)  500000    14  21.93ms   43.86ns 22799401.74
%ld(19)  500000    14  21.09ms   42.17ns 23713203.04
%.2f     500000     5  47.01ms   94.01ns 10636736.31
%.32f    500000   158  79.93ms  159.86ns  6255481.37


==================== EBENCHMARK ====================
sstr_wrtP
====================================================
OPRT      OPRTS SCALE     COST       TPO         OPS
-------- ------ ----- -------- --------- -----------
%s(10)   500000    14  17.96ms   35.92ns 27840883.78
%s(4096) 500000  4095 126.10ms  252.21ns  3964997.01
%d       500000    10  17.86ms   35.72ns 27997872.16
%ld(10)  500000    14  22.15ms   44.30ns 22574892.20
%ld(19)  500000    14  20.97ms   41.93ns 23847680.10
%.2f     500000     5  45.02ms   90.04ns 11105607.67
%.32f    500000   158  78.85ms  157.70ns  6340993.25


==================== EBENCHMARK ====================
estr_wrtF
====================================================
OPRT      OPRTS SCALE     COST       TPO         OPS
-------- ------ ----- -------- --------- -----------
%s(10)   500000    14  15.00ms   30.00ns 33338667.52
%s(4096) 500000  4095 143.15ms  286.30ns  3492883.60
%S(10)   500000    14  14.02ms   28.03ns 35671225.45
%S(4096) 500000  4095  59.98ms  119.96ns  8336139.83
%i(10)   500000    14  19.00ms   38.00ns 26315096.97
%I(19)   500000    23  27.17ms   54.33ns 18404478.91


==================== EBENCHMARK ====================
sprintf
====================================================
OPRT      OPRTS SCALE     COST       TPO         OPS
-------- ------ ----- -------- --------- -----------
%s(10)   500000    14  50.01ms  100.02ns  9997760.50
%s(4096) 500000  4095 151.13ms  302.26ns  3308385.90
%d       500000    10  73.12ms  146.23ns  6838429.79
%ld(10)  500000    14  84.12ms  168.24ns  5943847.29
%ld(19)  500000    14  84.08ms  168.16ns  5946781.07
%.2f     500000     5 141.15ms  282.29ns  3542403.63
%.32f    500000   158 799.14ms 1598.29ns   625668.68


==================== EBENCHMARK ====================
snprintf
====================================================
OPRT      OPRTS SCALE     COST       TPO         OPS
-------- ------ ----- -------- --------- -----------
%s(10)   500000    14  48.00ms   95.99ns 10417252.64
%s(4096) 500000  4095 154.01ms  308.02ns  3246504.49
%d       500000    10  75.00ms  150.00ns  6666666.67
%ld(10)  500000    14  92.15ms  184.31ns  5425729.90
%ld(19)  500000    14  97.13ms  194.26ns  5147724.24
%.2f     500000     5 149.15ms  298.30ns  3352372.57
%.32f    500000   158 810.08ms 1620.16ns   617221.70


==================== EBENCHMARK ====================
stbsp_sprintf
====================================================
OPRT      OPRTS SCALE     COST       TPO         OPS
-------- ------ ----- -------- --------- -----------
%s(10)   500000    14  15.13ms   30.25ns 33054354.58
%s(4096) 500000  4095 541.16ms 1082.32ns   923939.80
%d       500000    10  15.03ms   30.05ns 33276319.91
%ld(10)  500000    14  18.13ms   36.25ns 27584228.44
%ld(19)  500000    14  20.02ms   40.03ns 24980390.39
%.2f     500000     5  46.12ms   92.24ns 10840837.00
%.32f    500000   158  85.15ms  170.30ns  5871976.81


==================== EBENCHMARK ====================
stbsp_snprintf
====================================================
OPRT      OPRTS SCALE     COST       TPO         OPS
-------- ------ ----- -------- --------- -----------
%s(10)   500000    14  19.00ms   38.00ns 26317174.59
%s(4096) 500000  4095 554.14ms 1108.29ns   902291.57
%d       500000    10  17.97ms   35.94ns 27821829.01
%ld(10)  500000    14  20.14ms   40.28ns 24824244.35
%ld(19)  500000    14  20.00ms   40.00ns 25000250.00
%.2f     500000     5  44.15ms   88.29ns 11325772.25
%.32f    500000   158  87.90ms  175.80ns  5688340.38