sebastianhillig-db opened a new pull request, #59593:
URL: https://github.com/apache/spark/pull/59593
<!--
Thanks for sending a pull request! Here are some tips for you:
1. If this is your first time, please read our contributor guidelines:
https://spark.apache.org/contributing.html
2. Ensure you have added or run the appropriate tests for your PR:
https://spark.apache.org/developer-tools.html
3. If the PR is unfinished, add '[WIP]' in your PR title, e.g.,
'[WIP][SPARK-XXXX] Your PR title ...'.
4. Be sure to keep the PR description updated to reflect all changes.
5. Please write your PR title to summarize what this PR proposes.
6. If possible, provide a concise example to reproduce the issue for a
faster review.
7. If you want to add a new configuration, please read the guideline first
for naming configurations in
'common/utils/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
8. If you want to add or modify an error type or message, please read the
guideline first in
'common/utils/src/main/resources/error/README.md'.
-->
### What changes were proposed in this pull request?
<!--
Please clarify what changes you are proposing. The purpose of this section
is to outline the changes and how this PR fixes the issue.
If possible, please consider writing useful notes for better and faster
reviews in your PR. See the examples below.
1. If you refactor some codes with changing classes, showing the class
hierarchy will help reviewers.
2. If you fix some SQL features, you can provide some references of other
DBMSes.
3. If there is design documentation, please add the link.
4. If there is a discussion in the mailing list, please add the link.
-->
This PR makes `JavaUtils.isTesting()` look up the `SPARK_TESTING`
environment variable once, caching it in a `private static final boolean`,
instead of calling `System.getenv("SPARK_TESTING")` on every invocation. The
`spark.testing` system property is still read on every call, and the evaluation
order (env first, then property) is unchanged.
`Utils.isTesting` and `SparkEnvUtils.isTesting` delegate to this method, so
they benefit too.
### Why are the changes needed?
<!--
Please clarify why the changes are needed. For instance,
1. If you propose a new API, clarify the use case for a new API.
2. If you fix a bug, you can clarify why it is a bug.
-->
On Linux, `System.getenv(String)` goes through
`java.lang.ProcessEnvironment`, which wraps the name in a new byte[]-backed
`Variable` on every lookup, so each `isTesting()` call allocates (56 bytes per
call in the measurement below). `isTesting` is on hot paths. For example,
`AnalysisHelper.assertNotAnalysisRule` calls it from the `LogicalPlan`
overrides of `transformDownWithPruning` / `transformUpWithPruning` /
`transformAllExpressionsWithPruning`, which run once per plan node visited by
every transform. In a JFR allocation profile of driver-side query planning,
these call sites accounted for roughly 1.8% of driver allocation and about 1-2%
of CPU.
A JVM's environment cannot change after startup, so caching the env lookup
is safe. The system property cannot be cached: tests set and clear it at
runtime (`SparkTestSuite.beforeAll`, `BenchmarkBase`,
`KryoSerializerBenchmark`). The only code that mutates the process environment
reflectively (`SparkConnectClientSuite.withEnvs`, `JavaXmlSuite.setEnv`)
touches `SPARK_REMOTE` / `SPARK_LOCAL_IP`, not `SPARK_TESTING`. Code that sets
`SPARK_TESTING` for a child process via `ProcessBuilder.environment()` is
unaffected, because the child is a new JVM.
### Does this PR introduce _any_ user-facing change?
<!--
Note that it means *any* user-facing change including all aspects such as
new features, bug fixes, or other behavior changes. Documentation-only updates
are not considered user-facing changes.
If yes, please clarify the previous behavior and the change this PR proposes
- provide the console output, description and/or an example to show the
behavior difference if possible.
If possible, please also clarify if this is a user-facing change compared to
the released Spark versions or within the unreleased branches such as master.
If no, write 'No'.
-->
No.
### How was this patch tested?
<!--
If tests were added, say they were added here. Please make sure to add some
test cases that check the changes thoroughly including negative and positive
cases if possible.
If it was tested in a way different from regular unit tests, please clarify
how you tested step by step, ideally copy and paste-able, so that other
reviewers can test and check, and descendants can verify in the future.
If tests were not added, please describe why they were not added and/or why
it was difficult to add.
If benchmark tests were added, please run the benchmarks in GitHub Actions
for the consistent environment, and the instructions could accord to:
https://spark.apache.org/developer-tools.html#github-workflow-benchmarks.
-->
Existing tests:
```
build/sbt common-utils-java/test common-utils/test 'network-common/testOnly
*JavaUtilsSuite' catalyst/compile
```
All passed (common-utils-java: 25, common-utils: 121, JavaUtilsSuite: 2),
and catalyst compiled.
I also ran a manual allocation check (not committed): 1M calls on one
thread, measured with `ThreadMXBean.getThreadAllocatedBytes`, `SPARK_TESTING`
unset, JDK 17, after warm-up:
| | bytes / 1M calls | time / 1M calls |
|---|---|---|
| before (`System.getenv` + `System.getProperty`) | 56,000,000 (56 B/call) |
~25-43 ms |
| after | 0 | ~2-2.5 ms |
The same check confirmed that setting and clearing `spark.testing` at
runtime still flips the result (`false` / `true` / `false`), and that with
`SPARK_TESTING=1` the result is `true` as before.
### Was this patch authored or co-authored using generative AI tooling?
<!--
If generative AI tooling has been used in the process of authoring this
patch, please include the
phrase: 'Generated-by: ' followed by the name of the tool and its version.
If no, write 'No'.
Please refer to the [ASF Generative Tooling
Guidance](https://www.apache.org/legal/generative-tooling.html) for details.
-->
Generated-by: Isaac (Claude Opus 5.5)
This pull request and its description were written by Isaac.
--
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]