Skip to content

tbox uuid的性能存在缺陷 #279

@perrynzhou

Description

@perrynzhou

问题描述

  • tbox中uuid的性能太差了,考虑到性能我重写了uuid的功能,新的uuid的功能性能在跑一亿次的输出uuid,原生的uuid运行时间为751.416秒,重写后uuid运行时间是8.861秒。优化后的性能提升84倍。下面是参照基于运行时间的整体测试

测试机器配置

Image

tbox原始uuid性能测试

  • 测试代码
/*************************************************************************
  > File Name: uuid_main.c
  > Author:perrynzhou
  > Mail:perrynzhou@gmail.com
  > Created Time: Wed 18 Jun 2025 10:59:16 AM CST
 ************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdint.h>
#include <tbox/hash/uuid.h>
#include <tbox/prefix/type.h>
#define UUID_SIZE 37
inline static FILE *init_dump_file(const char *path)
{
  if (path == NULL)
  {
    return tb_null;
  }
  return fopen(path, "aw+");
}
int main(int argc, char *argv[])
{
  if (argc <= 1)
  {
    fprintf(stdout, "usage: %s {loop_count} {dump_file}\n", argv[0]);
    return -1;
  }
  char *ptr = NULL;
  uint64_t cnt = strtol(argv[1], &ptr, 10);
  tb_char_t data[UUID_SIZE] = {'\0'};
  FILE *fp = init_dump_file(argv[2]);
  if (fp == tb_null)
  {
    for (uint64_t i = 0; i < cnt; i++)
    {
      tb_uuid_make_cstr(data, tb_null);
    }
  }
  else
  {
    for (uint64_t i = 0; i < cnt; i++)
    {
      const tb_char_t *uuid = tb_uuid_make_cstr(data, tb_null);
      fprintf(fp, "%s\n", (tb_char_t *)&data);
    }
    fclose(fp);
  }
  fprintf(stdout, "******************************\n");
  return 0;
}
  • 性能测试数据
Image

改造后tbox uuid性能测试

  • 重写了tbox/src/hash/uuid的功能后进行测试,下面是测试代码
int main(int argc, char *argv[])
{
  if (argc < 2)
  {
    fprintf(stdout, "usage: %s {uuid_count} {uuid_dump_file_path}\n");
    return -1;
  }
  char *ptr = 0;
  uint64_t cnt = strtol(argv[1], &ptr, 10);
  tb_uuid_t uid;
  tb_uuid_init(&uid);
  tb_char_t uuid_str[37];
  if (argv[2] != tb_null)
  {
    remove(argv[2]);
  }
  FILE *fp = fopen(argv[2], "a+");
  if (fp != tb_null)
  {
    for (uint64_t i = 0; i < cnt; i++)
    {
      tb_char_t *str = tb_uuid_make(&uid, 4, &uuid_str);
      fprintf(fp, "%s\n", str);
    }
    fclose(fp);
  }
  else
  {
    for (uint64_t i = 0; i < cnt; i++)
    {
      tb_uuid_make(&uid, 4, &uuid_str);
    }
  }
  tb_uuid_exit(&uid);
}
  • 性能测试数据
Image

是否可以发起PR?

  • 重构后的性能提升非常明显,这个是否可以发起一个pr?发起pr流程是怎样的呢?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions