-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDemo.java
More file actions
124 lines (104 loc) · 6.22 KB
/
Copy pathDemo.java
File metadata and controls
124 lines (104 loc) · 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package examples;
import fastmemory.Memory;
import fastpointer.Pointer;
public class Demo {
public static void main(String[] args) {
System.out.println("=== FastMemory 0.1.0 - SIMD-Aligned Memory Demo ===");
System.out.println("High-performance off-heap memory with alignment and page locking");
// Real-world scenario: 4K video frame buffer (3840x2160 pixels, 4 bytes per pixel)
int width = 3840;
int height = 2160;
int bytesPerPixel = 4; // RGBA
long frameBufferSize = (long) width * height * bytesPerPixel;
System.out.println("\nScenario: 4K Video Frame Buffer");
System.out.println("Resolution: " + width + "x" + height);
System.out.println("Frame buffer size: " + (frameBufferSize / 1024 / 1024) + " MB");
// ===== FASTMEMORY APPROACH (SIMD-Aligned + Page Locked) =====
System.out.println("\n--- FastMemory (SIMD-Aligned + Page Locked) ---");
long startTime = System.nanoTime();
long allocTime = 0;
long writeTime = 0;
long readTime = 0;
// Allocate 32-byte SIMD-aligned memory for AVX2 vector operations
try (Memory frameBuffer = Memory.allocateAligned(frameBufferSize, 32)) {
allocTime = System.nanoTime() - startTime;
System.out.println("Allocation time: " + (allocTime / 1_000_000) + " ms");
System.out.printf("Address: 0x%016X (32-byte aligned)%n", frameBuffer.address());
// Lock physical RAM to prevent OS swapping during real-time video processing
startTime = System.nanoTime();
boolean pageLocked = frameBuffer.lockPages();
long lockTime = System.nanoTime() - startTime;
System.out.println("Page locking: " + (pageLocked ? "SUCCESS" : "SIMULATED"));
System.out.println("Lock time: " + (lockTime / 1_000_000) + " ms");
// Write frame data using FastPointer for zero-allocation access
Pointer ptr = frameBuffer.pointer();
startTime = System.nanoTime();
// Simulate writing pixel data (RGBA for each pixel)
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
long pixelOffset = (y * width + x) * bytesPerPixel;
ptr.setInt(pixelOffset, (x << 16) | (y << 8) | 255); // RGBA pattern
}
}
writeTime = System.nanoTime() - startTime;
System.out.println("Frame write time: " + (writeTime / 1_000_000) + " ms");
// Read back random pixels to verify
startTime = System.nanoTime();
long pixelSum = 0;
for (int i = 0; i < 10000; i++) {
int randomPixel = (int) (Math.random() * (width * height));
pixelSum += ptr.getInt(randomPixel * bytesPerPixel);
}
readTime = System.nanoTime() - startTime;
System.out.println("Random pixel read (10k samples): " + (readTime / 1_000_000) + " ms");
System.out.println("Memory overhead: ~0 bytes (off-heap, no GC)");
System.out.println("Alignment: 32-byte (optimal for AVX2 SIMD)");
} // Memory automatically freed via try-with-resources
// ===== JAVA HEAP APPROACH (Traditional) =====
System.out.println("\n--- Java Heap (byte[] array) ---");
startTime = System.nanoTime();
byte[] heapFrameBuffer = new byte[(int) frameBufferSize];
long heapAllocTime = System.nanoTime() - startTime;
System.out.println("Allocation time: " + (heapAllocTime / 1_000_000) + " ms");
System.out.println("Address: Heap (no alignment guarantee)");
// Write frame data to heap array
startTime = System.nanoTime();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int pixelOffset = (y * width + x) * bytesPerPixel;
int pixelValue = (x << 16) | (y << 8) | 255;
heapFrameBuffer[pixelOffset] = (byte) (pixelValue >> 24);
heapFrameBuffer[pixelOffset + 1] = (byte) (pixelValue >> 16);
heapFrameBuffer[pixelOffset + 2] = (byte) (pixelValue >> 8);
heapFrameBuffer[pixelOffset + 3] = (byte) pixelValue;
}
}
long heapWriteTime = System.nanoTime() - startTime;
System.out.println("Frame write time: " + (heapWriteTime / 1_000_000) + " ms");
// Read random pixels
startTime = System.nanoTime();
long heapPixelSum = 0;
for (int i = 0; i < 10000; i++) {
int randomPixel = (int) (Math.random() * (width * height));
int pixelOffset = randomPixel * bytesPerPixel;
heapPixelSum |= (heapFrameBuffer[pixelOffset] << 24) |
(heapFrameBuffer[pixelOffset + 1] << 16) |
(heapFrameBuffer[pixelOffset + 2] << 8) |
heapFrameBuffer[pixelOffset + 3];
}
long heapReadTime = System.nanoTime() - startTime;
System.out.println("Random pixel read (10k samples): " + (heapReadTime / 1_000_000) + " ms");
long estimatedHeapMemory = frameBufferSize + 16; // array object header
System.out.println("Memory overhead: ~" + (estimatedHeapMemory / 1024 / 1024) + " MB (heap allocation)");
System.out.println("Alignment: None (may cause SIMD penalties)");
// ===== RESULTS =====
System.out.println("\n=== Performance Results ===");
System.out.println("Allocation speedup: " + String.format("%.1fx", (double) heapAllocTime / allocTime));
System.out.println("Write speedup: " + String.format("%.1fx", (double) heapWriteTime / writeTime));
System.out.println("Read speedup: " + String.format("%.1fx", (double) heapReadTime / readTime));
System.out.println("SIMD-ready: FastMemory = YES (32-byte aligned) | Heap = NO");
System.out.println("Page-locked: FastMemory = YES (no swap) | Heap = NO");
System.out.println("\n=== Demo finished successfully! ===");
System.out.println("FastMemory: SIMD-aligned, page-locked, zero-GC off-heap memory");
}
}