Skip to content

Commit

Permalink
perf(java): Reduce unsafePutPositiveVarInt bytecode size. (#1490)
Browse files Browse the repository at this point in the history
<!--
**Thanks for contributing to Fury.**

**If this is your first time opening a PR on fury, you can refer to
[CONTRIBUTING.md](https://github.com/apache/incubator-fury/blob/main/CONTRIBUTING.md).**

Contribution Checklist

- The **Apache Fury (incubating)** community has restrictions on the
naming of pr titles. You can also find instructions in
[CONTRIBUTING.md](https://github.com/apache/incubator-fury/blob/main/CONTRIBUTING.md).

- Fury has a strong focus on performance. If the PR you submit will have
an impact on performance, please benchmark it first and provide the
benchmark result here.
-->

## What does this PR do?

<!-- Describe the purpose of this PR. -->
Reduce unsafePutPositiveVarInt bytecode size from 192 to 151.


## Related issues

<!--
Is there any related issue? Please attach here.

- #xxxx0
- #xxxx1
- #xxxx2
-->
#1466


## Does this PR introduce any user-facing change?

<!--
If any user-facing interface changes, please [open an
issue](https://github.com/apache/incubator-fury/issues/new/choose)
describing the need to do so and update the document if necessary.
-->

- [ ] Does this PR introduce any public API change?
- [ ] Does this PR introduce any binary protocol compatibility change?


## Benchmark

<!--
When the PR has an impact on performance (if you don't know whether the
PR will have an impact on performance, you can submit the PR first, and
if it will have impact on performance, the code reviewer will explain
it), be sure to attach a benchmark data here.
-->

before

![image](https://github.com/apache/incubator-fury/assets/116876207/f7406efc-abc8-4beb-98ef-0f1dfadeb39d)


with this pr

![image](https://github.com/apache/incubator-fury/assets/116876207/64bbc4d1-af5c-4e0f-ab8e-95f27dcf3ae1)

Signed-off-by: LiangliangSui <[email protected]>
  • Loading branch information
LiangliangSui authored Apr 11, 2024
1 parent da78548 commit 4ed1ff4
Showing 1 changed file with 13 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1260,38 +1260,36 @@ public int unsafeWritePositiveVarInt(int v) {
/**
* Caller must ensure there must be at least 8 bytes for writing, otherwise the crash may occur.
*/
public int unsafePutPositiveVarInt(int index, int v) {
public int unsafePutPositiveVarInt(int index, int value) {
// The encoding algorithm are based on kryo UnsafeMemoryOutput.writeVarInt
// varint are written using little endian byte order.
// This version should have better performance since it remove an index update.
long value = v;
long varInt = (value & 0x7F);
value >>>= 7;
if (value == 0) {
if (value >>> 7 == 0) {
UNSAFE.putByte(heapMemory, address + index, (byte) varInt);
return 1;
}
// bit 8 `set` indicates have next data bytes.
varInt |= ((value & 0x7F) << 8) | 0x80;
value >>>= 7;
if (value == 0) {
// 0x3f80: 0b1111111 << 7
varInt |= (((value & 0x3f80) << 1) | 0x80);
if (value >>> 14 == 0) {
unsafePutInt(index, (int) varInt);
return 2;
}
varInt |= ((value & 0x7F) << 16) | 0x8000;
value >>>= 7;
if (value == 0) {
// 0x1fc000: 0b1111111 << 14
varInt |= (((value & 0x1fc000) << 2) | 0x8000);
if (value >>> 21 == 0) {
unsafePutInt(index, (int) varInt);
return 3;
}
varInt |= ((value & 0x7F) << 24) | 0x800000;
value >>>= 7;
if (value == 0) {
// 0xfe00000: 0b1111111 << 21
varInt |= ((value & 0xfe00000) << 3) | 0x800000;
if (value >>> 28 == 0) {
unsafePutInt(index, (int) varInt);
return 4;
}
varInt |= ((value & 0x7F) << 32) | 0x80000000L;
varInt &= 0xFFFFFFFFFL;
// 0xfe00000: 0b1111111 << 28
varInt |= ((value & 0x7f0000000L) << 4) | 0x80000000L;
unsafePutLong(index, varInt);
return 5;
}
Expand Down

0 comments on commit 4ed1ff4

Please sign in to comment.