Return-Path: X-Original-To: apmail-flink-issues-archive@minotaur.apache.org Delivered-To: apmail-flink-issues-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 26B46175C9 for ; Mon, 2 Feb 2015 19:29:13 +0000 (UTC) Received: (qmail 93492 invoked by uid 500); 2 Feb 2015 19:29:07 -0000 Delivered-To: apmail-flink-issues-archive@flink.apache.org Received: (qmail 93446 invoked by uid 500); 2 Feb 2015 19:29:07 -0000 Mailing-List: contact issues-help@flink.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@flink.apache.org Delivered-To: mailing list issues@flink.apache.org Received: (qmail 93437 invoked by uid 99); 2 Feb 2015 19:29:07 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 02 Feb 2015 19:29:07 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED,T_RP_MATCHES_RCVD X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO mail.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with SMTP; Mon, 02 Feb 2015 19:28:45 +0000 Received: (qmail 92077 invoked by uid 99); 2 Feb 2015 19:28:42 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 02 Feb 2015 19:28:42 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id B8874E035D; Mon, 2 Feb 2015 19:28:42 +0000 (UTC) From: hsaputra To: issues@flink.incubator.apache.org Reply-To: issues@flink.incubator.apache.org References: In-Reply-To: Subject: [GitHub] flink pull request: [FLINK-1320] Add an off-heap variant of the ma... Content-Type: text/plain Message-Id: <20150202192842.B8874E035D@git1-us-west.apache.org> Date: Mon, 2 Feb 2015 19:28:42 +0000 (UTC) X-Virus-Checked: Checked by ClamAV on apache.org Github user hsaputra commented on a diff in the pull request: https://github.com/apache/flink/pull/290#discussion_r23950666 --- Diff: flink-core/src/main/java/org/apache/flink/core/memory/DirectMemorySegment.java --- @@ -0,0 +1,595 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.core.memory; + +import java.io.DataInput; +import java.io.DataOutput; +import java.io.IOException; +import java.lang.reflect.Field; +import java.nio.BufferOverflowException; +import java.nio.BufferUnderflowException; +import java.nio.ByteBuffer; + +/** + * This class uses in parts code from Java's direct byte buffer API. + * + * The use in this class two crucial additions: + * - It uses collapsed checks for range check and memory segment disposal. + * - It offers absolute positioning methods for byte array put/get methods, to guarantee thread safe use. + * + * In addition, the code that uses this class should make sure that only one implementation class is ever loaded - + * Either the {@link HeapMemorySegment}, or this DirectMemorySegment. That way, all the abstract methods in the + * MemorySegment base class have only one loaded actual implementation. This is easy for the JIT to recognize through + * class hierarchy analysis, or by identifying that the invocations are monomorphic (all go to the same concrete + * method implementation). Under this precondition, the JIT can perfectly inline methods. + * + * This is harder to do and control with byte buffers, where different code paths use different versions of the class + * (heap, direct, mapped) and thus virtual method invocations are polymorphic and are not as easily inlined. + */ +public class DirectMemorySegment extends MemorySegment { + + /** The direct byte buffer that allocated the memory */ + protected final ByteBuffer buffer; + + /** The address to the off-heap data */ + private long address; + + /** The address one byte after the last addressable byte. + * This is address + size while the segment is not disposed */ + private final long addressLimit; + + /** The size in bytes of the memory segment */ + private final int size; + + // ------------------------------------------------------------------------- + // Constructors + // ------------------------------------------------------------------------- + + public DirectMemorySegment(int size) { + this(ByteBuffer.allocateDirect(size)); + } + + public DirectMemorySegment(ByteBuffer buffer) { + if (buffer == null || !buffer.isDirect()) { + throw new IllegalArgumentException(); --- End diff -- Put message to the exception to help diagnose problem. --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastructure@apache.org or file a JIRA ticket with INFRA. ---