Skip to content

Commit

Permalink
perf(java): Add ClassInfo ClassBytes generation conditions. (#1667)
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. -->
`ClassInfo#classNameBytes` and `ClassInfo#packageNameBytes` are only
used when `classInfo.classId == NO_CLASS_ID && !metaContextShareEnabled`

```java
public void writeClass(MemoryBuffer buffer, ClassInfo classInfo) {
  if (classInfo.classId == NO_CLASS_ID) { // no class id provided.
    // use classname
    if (metaContextShareEnabled) {
      buffer.writeByte(USE_CLASS_VALUE_FLAG);
      // FIXME(chaokunyang) Register class but not register serializer can't be used with
      //  meta share mode, because no class def are sent to peer.
      writeClassWithMetaShare(buffer, classInfo);
    } else {
      // if it's null, it's a bug.
      assert classInfo.packageNameBytes != null;
      metaStringResolver.writeMetaStringBytesWithFlag(buffer, classInfo.packageNameBytes);
      assert classInfo.classNameBytes != null;
      metaStringResolver.writeMetaStringBytes(buffer, classInfo.classNameBytes);
    }
  } else {
    // use classId
    buffer.writeVarUint32(classInfo.classId << 1);
  }
}
```


## Related issues

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

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


## 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.
-->

---------

Signed-off-by: LiangliangSui <[email protected]>
  • Loading branch information
LiangliangSui authored Jun 3, 2024
1 parent a2515a9 commit 31d37f9
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@

/** A class used to encode package/class/field name. */
public class Encoders {
public static final MetaStringEncoder PACKAGE_ENCODER = new MetaStringEncoder('.', '_');
public static final MetaStringDecoder PACKAGE_DECODER = new MetaStringDecoder('.', '_');
public static final MetaStringEncoder GENERIC_ENCODER = new MetaStringEncoder('.', '_');
public static final MetaStringDecoder GENERIC_DECODER = new MetaStringDecoder('.', '_');
public static final MetaStringEncoder PACKAGE_ENCODER = GENERIC_ENCODER;
public static final MetaStringDecoder PACKAGE_DECODER = GENERIC_DECODER;
public static final MetaStringEncoder TYPE_NAME_ENCODER = new MetaStringEncoder('$', '_');
public static final MetaStringDecoder TYPE_NAME_DECODER = new MetaStringDecoder('$', '_');
public static final String ARRAY_PREFIX = "1";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

package org.apache.fury.resolver;

import static org.apache.fury.meta.Encoders.GENERIC_ENCODER;
import static org.apache.fury.meta.Encoders.PACKAGE_ENCODER;
import static org.apache.fury.meta.Encoders.TYPE_NAME_ENCODER;

import org.apache.fury.collection.Tuple2;
import org.apache.fury.config.Language;
Expand Down Expand Up @@ -82,18 +82,23 @@ public class ClassInfo {
if (cls != null && classResolver.getFury().getLanguage() != Language.JAVA) {
this.fullClassNameBytes =
metaStringResolver.getOrCreateMetaStringBytes(
PACKAGE_ENCODER.encode(cls.getName(), Encoding.UTF_8));
GENERIC_ENCODER.encode(cls.getName(), Encoding.UTF_8));
} else {
this.fullClassNameBytes = null;
}
// When `classId == ClassResolver.REPLACE_STUB_ID` was established,
// means only classes are serialized, not the instance. If we
// serialize such class only, we need to write classname bytes.
if (cls != null
&& (classId == ClassResolver.NO_CLASS_ID || classId == ClassResolver.REPLACE_STUB_ID)) {
&& ((classId == ClassResolver.NO_CLASS_ID
&& !classResolver.getFury().getConfig().isMetaShareEnabled())
|| classId == ClassResolver.REPLACE_STUB_ID)) {
// REPLACE_STUB_ID for write replace class in `ClassSerializer`.
Tuple2<String, String> tuple2 = Encoders.encodePkgAndClass(cls);
this.packageNameBytes =
metaStringResolver.getOrCreateMetaStringBytes(PACKAGE_ENCODER.encode(tuple2.f0));
metaStringResolver.getOrCreateMetaStringBytes(Encoders.encodePackage(tuple2.f0));
this.classNameBytes =
metaStringResolver.getOrCreateMetaStringBytes(TYPE_NAME_ENCODER.encode(tuple2.f1));
metaStringResolver.getOrCreateMetaStringBytes(Encoders.encodeTypeName(tuple2.f1));
} else {
this.packageNameBytes = null;
this.classNameBytes = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1701,7 +1701,7 @@ public Class<?> xreadClass(MemoryBuffer buffer) {
Class<?> cls = classNameBytes2Class.get(byteString);
if (cls == null) {
Preconditions.checkNotNull(byteString);
String className = byteString.decode('.', '_');
String className = byteString.decode(Encoders.GENERIC_DECODER);
cls = loadClass(className);
classNameBytes2Class.put(byteString, cls);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.apache.fury.collection.LongMap;
import org.apache.fury.collection.ObjectMap;
import org.apache.fury.memory.MemoryBuffer;
import org.apache.fury.meta.Encoders;
import org.apache.fury.meta.MetaString;

/**
Expand Down Expand Up @@ -109,7 +110,7 @@ public String readMetaString(MemoryBuffer buffer) {
String str = metaStringBytes2StringMap.get(byteString);
if (str == null) {
// TODO support meta string in other languages.
str = byteString.decode('.', '_');
str = byteString.decode(Encoders.GENERIC_DECODER);
metaStringBytes2StringMap.put(byteString, str);
}
return str;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.fury.resolver;

import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;

import org.apache.fury.Fury;
import org.apache.fury.config.Language;
import org.testng.annotations.Test;

public class ClassInfoTest {
@Test
public void testEncodePackageNameAndTypeName() {
Fury fury =
Fury.builder()
.withLanguage(Language.JAVA)
.requireClassRegistration(false)
.withMetaShare(true)
.build();
ClassInfo info = fury.getClassResolver().getClassInfo(org.apache.fury.test.bean.Foo.class);
assertNull(info.packageNameBytes);

Fury fury1 = Fury.builder().withLanguage(Language.JAVA).requireClassRegistration(false).build();
ClassInfo info1 = fury1.getClassResolver().getClassInfo(org.apache.fury.test.bean.Foo.class);
assertNotNull(info1.packageNameBytes);
assertNotNull(info1.classNameBytes);
}
}

0 comments on commit 31d37f9

Please sign in to comment.