Oracle이 고려 중인 Java 9의 Unsafe API 제거 계획

성능이 중요한 꽤 많은 자바 프로젝트 (하둡 등 데이터 처리 프로젝트들 역시)이 Java Unsafe API에 의존하고 있다. Unsafe API는 JVM에서 공식적으로 제공하는 API가 아닌 Oracle JDK에서 내부적인 사용을 목적으로 제공하는 API이다. JNI와 다른 기술이며 콜 오버헤드 없이 직접 native 코드로 실행된기 때문에 빠르고, C 와 같이 메모리를 동적할당할 수 있으며 bounding check 없는 배열 접근 등 다소 위험하지만 성능 좋은 API를 100여가지 제공한다.

붙인 링크는 Oracle에서 JVM9 에서 Java Unsafe API 정말 제거하려는 계획과 지워질 경우 일어날 재앙에 대해서 언급한다. 아직까지는 계획일 뿐이고 계획을 직접 훑어보니 어느 정도의 대체 API도 고려하는 것 같기는 하다. 그럼에도 불구하고 그런일이 실제로 일어난다면 많은 자바 프로젝트들은 큰 변화를 겪어야 할 수 도 있다. 어쩌면 자바로 작성한 것이 의미가 없어질 정도로.. 어떤 프로젝트들은 헤비한 JNI 사용을 해야 할 것 이며 어떤 프로젝트들은 C++이나 기타 시스템 프로그래밍 언어로 이동을 해야 할지도 모르겠다.


Java Universal Network/Graph Framework

Recently, I’m primarily concerned with large-scale graph data processing. Occasionally, the visualization of graph can be a good way for us to observe some properties from graph data sets. Today, I’m going to introduce a graph framework, called Java Universal Network/Graph Framework (Jung). Jung provides data structures for graph, a programming interface familiar with graph features, some fundamental graph algorithms (e.g., minimum spanning tree, depth-first search, breath-first search, and dijkstra algorithm), and even visualization methods. Especially, I’m interested in its visualization methods.

The following java source shows the programming interface of Jung. In more detail, this program make a graph, add three vertices to the graph, and connect vertices. This source code is brought from Jung tutorial. As you can see, Jung’s APIs are very easy.

  // Make a graph by a SparseMultigraph instance.
  Graph<Integer, String> g = new SparseMultigraph<Integer, String>();
  g.addVertex((Integer)1); // Add a vertex with an integer 1
  g.addVertex((Integer)2);
  g.addVertex((Integer)3);
  g.addEdge("Edge-A", 1,3); // Added an edge to connect between 1 and 3 vertices.
  g.addEdge("Edge-B", 2,3, EdgeType.DIRECTED);
  g.addEdge("Edge-C", 3, 2, EdgeType.DIRECTED);
  g.addEdge("Edge-P", 2,3); // A parallel edge

  // Make some objects for graph layout and visualization.
  Layout<Integer, String> layout = new KKLayout<Integer, String>(g);
  BasicVisualizationServer<Integer, String> vv =
  new BasicVisualizationServer<Integer, String>(layout);
  vv.setPreferredSize(new Dimension(800,800));

  // It determine how each vertex with its value is represented in a diagram.
  ToStringLabeller<Integer> vertexPaint = new ToStringLabeller<Integer>() {
    public String transform(Integer i) {
    return ""+i;
   }
  };

  vv.getRenderContext().setVertexLabelTransformer(vertexPaint);

  JFrame frame = new JFrame("Simple Graph View");
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  frame.getContentPane().add(vv);
  frame.pack();
  frame.setVisible(true);

Some APIs of the Jung are based on generic programming, so you can use easily vertices or edges to contains user-defined data. If you want more detail information, visit http://jung.sourceforge.net.

The above source code shows the following diagram.
Jung example


Zipf Distribution Random Generator in Java

When I carry out some experiments, I usually make synthetic data sets generated by  some probability distributions.  Especially, Zipf distribution is frequently used for a synthetic data set. Zipf distribution is  one of the discrete power law probability distributions. You can get detail information from Zipf’s law in Wikipedia. Anyway, I attached my own java class for zip distribution. Below graphs are generated by my own java code and the gnuplot.

Zipf Distribution (s=1)Zipf Distribution with log scale (s=1)
import java.util.Random;

public class ZipfGenerator {
 private Random rnd = new Random(System.currentTimeMillis());
 private int size;
 private double skew;
 private double bottom = 0;

 public ZipfGenerator(int size, double skew) {
  this.size = size;
  this.skew = skew;

  for(int i=1;i < size; i++) {
  this.bottom += (1/Math.pow(i, this.skew));
  }
 }

 // the next() method returns an random rank id.
 // The frequency of returned rank ids are follows Zipf distribution.
 public int next() {
   int rank;
   double friquency = 0;
   double dice;

   rank = rnd.nextInt(size);
   friquency = (1.0d / Math.pow(rank, this.skew)) / this.bottom;
   dice = rnd.nextDouble();

   while(!(dice &lt; friquency)) {
     rank = rnd.nextInt(size);
     friquency = (1.0d / Math.pow(rank, this.skew)) / this.bottom;
     dice = rnd.nextDouble();
   }

   return rank;
 }

 // This method returns a probability that the given rank occurs.
 public double getProbability(int rank) {
   return (1.0d / Math.pow(rank, this.skew)) / this.bottom;
 }

 public static void main(String[] args) {
   if(args.length != 2) {
     System.out.println("usage: ./zipf size skew");
     System.exit(-1);
   }

   ZipfGenerator zipf = new ZipfGenerator(Integer.valueOf(args[0]),
   Double.valueOf(args[1]));
   for(int i=1;i <= 100; i++)
     System.out.println(i+" "+zipf.getProbability(i));
 }
}

Follow

Get every new post delivered to your Inbox.