Skip to content

Commit

Permalink
优化批量拷贝的效率
Browse files Browse the repository at this point in the history
  • Loading branch information
tanyaofei committed Jan 7, 2023
1 parent 7525017 commit c944c3a
Show file tree
Hide file tree
Showing 9 changed files with 402 additions and 70 deletions.
35 changes: 30 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@
<dependency>
<groupId>io.github.tanyaofei</groupId>
<artifactId>beancopier</artifactId>
<version>0.1.2</version>
<version>0.1.3</version>
</dependency>
```

## 性能对比

| 拷贝工具 | 拷贝 1 百万个对象耗时 | 递归拷贝 | 链式 setter 对象支持 |
|------------------|--------------|------|----------------|
| **BeanCopier** | 141ms | ✔️ | ✔️ |
| cglib BeanCopier | 79ms |||
| BeanUtils | 5832ms || ✔️ |
| ModelMapper | 2611ms | ✔️ | ✔️ |
| **BeanCopier** | 17ms | ✔️ | ✔️ |
| cglib BeanCopier | 20ms |||
| BeanUtils | 1387ms || ✔️ |
| ModelMapper | 4262ms | ✔️ | ✔️ |

## 使用前提和约束

Expand Down Expand Up @@ -266,6 +266,31 @@ public class Target {
}
```

## 版本记录
+ 0.1.3
+ 大幅度优化批量拷贝的速度 `BeanCopier.cloneList()``BeanCopier.copyList()`, 拷贝一百万个对象由 `200ms+` 缩减到 `20ms+`
+ 修复 `BeanCopier.cloneList` 第一个元素为 null 时会出现 `NullPointerException` 的 bug

+ 0.1.2
+ 除了提供 `BeanCopier` 的静态方法以外,现在可 `new BeanCopierImpl()` 来创建拷贝对象,适用于有类卸载需求的场景
+ 更多测试用例

+ 0.1.1
+ 修正一些 bug
+ 完全的泛型兼容, `List<Integer>` 现在可以拷贝到 `List<? extends Number>`, `IntegerBox extends Box<Integer>` 现在可以拷贝到 `Box<Integer>`

+ 0.1.0
+ 新增 `@Property` 注解支持字段别名和跳过字段选项

+ 0.0.8
+ lombok 依赖 `scope` 修改为 `provided`

+ 0.0.7
+ 不拷贝提供了参数数量不正确的 `getter``setter`

+ 0.0.5
+ 基础类型 `boolean``getter` 方法修改为 `isXxxx()`


## 调试

Expand Down
33 changes: 29 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.tanyaofei</groupId>
<artifactId>beancopier</artifactId>
<version>0.1.2</version>
<version>0.1.3</version>
<name>beancopier</name>

<url>https://github.com/tanyaofei/beancopier</url>
Expand Down Expand Up @@ -39,10 +39,15 @@
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<asm.version>7.1</asm.version>
<lombok.version>1.18.20</lombok.version>
<junit.version>5.7.2</junit.version>
<lombok.version>1.18.24</lombok.version>
<jetbrains-annotations.version>23.1.0</jetbrains-annotations.version>
<guava.version>31.1-jre</guava.version>

<!-- test -->
<junit.version>5.9.0</junit.version>
<modelmapper.version>3.0.0</modelmapper.version>
<cglib.version>3.3.0</cglib.version>
<beanutils.version>1.9.4</beanutils.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -76,11 +81,31 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>${modelmapper.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>${cglib.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>${beanutils.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<!-- 打包 jar 包时包含源码 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/io/github/tanyaofei/beancopier/BeanCopier.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,49 +27,49 @@
public final class BeanCopier {

/**
* {@linkplain BeanCopierImpl#copy(Object, Class)}
* {@link BeanCopierImpl#copy(Object, Class)}
*/
public static <S, T> T copy(S source, Class<T> targetClass) {
return Lazy.IMPL.copy(source, targetClass, null);
}

/**
* {@linkplain BeanCopierImpl#clone(Object)}
* {@link BeanCopierImpl#clone(Object)}
*/
public static <T> T clone(T source) {
return Lazy.IMPL.clone(source);
}

/**
* {@linkplain BeanCopierImpl#cloneList(Collection)}
* {@link BeanCopierImpl#cloneList(Collection)}
*/
public static <T> List<T> cloneList(Collection<T> sources) {
return Lazy.IMPL.cloneList(sources, null);
}

/**
* {@linkplain BeanCopierImpl#cloneList(Collection, Callback)}
* {@link BeanCopierImpl#cloneList(Collection, Callback)}
*/
public static <T> List<T> cloneList(Collection<T> sources, Callback<T, T> callback) {
return Lazy.IMPL.cloneList(sources, callback);
}

/**
* {@linkplain BeanCopierImpl#copyList(Collection, Class)}
* {@link BeanCopierImpl#copyList(Collection, Class)}
*/
public static <S, T> List<T> copyList(Collection<S> sources, Class<T> targetClass) {
return Lazy.IMPL.copyList(sources, targetClass);
}

/**
* {@linkplain BeanCopierImpl#copyList(Collection, Class, Callback)}
* {@link BeanCopierImpl#copyList(Collection, Class, Callback)}
*/
public static <S, T> List<T> copyList(Collection<S> source, Class<T> targetClass, Callback<S, T> callback) {
return Lazy.IMPL.copyList(source, targetClass, callback);
}

/**
* {@linkplain BeanCopierImpl#copy(Object, Class, Callback)}
* {@link BeanCopierImpl#copy(Object, Class, Callback)}
*/
public static <S, T> T copy(S source, Class<T> targetClass, Callback<S, T> callback) {
return Lazy.IMPL.copy(source, targetClass, callback);
Expand Down
Loading

0 comments on commit c944c3a

Please sign in to comment.