Skip to content

Commit

Permalink
Move ScopedBytes into libcore/luni from libnativehelper
Browse files Browse the repository at this point in the history
The ScopedBytes class is only used in libcore and we can make it more
efficient if it uses a JniConstant for primitive byte array.

Previously it could not use caching in libnativehelper because it was
part of a header-only library and the lack of caching showed primitive
byte array as a hot class due to use of ScopedBytes in libcore.

Bug: 168471625
Test: atest CtsLibcoreTestCases:libcore.android.system.OsTest
Test: m checkbuild
Change-Id: I3fb268481da73a527d083eb1122a2e7d20d4faa5
  • Loading branch information
ohodson committed Sep 22, 2020
1 parent b7b3c8e commit 73caf18
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 6 deletions.
24 changes: 20 additions & 4 deletions NativeCode.bp
Original file line number Diff line number Diff line change
Expand Up @@ -232,18 +232,34 @@ cc_test {
// Add -fno-builtin so that the compiler doesn't attempt to inline
// memcpy calls that are not really aligned.
cflags: ["-fno-builtin"],
srcs: ["luni/src/test/native/libcore_io_Memory_test.cpp"],
srcs: [
"luni/src/test/native/libcore_io_Memory_test.cpp",
// libcore_io_Memory_test.cpp includes libcore_io_Memory.cpp which
// depends on JniConstants.cpp (but these are not used in the tests).
"luni/src/main/native/JniConstants.cpp",
],

shared_libs: ["libnativehelper"],
shared_libs: [
"liblog",
"libnativehelper",
],
}

