Skip to content

Instantly share code, notes, and snippets.

View apangin's full-sized avatar

Andrei Pangin apangin

View GitHub Profile
/*
* Copyright The async-profiler authors
* SPDX-License-Identifier: Apache-2.0
*/
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Stream;
@apangin
apangin / JsonReader.java
Last active July 22, 2024 20:25
Simplified JSON reader
/*
* Copyright Andrei Pangin
* SPDX-License-Identifier: Apache-2.0
*/
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
/**
@apangin
apangin / Objects.java
Created May 23, 2024 23:22
Objects.hash alternative capable of allocation elimination
public static int hash(String... args) {
if (args != null) {
switch (args.length) {
case 1:
return 31 + Objects.hashCode(args[0]);
case 2:
return 961 + Objects.hashCode(args[0]) * 31 + args[1].hashCode();
case 3:
return 29791 + Objects.hashCode(args[0]) * 961 + args[1].hashCode() * 31 + args[2].hashCode();
}
import java.io.Serializable;
import java.util.concurrent.atomic.LongAdder;
public class ReaderThread extends Thread {
private static final LongAdder ops = new LongAdder();
interface Reader extends Serializable {
boolean advanceTo(int docId);
}
package bench;
import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import org.openjdk.jmh.annotations.Benchmark;
import java.util.HashMap;
public class JwtToken {
import sun.misc.Unsafe;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
package bench;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import java.util.Arrays;
import java.util.concurrent.ThreadLocalRandom;
@apangin
apangin / MapBench.java
Created July 29, 2021 16:27
ConcurrentHashMap vs. HashMap performance
package bench;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Param;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import java.util.HashMap;
import java.util.Map;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
// See https://twitter.com/tagir_valeev/status/1402089474805354499
public class StringBuilderHash {
import java.util.ArrayList;
import java.util.concurrent.locks.LockSupport;
import java.util.stream.StreamSupport;
public class ParallelStream {
public static int[] process(Iterable<Integer> iterable) {
return StreamSupport.stream(iterable.spliterator(), true)
.mapToInt(i -> {
LockSupport.parkNanos(1_000_000);