[foreign-preview] RFR: 8282070: Drop workaround from memory segment implementation
Radoslaw Smogura
duke at openjdk.java.net
Thu Feb 17 22:46:13 UTC 2022
On Thu, 17 Feb 2022 22:05:24 GMT, Radoslaw Smogura <duke at openjdk.java.net> wrote:
>> Consider a case of a segment whose length is Long.MAX_VALUE. Then you want to take a slice of size 0. This will throw an exception, as Long.MAX_VALUE + 1 will flip into negative territory.
>>
>> Another problem is when the segment has size zero, in which case the check:
>>
>>
>> long checkedOffset = Objects.checkIndex(offset, this.length);
>>
>>
>> Will also fail. To make that pass we'd need to tweak that check to use `this.length + 1` again, and risk overflow. So I figured it was better just just special case when the accessed length was zero (which should happen rarely). Another option would be to have different checks for slicing and for access (since for access length can never be zero) - but then it duplicates the implementation, and there are also methods like `copy` which are left a bit in the middle.
>>
>> As for inlining, I doubt that it would make a difference to call Preconditions directly, since it's all `@ForceInline`d.
>
> I get it, it's to allow creating 0-length chunks (I've thought in context of reading value).
>
> If we assume `length > 0`, then `this.length - length + 1 =< this.length`, (in case `this.len == 0 -> 0 <= 0`)
I'm not sure if I missed some case, but if it's in if (`length > 0`), then one `checkIndex` is redundant? Unless we want to disallow slicing 0 length indices?
-------------
PR: https://git.openjdk.java.net/panama-foreign/pull/644
More information about the panama-dev
mailing list