// Set of benchmarks for libjavacore functions.
cc_benchmark {
name: "libjavacore-benchmarks",
defaults: ["core_native_default_flags"],

srcs: ["luni/src/benchmark/native/libcore_io_Memory_bench.cpp"],
srcs: [
"luni/src/benchmark/native/libcore_io_Memory_bench.cpp",
// libcore_io_Memory_bench.cpp includes libcore_io_Memory.cpp which
// depends on JniConstants.cpp (but these are not used in the benchmark).
"luni/src/main/native/JniConstants.cpp",
],
test_suites: ["device-tests"],

shared_libs: ["libnativehelper"],
shared_libs: [
"liblog",
"libnativehelper",
],
}
7 changes: 7 additions & 0 deletions luni/src/main/native/JniConstants.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ jclass localeDataClass;
jclass longClass;
jclass netlinkSocketAddressClass;
jclass packetSocketAddressClass;
jclass primitiveByteArrayClass;
jclass stringClass;
jclass structAddrinfoClass;
jclass structGroupReqClass;
Expand Down Expand Up @@ -105,6 +106,7 @@ void EnsureJniConstantsInitialized(JNIEnv* env) {
longClass = findClass(env, "java/lang/Long");
netlinkSocketAddressClass = findClass(env, "android/system/NetlinkSocketAddress");
packetSocketAddressClass = findClass(env, "android/system/PacketSocketAddress");
primitiveByteArrayClass = findClass(env, "[B");
stringClass = findClass(env, "java/lang/String");
structAddrinfoClass = findClass(env, "android/system/StructAddrinfo");
structGroupReqClass = findClass(env, "android/system/StructGroupReq");
Expand Down Expand Up @@ -223,6 +225,11 @@ jclass JniConstants::GetPacketSocketAddressClass(JNIEnv* env) {
return packetSocketAddressClass;
}

jclass JniConstants::GetPrimitiveByteArrayClass(JNIEnv* env) {
EnsureJniConstantsInitialized(env);
return primitiveByteArrayClass;
}

jclass JniConstants::GetStringClass(JNIEnv* env) {
EnsureJniConstantsInitialized(env);
return stringClass;
Expand Down
1 change: 1 addition & 0 deletions luni/src/main/native/JniConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ struct JniConstants {
static jclass GetLongClass(JNIEnv* env);
static jclass GetNetlinkSocketAddressClass(JNIEnv* env);
static jclass GetPacketSocketAddressClass(JNIEnv* env);
static jclass GetPrimitiveByteArrayClass(JNIEnv* env);
static jclass GetStringClass(JNIEnv* env);
static jclass GetStructAddrinfoClass(JNIEnv* env);
static jclass GetStructFlockClass(JNIEnv* env);
Expand Down
81 changes: 81 additions & 0 deletions luni/src/main/native/ScopedBytes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright (C) 2010 The Android Open Source Project
*
* Licensed 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.
*/

#pragma once

#include <nativehelper/JNIHelp.h>

#include "JniConstants.h"

/**
* ScopedBytesRO and ScopedBytesRW attempt to paper over the differences between byte[]s and
* ByteBuffers. This in turn helps paper over the differences between non-direct ByteBuffers backed
* by byte[]s, direct ByteBuffers backed by bytes[]s, and direct ByteBuffers not backed by byte[]s.
* (On Android, this last group only contains MappedByteBuffers.)
*/
template<bool readOnly>
class ScopedBytes {
public:
ScopedBytes(JNIEnv* env, jobject object)
: mEnv(env), mObject(object), mByteArray(nullptr), mPtr(nullptr)
{
if (mObject == nullptr) {
jniThrowNullPointerException(mEnv);
} else {
jclass byteArrayClass = JniConstants::GetPrimitiveByteArrayClass(env);
if (mEnv->IsInstanceOf(mObject, byteArrayClass)) {
mByteArray = reinterpret_cast<jbyteArray>(mObject);
mPtr = mEnv->GetByteArrayElements(mByteArray, nullptr);
} else {
mPtr = reinterpret_cast<jbyte*>(mEnv->GetDirectBufferAddress(mObject));
}
}
}

~ScopedBytes() {
if (mByteArray != nullptr) {
mEnv->ReleaseByteArrayElements(mByteArray, mPtr, readOnly ? JNI_ABORT : 0);
}
}

private:
JNIEnv* const mEnv;
const jobject mObject;
jbyteArray mByteArray;

protected:
jbyte* mPtr;

private:
DISALLOW_COPY_AND_ASSIGN(ScopedBytes);
};

class ScopedBytesRO : public ScopedBytes<true> {
public:
ScopedBytesRO(JNIEnv* env, jobject object) : ScopedBytes<true>(env, object) {}
const jbyte* get() const {
return mPtr;
}
};

class ScopedBytesRW : public ScopedBytes<false> {
public:
ScopedBytesRW(JNIEnv* env, jobject object) : ScopedBytes<false>(env, object) {}
jbyte* get() {
return mPtr;
}
};

2 changes: 1 addition & 1 deletion luni/src/main/native/libcore_io_Linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
#include <android-base/strings.h>
#include <log/log.h>
#include <nativehelper/JNIPlatformHelp.h>
#include <nativehelper/ScopedBytes.h>
#include <nativehelper/ScopedLocalRef.h>
#include <nativehelper/ScopedPrimitiveArray.h>
#include <nativehelper/ScopedUtfChars.h>
Expand All @@ -73,6 +72,7 @@
#include "JniException.h"
#include "NetworkUtilities.h"
#include "Portability.h"
#include "ScopedBytes.h"

#ifndef __unused
#define __unused __attribute__((__unused__))
Expand Down
3 changes: 2 additions & 1 deletion luni/src/main/native/libcore_io_Memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@
#include <sys/mman.h>

#include <nativehelper/JNIHelp.h>
#include <nativehelper/ScopedBytes.h>

#include <nativehelper/ScopedPrimitiveArray.h>
#include <nativehelper/jni_macros.h>

#include "JniConstants.h"
#include "Portability.h"
#include "ScopedBytes.h"

// Use packed structures for access to unaligned data on targets with alignment restrictions.
// The compiler will generate appropriate code to access these structures without
Expand Down

0 comments on commit 73caf18

Please sign in to comment.