Skip to content

Commit 135ab0c

Browse files
authored
perf(brush): drop the shared securerandom from exported brushes (#41)
Claude-Session: https://claude.ai/code/session_01JavEedb4MyR9NehY3HusTn
1 parent 0ef0edf commit 135ab0c

2 files changed

Lines changed: 61 additions & 4 deletions

File tree

‎src/main/java/net/onelitefeather/bettergopaint/brush/ExportedPlayerBrush.java‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626
import org.jetbrains.annotations.NotNull;
2727
import org.jetbrains.annotations.Nullable;
2828

29-
import java.security.SecureRandom;
3029
import java.util.Collections;
3130
import java.util.List;
3231
import java.util.Random;
32+
import java.util.concurrent.ThreadLocalRandom;
3333

3434
public record ExportedPlayerBrush(
3535
@NotNull Brush brush,
@@ -47,8 +47,6 @@ public record ExportedPlayerBrush(
4747
double angleHeightDifference
4848
) implements BrushSettings {
4949

50-
private static final Random RANDOM = new SecureRandom();
51-
5250
public ExportedPlayerBrush(@NotNull Builder builder) {
5351
this(
5452
builder.brush,
@@ -84,7 +82,7 @@ public boolean maskEnabled() {
8482

8583
@Override
8684
public @NotNull Random random() {
87-
return RANDOM;
85+
return ThreadLocalRandom.current();
8886
}
8987

9088
public static Builder builder(@NotNull Brush brush, @NotNull Settings settings) {
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* goPaint is designed to simplify painting inside of Minecraft.
3+
* Copyright (C) Arcaniax-Development
4+
* Copyright (C) Arcaniax team and contributors
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
package net.onelitefeather.bettergopaint.brush;
20+
21+
import net.onelitefeather.bettergopaint.menu.BrushMenuFactory;
22+
import net.onelitefeather.bettergopaint.objects.brush.Brush;
23+
import net.onelitefeather.bettergopaint.settings.Settings;
24+
import net.onelitefeather.bettergopaint.settings.SettingsHolder;
25+
import org.junit.jupiter.api.DisplayName;
26+
import org.junit.jupiter.api.Test;
27+
28+
import java.security.SecureRandom;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
import static org.mockito.Mockito.mock;
32+
33+
class ExportedPlayerBrushRandomTest {
34+
35+
private final Settings settings = Settings.defaults();
36+
private final PlayerBrushManager manager = new PlayerBrushManager(SettingsHolder.of(settings), mock(BrushMenuFactory.class));
37+
private final Brush disc = manager.getBrushByName("Disc Brush").orElseThrow();
38+
39+
@Test
40+
@DisplayName("the jitter source of an exported brush is not a cryptographic generator")
41+
void jitterSourceIsNotSecureRandom() {
42+
ExportedPlayerBrush brush = ExportedPlayerBrush.builder(disc, settings).build();
43+
44+
assertThat(brush.random())
45+
.as("brush jitter needs no cryptographic strength, a SecureRandom is a synchronized hot spot")
46+
.isNotInstanceOf(SecureRandom.class);
47+
}
48+
49+
@Test
50+
@DisplayName("exported brushes built from the same values are equal")
51+
void exportedBrushesBuiltFromSameValuesAreEqual() {
52+
ExportedPlayerBrush first = ExportedPlayerBrush.builder(disc, settings).size(3).chance(50).build();
53+
ExportedPlayerBrush second = ExportedPlayerBrush.builder(disc, settings).size(3).chance(50).build();
54+
55+
assertThat(first)
56+
.as("record equality must depend only on the brush values, not on a randomness source")
57+
.isEqualTo(second);
58+
}
59+
}

0 commit comments

Comments
 (0)