Skip to content

Instantly share code, notes, and snippets.

View amaembo's full-sized avatar

Tagir Valeev amaembo

View GitHub Profile
@amaembo
amaembo / Benchmark.java
Created October 24, 2023 10:09
Benchmark puzzle
import java.util.function.IntSupplier;
import java.util.stream.IntStream;
public class Benchmark {
private int compute() {
IntSupplier s1 = () -> IntStream.range(0, 10000).map(v -> 1).sum();
IntSupplier s2 = () -> IntStream.range(0, 10000).map(v -> 1).sum();
IntSupplier s3 = () -> IntStream.range(0, 10000).map(v -> 1).sum();
return s1.getAsInt() + s2.getAsInt() + s3.getAsInt();
}
Index: src/java.base/share/classes/java/lang/AbstractStringBuilder.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/java.base/share/classes/java/lang/AbstractStringBuilder.java b/src/java.base/share/classes/java/lang/AbstractStringBuilder.java
--- a/src/java.base/share/classes/java/lang/AbstractStringBuilder.java (revision 674fa32cf8403ceec84bbe2c3f9404d612c10797)
+++ b/src/java.base/share/classes/java/lang/AbstractStringBuilder.java (date 1674682743075)
@@ -1680,8 +1680,8 @@
return;
@amaembo
amaembo / MemoryTest.java
Created October 25, 2021 10:04
MemoryTest sample
import java.util.ArrayList;
public class MemoryTest {
public static void main(String[] args) {
var processors = new ArrayList<Processor>();
for (int i = 0; i < 1000; i++) {
int[] data = new int[10_000_000];
processors.add(new Processor(data) {
@Override
int calculate() {
@amaembo
amaembo / stream_chain.svg
Created September 4, 2021 08:22
Stream Chain visualization
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@amaembo
amaembo / Test.java
Created August 19, 2021 03:40
StreamEx reducingWithZero demo
import one.util.streamex.MoreCollectors;
import one.util.streamex.StreamEx;
import java.util.stream.Collector;
class Test {
enum MyLattice {
/*
TOP
/ \
@amaembo
amaembo / InnerClassLambdaMetafactory.java.patch
Created July 21, 2021 17:08
Patch for InnerClassLambdaMetafactory to generate line number debug info
diff --git a/src/java.base/share/classes/java/lang/invoke/InnerClassLambdaMetafactory.java b/src/java.base/share/classes/java/lang/invoke/InnerClassLambdaMetafactory.java
--- a/src/java.base/share/classes/java/lang/invoke/InnerClassLambdaMetafactory.java (revision 57611b30219191160f7faccb811b41a31c25c0b8)
+++ b/src/java.base/share/classes/java/lang/invoke/InnerClassLambdaMetafactory.java (date 1626887157914)
@@ -315,6 +315,18 @@
return generateInnerClass();
}
+ private static StackTraceElement getCallerFrame() {
+ StackTraceElement[] trace = new Exception().getStackTrace();
+ for (int i = 0; i < trace.length - 1; i++) {
@amaembo
amaembo / Circle.java
Created July 19, 2021 09:28
Bresenham circle algorithm
public class Circle {
public static void main(String[] args) {
// Preparation
int radius = Integer.parseInt(args[0]);
int rasterSize = radius * 2 + 1;
boolean[][] raster = new boolean[rasterSize][rasterSize];
// Bresenham algorithm
int y = radius;
int err = radius;
import java.util.*;
import java.util.stream.*;
public class StringBuilderInHashMap {
public static void main(String[] args) {
List<StringBuilder> list = Stream.generate(() -> {
while (true) {
StringBuilder sb = new StringBuilder("a");
int hc = sb.hashCode();
if (((hc ^ (hc >>> 16)) & 0x3F) == 0) {
@amaembo
amaembo / BreakHashSet.java
Created March 22, 2021 02:30
Break HashSet with incorrect compareTo implementation
import java.util.*;
class BreakHashSet {
public static void main(String[] args) {
Point[] points = new Point[20];
Arrays.setAll(points, idx -> new Point(idx * 500_000_000,
-new Point(idx * 500_000_000, 0).hashCode()));
Set<Point> set = new HashSet<>(Arrays.asList(points));
set.remove(points[1]);
System.out.println(set.contains(points[14])); // Prints false!
@amaembo
amaembo / WhoInheritsComparator.java
Created December 14, 2020 09:55
Which collection inherits comparator?
import java.util.Collection;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.concurrent.PriorityBlockingQueue;
public class WhoInheritsComparator {
public static void main(String[] args) {
SortedSet<Integer> c1 = new TreeSet<>(Comparator.reverseOrder());