Reduce the default maximum heap size for all Scala processes. (#5350)

* bazel_tools: Set `unused_dependency_checker_mode` in one place.

* bazel_tools: Set the default max heap size for Scala processes to 2GB.

And the default initial max heap size to 512MB.

CHANGELOG_BEGIN
CHANGELOG_END

* bazel_tools: Set the `scalac` heap size to 2GB and stack size to 2MB.

* bazel_tools: Delete `da_scala_macro_library`, as it's unused.

* bazel_tools: Revert the description of `da_scala_library_suite`.

Misread it.
This commit is contained in:
Samir Talwar 2020-04-01 21:14:20 +02:00 committed by GitHub
parent c23564b0cc
commit 4b9b2e8e61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 88 additions and 68 deletions

View File

@ -498,17 +498,6 @@ Scala source file, and bundle them in one target. This rule takes the same
attributes as `da_scala_library` with the exception of
`unused_dependency_checker_mode` which will always be disabled.
If a Scala library defines macros then you must use the
`da_scala_macro_library` rule instead of the above. Otherwise, you will encounter
compiler errors of the following form (formatted for readability):
```
error: macro annotation could not be expanded (the most common reason
for that is that you need to enable the macro paradise plugin; another
possibility is that you try to use macro annotation in the same
compilation run that defines it)
```
#### Tests
Scala tests can be defined using the `da_scala_test` rule. It will generate an
@ -547,6 +536,10 @@ da_scala_test_suite(
# Expected runtime and resource requirements.
size = "small",
...
# You can adjust the heap size as follows:
initial_heap_size = "512m",
max_heap_size = "2g",
)
```
@ -554,6 +547,11 @@ The `size` attribute is used to determine the default timeout and resource
requirements. Refer to the [official documentation][bazel_test_size] for
details about test size and other common test attributes.
A couple of arguments have been added:
* `initial_heap_size` is translated to `-Xms`, and defaults to `512m`, and
* `max_heap_size` is translated to `-Xmx`, and defaults to `2g`.
#### Executables
Scala executables can be defined using the `da_scala_binary` rule. It will
@ -581,6 +579,10 @@ da_scala_binary(
# A list of files that should be present in the runtime path at runtime.
data = [ ... ],
...
# You can adjust the heap size as follows:
initial_heap_size = "512m",
max_heap_size = "2g",
)
```

View File

@ -807,12 +807,6 @@ da_scala_library(
)
```
### Scala Macro Libraries
If a Scala library defines macros that should be used by other Scala targets
later on, then it has to be defined using `da_scala_macro_library`. Macros may
not be defined and used within the same target.
### Scala Executables
Scala executables are defined using `da_scala_binary`. It takes most of the

View File

@ -6,7 +6,6 @@ load(
"scala_binary",
"scala_library",
"scala_library_suite",
"scala_macro_library",
"scala_test",
"scala_test_suite",
)
@ -121,6 +120,44 @@ lf_scalacopts = [
"-Ywarn-unused",
]
default_compile_arguments = {
"unused_dependency_checker_mode": "error",
}
default_initial_heap_size = "512m"
default_max_heap_size = "2g"
default_scalac_stack_size = "2m"
def _jvm_flags(initial_heap_size, max_heap_size):
return ["-Xms{}".format(initial_heap_size), "-Xmx{}".format(max_heap_size)]
def _set_compile_jvm_flags(
arguments,
initial_heap_size = default_initial_heap_size,
max_heap_size = default_max_heap_size,
scalac_stack_size = default_scalac_stack_size):
jvm_flags = _jvm_flags(initial_heap_size, max_heap_size)
result = {}
result.update(arguments)
result.update({
"scalac_jvm_flags": arguments.get("scalac_jvm_flags", []) + ["-Xss{}".format(scalac_stack_size)] + jvm_flags,
})
return result
def _set_jvm_flags(
arguments,
initial_heap_size = default_initial_heap_size,
max_heap_size = default_max_heap_size,
scalac_stack_size = default_scalac_stack_size):
jvm_flags = _jvm_flags(initial_heap_size, max_heap_size)
result = {}
result.update(arguments)
result.update({
"scalac_jvm_flags": arguments.get("scalac_jvm_flags", []) + ["-Xss{}".format(scalac_stack_size)] + jvm_flags,
"jvm_flags": arguments.get("jvm_flags", []) + jvm_flags,
})
return result
def _wrap_rule(rule, name = "", scalacopts = [], plugins = [], generated_srcs = [], **kwargs):
rule(
name = name,
@ -408,7 +445,7 @@ def _create_scaladoc_jar(**kwargs):
tags = ["scaladoc"],
)
def da_scala_library(name, unused_dependency_checker_mode = "error", **kwargs):
def da_scala_library(name, **kwargs):
"""
Define a Scala library.
@ -418,13 +455,16 @@ def da_scala_library(name, unused_dependency_checker_mode = "error", **kwargs):
[rules_scala_library_docs]: https://github.com/bazelbuild/rules_scala/blob/master/docs/scala_library.md
"""
kwargs["unused_dependency_checker_mode"] = unused_dependency_checker_mode
_wrap_rule(scala_library, name, **kwargs)
_create_scala_source_jar(name = name, **kwargs)
_create_scaladoc_jar(name = name, **kwargs)
arguments = {}
arguments.update(default_compile_arguments)
arguments.update(kwargs)
arguments = _set_compile_jvm_flags(arguments)
_wrap_rule(scala_library, name, **arguments)
_create_scala_source_jar(name = name, **arguments)
_create_scaladoc_jar(name = name, **arguments)
if "tags" in kwargs:
for tag in kwargs["tags"]:
if "tags" in arguments:
for tag in arguments["tags"]:
if tag.startswith("maven_coordinates="):
pom_file(
name = name + "_pom",
@ -442,30 +482,20 @@ def da_scala_library_suite(name, **kwargs):
[rules_scala_library_suite_docs]: https://github.com/bazelbuild/rules_scala/blob/master/docs/scala_library_suite.md
"""
_wrap_rule(scala_library_suite, name, **kwargs)
_create_scala_source_jar(name = name, **kwargs)
_create_scaladoc_jar(name = name, **kwargs)
arguments = {}
arguments.update(kwargs)
arguments = _set_compile_jvm_flags(arguments)
_wrap_rule(scala_library_suite, name, **arguments)
_create_scala_source_jar(name = name, **arguments)
_create_scaladoc_jar(name = name, **arguments)
if "tags" in kwargs:
for tag in kwargs["tags"]:
if "tags" in arguments:
for tag in arguments["tags"]:
if tag.startswith("maven_coordinates="):
fail("Usage of maven_coordinates in da_scala_library_suite is NOT supported", "tags")
break
def da_scala_macro_library(**kwargs):
"""
Define a Scala library that contains macros.
Applies common Scala options defined in `bazel_tools/scala.bzl`.
And forwards to `scala_macro_library` from `rules_scala`.
Refer to the [`rules_scala` documentation][rules_scala_docs].
[rules_scala_docs]: https://github.com/bazelbuild/rules_scala#scala_library
"""
_wrap_rule(scala_macro_library, **kwargs)
_create_scala_source_jar(**kwargs)
def da_scala_binary(name, unused_dependency_checker_mode = "error", **kwargs):
def da_scala_binary(name, initial_heap_size = default_initial_heap_size, max_heap_size = default_max_heap_size, **kwargs):
"""
Define a Scala executable.
@ -475,11 +505,14 @@ def da_scala_binary(name, unused_dependency_checker_mode = "error", **kwargs):
[rules_scala_docs]: https://github.com/bazelbuild/rules_scala#scala_binary
"""
kwargs["unused_dependency_checker_mode"] = unused_dependency_checker_mode
_wrap_rule(scala_binary, name, **kwargs)
arguments = {}
arguments.update(default_compile_arguments)
arguments.update(kwargs)
arguments = _set_jvm_flags(arguments, initial_heap_size = initial_heap_size, max_heap_size = max_heap_size)
_wrap_rule(scala_binary, name, **arguments)
if "tags" in kwargs:
for tag in kwargs["tags"]:
if "tags" in arguments:
for tag in arguments["tags"]:
if tag.startswith("maven_coordinates="):
pom_file(
name = name + "_pom",
@ -499,7 +532,7 @@ def da_scala_binary(name, unused_dependency_checker_mode = "error", **kwargs):
)
break
def da_scala_test(unused_dependency_checker_mode = "error", **kwargs):
def da_scala_test(initial_heap_size = default_initial_heap_size, max_heap_size = default_max_heap_size, **kwargs):
"""
Define a Scala executable that runs the unit tests in the given source files.
@ -509,10 +542,13 @@ def da_scala_test(unused_dependency_checker_mode = "error", **kwargs):
[rules_scala_docs]: https://github.com/bazelbuild/rules_scala#scala_test
"""
kwargs["unused_dependency_checker_mode"] = unused_dependency_checker_mode
_wrap_rule(scala_test, **kwargs)
arguments = {}
arguments.update(default_compile_arguments)
arguments.update(kwargs)
arguments = _set_jvm_flags(arguments, initial_heap_size = initial_heap_size, max_heap_size = max_heap_size)
_wrap_rule(scala_test, **arguments)
def da_scala_test_suite(**kwargs):
def da_scala_test_suite(initial_heap_size = default_initial_heap_size, max_heap_size = default_max_heap_size, **kwargs):
"""
Define a Scala test executable for each source file and bundle them into one target.
@ -522,7 +558,10 @@ def da_scala_test_suite(**kwargs):
[rules_scala_docs]: https://github.com/bazelbuild/rules_scala#scala_test_suite
"""
_wrap_rule(scala_test_suite, use_short_names = is_windows, **kwargs)
arguments = {}
arguments.update(kwargs)
arguments = _set_jvm_flags(arguments, initial_heap_size = initial_heap_size, max_heap_size = max_heap_size)
_wrap_rule(scala_test_suite, use_short_names = is_windows, **arguments)
# TODO make the jmh rule work with plugins -- probably
# just a matter of passing the flag in

View File

@ -50,11 +50,6 @@ da_scala_library(
# Plugins have to be specified as JARs.
"@maven//:org_spire_math_kind_projector_2_12",
],
# Bump stack size to avoid stack overflow in reflection.
scalac_jvm_flags = [
"-Xmx2G",
"-Xss2M",
],
visibility = [
"//visibility:public",
],

View File

@ -13,10 +13,6 @@ common_scalacopts = [
"CONFIG",
]
common_jvm_flags = [
"-Xmx2G",
]
da_scala_library(
name = "codegen",
srcs =
@ -32,7 +28,6 @@ da_scala_library(
# Plugins have to be specified as JARs.
"@maven//:org_spire_math_kind_projector_2_12",
],
scalac_jvm_flags = common_jvm_flags,
scalacopts = common_scalacopts,
tags = ["maven_coordinates=com.daml:codegen-scala:__VERSION__"],
visibility = [
@ -79,7 +74,6 @@ da_scala_test_suite(
name = "tests",
size = "small",
srcs = glob(["src/test/scala/**/*.scala"]),
jvm_flags = common_jvm_flags,
scalacopts = common_scalacopts,
deps = [
":codegen",

View File

@ -14,10 +14,6 @@ navigator_scalacopts = [
"-Xsource:2.13",
]
common_jvm_flags = [
"-Xmx2G",
]
# All frontend resource files.
# These come in a manually created JAR file, this rule is just wrapping it in
# a java_import, so that it is a valid target for the 'resources' property of