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

fix: Minor QoL clean up #175

Merged
merged 4 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/.idea/
/.vscode/

# Ignore Gradle project-specific cache directory
.gradle
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.pyroscope.javaagent.impl;

import io.pyroscope.http.Format;
import io.pyroscope.javaagent.EventType;
import io.pyroscope.javaagent.Snapshot;
import io.pyroscope.javaagent.api.Exporter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.pyroscope.labels;

import java.util.Objects;
import java.util.concurrent.atomic.AtomicLong;

class Ref<T> {
Expand All @@ -16,13 +17,16 @@ public Ref(T val, Long id) {
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Ref<T> valueRef = (Ref<T>) o;
return id.equals(valueRef.id);

Ref<?> valueRef = (Ref<?>) o;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note this uses ? instead of T. I gather that ? is somewhat safer when casting generic classes.

if (val.getClass() != o.getClass()) return false;

return id.equals(valueRef.id) && val.equals(valueRef.val);
}

@Override
public int hashCode() {
return id.hashCode();
return Objects.hash(id, val);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure we want this change. The value here can be a string or a container type (we seem to be using maps as T), and if it is a container type we'll end up doing a deep traversal for equals() and hashCode() as opposed to a simple integer comparison / hashing which can become bad for performance.

Semantically, ids should be unique and generated based on a similar (and somewhat expensive) map lookup, so we should be fine with using the id only.

@korniltsev can you confirm this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In 32f2f77 I reverted this change. I did this in the interest of keeping the changes in this PR non-controversial. I mostly wanted this PR to clean up a few untidy spots in the code base.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@korniltsev can you confirm this?

I confirm the id already represents the contents of the value which is uniquely stored in a mp Aleks pointed. It will not hurt to hash/equals the value, but it is also not needed.

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,14 @@

import one.profiler.AsyncProfiler;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class PyroscopeAsyncProfiler {
static final String libraryPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ void stressTest() throws InterruptedException {
}
e.shutdown();
e.awaitTermination(100, TimeUnit.SECONDS);
Snapshot res = Pyroscope.LabelsWrapper.dump();
Pyroscope.LabelsWrapper.dump();
assertEquals(0, ScopedContext.context.get().id);
assertEquals(0, RefCounted.strings.valueToRef.size());
assertEquals(0, RefCounted.contexts.valueToRef.size());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package io.pyroscope.labels;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class RefTest {
@Test
void testCreateRef() {
Ref<String> ref = new Ref<>("test", 1L);
assertEquals("test", ref.val);
assertEquals(1L, ref.id);
assertEquals(1L, ref.refCount.get());
}

@Test
@SuppressWarnings("unlikely-arg-type")
void testEquals() {
Ref<String> ref1 = new Ref<>("test", 1L);
assertTrue(ref1.equals(ref1));
assertFalse(ref1.equals(null));
assertFalse(ref1.equals(new Integer(3)));
assertFalse(ref1.equals(new Ref<Integer>(3, 1L)));
}

@Test
void testHashCode() {
Ref<String> ref1 = new Ref<>("test", 1L);
assertEquals(3557490, ref1.hashCode());
}
}
2 changes: 0 additions & 2 deletions demo/src/main/java/Fib.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

Expand Down
Loading