Skip to content

Instantly share code, notes, and snippets.

@afs
Last active November 17, 2022 11:21
Show Gist options
  • Save afs/2549580caec51466955bd78ff93bc327 to your computer and use it in GitHub Desktop.
Save afs/2549580caec51466955bd78ff93bc327 to your computer and use it in GitHub Desktop.
JavaCC Buffer location strategy
/*
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
https://xgjvouic68.proxynodejs.usequeue.com/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 dev;
import java.util.function.Function;
public class BufferAlloc {
public static void main(String...args) {
run(1024);
run(2048);
run(2*2048);
run(64*1048);
run(1024*1024);
}
public static void run(int N) {
Function<Long, Long> f1 = x -> x + 2048;
Function<Long, Long> f2 = x -> (x == 2048) ? x+2048 : x+(x>>1);
run("Alloc +2k ", N, f1);
run("Alloc *1.5", N, f2);
System.out.println();
}
public static void run(String label, int N, Function<Long, Long> growth) {
long size = 2*1024;
long copy = 0;
for ( int i = 0 ; i < 1000 ; i++ ) {
if ( size >= N )
break;
copy = copy+size;
long x2 = growth.apply(size);
//System.out.printf("%,10d\n", x2);
size = x2;
}
System.out.printf("%s : N = %-,10d, Size = %-,10d Copy = %,d \n", label, N, size, copy);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment