Skip to content
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

Check target array length before allocating. #4159

Open
wants to merge 4 commits into
base: series/3.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright 2020-2024 Typelevel
*
* Licensed 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 cats.effect.benchmarks

import cats.effect.ArrayStack

import org.openjdk.jmh.annotations._

import java.util.concurrent.TimeUnit

/**
* To do comparative benchmarks between versions:
*
* benchmarks/run-benchmark ArrayStackBenchmark
*
* This will generate results in `benchmarks/results`.
*
* Or to run the benchmark from within sbt:
*
* Jmh / run -i 10 -wi 10 -f 2 -t 1 cats.effect.benchmarks.ArrayStackBenchmark
*
* Which means "10 iterations", "10 warm-up iterations", "2 forks", "1 thread". Please note that
* benchmarks should be usually executed at least in 10 iterations (as a rule of thumb), but
* more is better.
*/
@State(Scope.Thread)
@BenchmarkMode(Array(Mode.Throughput))
@OutputTimeUnit(TimeUnit.SECONDS)
class ArrayStackBenchmark {

@Param(Array("1000000"))
var size: Int = _

@Benchmark
def push(): Unit = {
val stack: ArrayStack[Integer] = ArrayStack[Integer](size)

var i = 0
while (i < size) {
stack.push(i)
i += 1
}
}
}
16 changes: 14 additions & 2 deletions core/jvm-native/src/main/scala/cats/effect/ArrayStack.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

package cats.effect

import PlatformStatics.VM_MaxArraySize

import Platform.static

private final class ArrayStack[A <: AnyRef](
private[effect] final class ArrayStack[A <: AnyRef](
private[this] var buffer: Array[AnyRef],
private[this] var index: Int) {

Expand Down Expand Up @@ -72,7 +74,17 @@ private final class ArrayStack[A <: AnyRef](
private[this] def checkAndGrow(): Unit =
if (index >= buffer.length) {
val len = buffer.length
val buffer2 = new Array[AnyRef](len * 2)
val targetLen = len * 2

val resizeLen =
if (targetLen < 0)
throw new Exception(s"Overflow while resizing array. Request length: $targetLen")
else if (len > VM_MaxArraySize / 2)
VM_MaxArraySize
else
targetLen

val buffer2 = new Array[AnyRef](resizeLen)
System.arraycopy(buffer, 0, buffer2, 0, len)
buffer = buffer2
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ import scala.annotation.tailrec

import java.util.concurrent.atomic.{AtomicBoolean, AtomicReference}

import CallbackStack.Handle
import CallbackStack.Node
import CallbackStack.{Handle, Node}
import Platform.static

private final class CallbackStack[A](private[this] var callback: A => Unit)
Expand Down
24 changes: 24 additions & 0 deletions core/jvm-native/src/main/scala/cats/effect/PlatformStatics.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright 2020-2024 Typelevel
*
* Licensed 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 cats.effect

// copy from scala.runtime.PStatics
private[effect] object PlatformStatics {
// `Int.MaxValue - 8` traditional soft limit to maximize compatibility with diverse JVMs
// See https://stackoverflow.com/a/8381338 for example
final val VM_MaxArraySize = 2147483639
}
3 changes: 2 additions & 1 deletion core/shared/src/main/scala/cats/effect/IO.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import cats.{
StackSafeMonad,
Traverse
}
import cats.data.Ior
import cats.effect.instances.spawn
import cats.effect.kernel.CancelScope
import cats.effect.kernel.GenTemporal.handleDuration
Expand All @@ -57,6 +56,8 @@ import java.util.concurrent.Executor

import Platform.static

import cats.data.Ior

/**
* A pure abstraction representing the intention to perform a side effect, where the result of
* that side effect may be obtained synchronously (via return) or asynchronously (via callback).
Expand Down
Loading