-
Notifications
You must be signed in to change notification settings - Fork 303
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NegativeArraySizeException on @Tag(Integer.MAX_VALUE) #81
Comments
Avoid using large numbers as tags. |
I see implementation, it uses arrays by default, that is the wrong approach of utilizing memory. Otherwise, protostuff needs to check the largest field number with some border and use the Map algorithm. |
Well you can add a new annotation which marks the entity as having "sparse" field numbers. |
There is a standard approach in building software where any dumb value have to be processed correctly. If Integer.MAX_VALUE is not correct, it have to be IllegalArgumentException, but not the NegativeArraySizeException. Second thing is the software efficiency. We know that array is faster, but for big tag numbers it consumes more memory, and users are not expecting this. For example, Java sort has two implementations depending on sorting size. Also, David, can you look on this repo https://github.com/inwebby/protostuff-runtime-proto, it is the converter from RuntimeSchema to .proto files. |
Protobuf documentation says:
We can optionally check this range and throw |
That will be a right approach to validate tag numbers. |
The |
Here is another test case, when we get Add to pom.xml <dependency>
<groupId>org.openjdk.jol</groupId>
<artifactId>jol-core</artifactId>
<version>0.3</version>
<scope>test</scope>
</dependency> package io.protostuff.runtime;
import org.junit.Test;
import org.openjdk.jol.info.GraphLayout;
import io.protostuff.Tag;
import static io.protostuff.runtime.RuntimeSchema.createFrom;
import static org.openjdk.jol.info.GraphLayout.parseInstance;
/**
* @author Konstantin Shchepanovskyi
*/
public class RuntimeSchemaBigTagValueTest
{
@Test
public void testOutOfMemory() throws Exception
{
long sizeWhenTagIs1 = parseInstance(createFrom(A1.class)).totalSize();
System.out.println(sizeWhenTagIs1);
long sizeWhenTagIs1000 = parseInstance(createFrom(A1000.class)).totalSize();
System.out.println(sizeWhenTagIs1000);
long sizeWhenTagIs1000_000 = parseInstance(createFrom(A1000_000.class)).totalSize();
System.out.println(sizeWhenTagIs1000_000);
long sizeWhenTagIs1000_000_000 = parseInstance(createFrom(A1000_000_000.class)).totalSize();
System.out.println(sizeWhenTagIs1000_000_000);
}
static class A1
{
@Tag(1)
private int x;
}
static class A1000
{
@Tag(1000)
private int x;
}
static class A1000_000
{
@Tag(1000_000)
private int x;
}
static class A1000_000_000
{
@Tag(1000_000_000)
private int x;
}
} Result:
|
Solution1: Solution2: In any case we need fast |
Does not work big tag number.
Caused by: java.lang.NegativeArraySizeException
at com.dyuproject.protostuff.runtime.MappedSchema.(MappedSchema.java:84)
at com.dyuproject.protostuff.runtime.RuntimeSchema.(RuntimeSchema.java:331)
at com.dyuproject.protostuff.runtime.RuntimeSchema.createFrom(RuntimeSchema.java:249)
at com.dyuproject.protostuff.runtime.RuntimeSchema.createFrom(RuntimeSchema.java:156)
at com.dyuproject.protostuff.runtime.IdStrategy.newSchema(IdStrategy.java:129)
at com.dyuproject.protostuff.runtime.DefaultIdStrategy$Lazy.getSchema(DefaultIdStrategy.java:630)
... 26 more
The text was updated successfully, but these errors were encountered: