MrChang0 commented on code in PR #1616: URL: https://github.com/apache/incubator-fury/pull/1616#discussion_r1596360862
########## integration_tests/graalvm_tests/src/main/java/org/apache/fury/graalvm/Collection.java: ########## @@ -0,0 +1,58 @@ +/* + * 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.graalvm; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import org.apache.fury.Fury; +import org.apache.fury.util.Preconditions; + +public class Collection { Review Comment: > This may be confusing with java.util.Collection. How about naming it as `CollectionTest` ok, I am looking for why `ArrayAsList` failed. ########## java/fury-core/src/main/java/org/apache/fury/serializer/collection/CollectionSerializers.java: ########## @@ -411,6 +414,60 @@ public ConcurrentSkipListSet newCollection(MemoryBuffer buffer) { } } + public static final class SetFromMapSerializer extends CollectionSerializer<Set<?>> { + + private static final long MAP_FIELD_OFFSET; + + private static final long SET_FIELD_OFFSET; + + static { + try { + Field mapField = Class.forName("java.util.Collections$SetFromMap").getDeclaredField("m"); + MAP_FIELD_OFFSET = Platform.objectFieldOffset(mapField); + Field setField = Class.forName("java.util.Collections$SetFromMap").getDeclaredField("s"); + SET_FIELD_OFFSET = Platform.objectFieldOffset(setField); + } catch (final Exception e) { + throw new RuntimeException(e); + } + } + + public SetFromMapSerializer(Fury fury, Class<Set<?>> type) { + super(fury, type, false); + } + + @Override + public void write(MemoryBuffer buffer, Set value) { + final Map<?, Boolean> map = (Map<?, Boolean>) Platform.getObject(value, MAP_FIELD_OFFSET); + fury.writeRef(buffer, map); + } + + @Override + public Set<?> read(MemoryBuffer buffer) { + final Map<?, Boolean> map = (Map<?, Boolean>) fury.readRef(buffer); + final Set<?> set = Collections.newSetFromMap(Collections.emptyMap()); Review Comment: > Why not just invoke `Collections.newSetFromMap(map)` constructor must be a empty map. ########## java/fury-core/src/main/java/org/apache/fury/serializer/collection/CollectionSerializers.java: ########## @@ -433,28 +436,18 @@ public SetFromMapSerializer(Fury fury, Class<Set<?>> type) { } @Override - public Collection newCollection(MemoryBuffer buffer) { - int numElements = buffer.readVarUint32Small7(); - setNumElements(numElements); - final ClassInfo mapClassInfo = fury.getClassResolver().readClassInfo(buffer); - final MethodHandle methodHandle = ReflectionUtils.getCtrHandle(mapClassInfo.getCls(), true); - Map map; - try { - map = (Map) methodHandle.invoke(); - } catch (Throwable e) { - throw new RuntimeException(e); - } - final Set set = Collections.newSetFromMap(map); - fury.getRefResolver().reference(set); - return set; + public void write(MemoryBuffer buffer, Set<?> value) { + Object fieldValue = Platform.getObject(value, MAP_FIELD_OFFSET); + fury.writeRef(buffer, fieldValue); } @Override - public Collection onCollectionWrite(MemoryBuffer buffer, Set<?> value) { - buffer.writeVarUint32Small7(value.size()); - final Map<?, Boolean> map = (Map<?, Boolean>) Platform.getObject(value, MAP_FIELD_OFFSET); - fury.getClassResolver().writeClassAndUpdateCache(buffer, map.getClass()); - return value; + public Set<?> read(MemoryBuffer buffer) { + final Map map = (Map) fury.readRef(buffer); + final Set set = Collections.newSetFromMap(Collections.emptyMap()); Review Comment: > How about: > > ```java > final Map map = (Map) fury.readRef(buffer); > return Collections.newSetFromMap(map); > ``` In JDK17 ```java SetFromMap(Map<E, Boolean> map) { if (!map.isEmpty()) throw new IllegalArgumentException("Map is non-empty"); m = map; s = map.keySet(); } ``` that's why I can't do it. ########## java/fury-core/src/main/java/org/apache/fury/serializer/collection/CollectionSerializers.java: ########## @@ -411,6 +415,66 @@ public ConcurrentSkipListSet newCollection(MemoryBuffer buffer) { } } + public static final class SetFromMapSerializer extends CollectionSerializer<Set<?>> { + + private static final long MAP_FIELD_OFFSET; + + static { + try { + Field mapField = Class.forName("java.util.Collections$SetFromMap").getDeclaredField("m"); + MAP_FIELD_OFFSET = Platform.objectFieldOffset(mapField); + } catch (final Exception e) { + throw new RuntimeException(e); + } + } + + public SetFromMapSerializer(Fury fury, Class<Set<?>> type) { + super(fury, type, false); + } + + @Override + public Collection newCollection(MemoryBuffer buffer) { + int numElements = buffer.readVarUint32Small7(); + setNumElements(numElements); + final ClassInfo mapClassInfo = fury.getClassResolver().readClassInfo(buffer); + final MethodHandle methodHandle = ReflectionUtils.getCtrHandle(mapClassInfo.getCls(), true); Review Comment: I test this version code and graalvm test passed. ########## java/fury-core/src/main/java/org/apache/fury/serializer/collection/CollectionSerializers.java: ########## @@ -411,6 +415,66 @@ public ConcurrentSkipListSet newCollection(MemoryBuffer buffer) { } } + public static final class SetFromMapSerializer extends CollectionSerializer<Set<?>> { + + private static final long MAP_FIELD_OFFSET; + + static { + try { + Field mapField = Class.forName("java.util.Collections$SetFromMap").getDeclaredField("m"); + MAP_FIELD_OFFSET = Platform.objectFieldOffset(mapField); + } catch (final Exception e) { + throw new RuntimeException(e); + } + } + + public SetFromMapSerializer(Fury fury, Class<Set<?>> type) { + super(fury, type, false); + } + + @Override + public Collection newCollection(MemoryBuffer buffer) { Review Comment: I will take this way, but `Collections.newSetFromMap(map)` must use empty map(the check is in constructor method), so I have to use `Platform.putObject` to set data. I need learn more about `codegen` to make sure code is right. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
