Commit 7ddfb4c3 authored by kenton@google.com's avatar kenton@google.com

Remove semi-broken Java thread-local builder freelist 'optimization'. Maybe…

Remove semi-broken Java thread-local builder freelist 'optimization'.  Maybe bring back later in optional form.
parent 68996fc8
...@@ -24,8 +24,6 @@ ...@@ -24,8 +24,6 @@
* Lite mode: The "optimize_for = LITE_RUNTIME" option causes the compiler * Lite mode: The "optimize_for = LITE_RUNTIME" option causes the compiler
to generate code which only depends libprotobuf-lite, which is much smaller to generate code which only depends libprotobuf-lite, which is much smaller
than libprotobuf but lacks descriptors, reflection, and some other features. than libprotobuf but lacks descriptors, reflection, and some other features.
* Put Builder objects on a freelist after build() is called, so they may be
reused later.
* Lots of style cleanups. * Lots of style cleanups.
Python Python
......
...@@ -38,82 +38,9 @@ import java.io.UnsupportedEncodingException; ...@@ -38,82 +38,9 @@ import java.io.UnsupportedEncodingException;
* those generated messages do not reside in the {@code protobuf} package. * those generated messages do not reside in the {@code protobuf} package.
* Others should not use this class directly. * Others should not use this class directly.
* *
* @author cyrusn@google.com (Cyrus Najmabadi) * @author kenton@google.com (Kenton Varda)
*/ */
public class Internal { public class Internal {
/**
* Implementation of a Queue designed to have as little overhead as possible.
* No guarantees are made as to the order you will get values back from the
* queue. Currently it is a Last-In-First-Out implementation, but that may
* change in the future.
*
* Duplicate values are allowed, as are null values.
*
* Not threadsafe.
*
* @author cyrusn@google.com (Cyrus Najmabadi)
*/
public static final class QuickQueue<T> {
@SuppressWarnings("unchecked")
private T[] array = (T[]) new Object[16];
private int size;
/**
* Adds a value to the queue.
*
* @param value The value to add to the queue.
*/
public void offer(final T value) {
if (size == array.length) {
// I'd like to use Arrays.copy here. However, it is currently
// unavailable
// on android. So, for now, we just use the tried and true arraycopy
// technique.
@SuppressWarnings("unchecked")
final T[] copy = (T[]) new Object[size * 2];
System.arraycopy(array, 0, copy, 0, array.length);
array = copy;
}
array[size++] = value;
}
/**
* Removes some previously added value to the queue, or {@code null} if the
* queue is empty.
*
* @return An existing value in the queue, or {@code null} if the queue is
* empty.
*/
public T poll() {
if (size == 0) {
return null;
}
final T result = array[--size];
// make sure we null out the entry so that we're not keeping anything
// alive unnecessarily.
array[size] = null;
return result;
}
}
/**
* Instances of this class will provide a unique {@code QuickQueue} to each
* thread that accesses it. Very useful for providing free lists without
* needing to take any locks.
*
* @author cyrusn@google.com (Cyrus Najmabadi)
*/
public static final class ThreadLocalQuickQueue<T>
extends ThreadLocal<QuickQueue<T>> {
@Override
protected QuickQueue<T> initialValue() {
return new QuickQueue<T>();
}
}
/** /**
* Helper called by generated code to construct default values for string * Helper called by generated code to construct default values for string
* fields. * fields.
......
...@@ -601,15 +601,6 @@ void MessageGenerator::GenerateBuilder(io::Printer* printer) { ...@@ -601,15 +601,6 @@ void MessageGenerator::GenerateBuilder(io::Printer* printer) {
} }
printer->Indent(); printer->Indent();
// By using a threadlocal queue, we do not have to worry about locking when
// accessing the queue. Current JDKs implement this very efficiently, using
// no locks themselves to acquire the value when needed.
printer->Print(
"private static final "
" com.google.protobuf.Internal.ThreadLocalQuickQueue<Builder> builders =\n"
" new com.google.protobuf.Internal.ThreadLocalQuickQueue<Builder>();\n"
"\n");
GenerateCommonBuilderMethods(printer); GenerateCommonBuilderMethods(printer);
if (HasGeneratedMethods(descriptor_)) { if (HasGeneratedMethods(descriptor_)) {
...@@ -637,10 +628,7 @@ void MessageGenerator::GenerateCommonBuilderMethods(io::Printer* printer) { ...@@ -637,10 +628,7 @@ void MessageGenerator::GenerateCommonBuilderMethods(io::Printer* printer) {
"private Builder() {}\n" "private Builder() {}\n"
"\n" "\n"
"private static Builder create() {\n" "private static Builder create() {\n"
" Builder builder = builders.get().poll();\n" " Builder builder = new Builder();\n"
" if (builder == null) {\n"
" builder = new Builder();\n"
" }\n"
" builder.result = new $classname$();\n" " builder.result = new $classname$();\n"
" return builder;\n" " return builder;\n"
"}\n" "}\n"
...@@ -718,7 +706,6 @@ void MessageGenerator::GenerateCommonBuilderMethods(io::Printer* printer) { ...@@ -718,7 +706,6 @@ void MessageGenerator::GenerateCommonBuilderMethods(io::Printer* printer) {
printer->Print( printer->Print(
" $classname$ returnMe = result;\n" " $classname$ returnMe = result;\n"
" result = null;\n" " result = null;\n"
" builders.get().offer(this);\n"
" return returnMe;\n" " return returnMe;\n"
"}\n" "}\n"
"\n", "\n",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment