@@ -47,24 +47,33 @@ public void close() {
4747 private static final class NameBuffer implements AutoCloseable {
4848 private Arena arena ;
4949 private MemorySegment segment ;
50- private int capacity ;
50+ private long capacity ;
5151
52- NameBuffer (int initialCapacity ) {
52+ NameBuffer (long initialCapacity ) {
5353 this .capacity = initialCapacity ;
5454 this .arena = Arena .ofConfined ();
5555 this .segment = this .arena .allocate (initialCapacity );
5656 }
5757
58- MemorySegment ensure (int needed ) {
58+ private MemorySegment ensure (long needed ) {
5959 if (needed > capacity ) {
6060 this .arena .close ();
6161 this .arena = Arena .ofConfined ();
62- this .segment = this . arena . allocate (needed );
63- this .capacity = needed ;
62+ this .capacity = Math . max (needed , capacity * 2 );
63+ this .segment = this . arena . allocate ( this . capacity ) ;
6464 }
6565 return segment ;
6666 }
6767
68+ public MemorySegment set (String value ) {
69+ byte [] utf8 = value .getBytes (StandardCharsets .UTF_8 );
70+ long needed = utf8 .length + 1 ;
71+ MemorySegment seg = ensure (needed );
72+ MemorySegment .copy (utf8 , 0 , seg , JAVA_BYTE , 0 , utf8 .length );
73+ seg .set (JAVA_BYTE , utf8 .length , (byte ) 0 );
74+ return seg ;
75+ }
76+
6877 @ Override
6978 public void close () {
7079 if (this .arena != null ) {
@@ -125,7 +134,7 @@ public void close() {
125134 }
126135
127136 public World () {
128- this .arena = Arena .ofShared ();
137+ this .arena = Arena .ofConfined ();
129138 this .nativeWorld = flecs_h .ecs_init ();
130139
131140 if (this .nativeWorld == null || this .nativeWorld .address () == 0 ) {
@@ -159,13 +168,7 @@ public long entity() {
159168
160169 public long entity (String name ) {
161170 this .checkClosed ();
162-
163- byte [] utf8 = name .getBytes (StandardCharsets .UTF_8 );
164- int len = utf8 .length ;
165-
166- MemorySegment nameSegment = this .defaultBuffers .nameBuffer ().ensure (len + 1 );
167- nameSegment .asSlice (0 , len ).copyFrom (MemorySegment .ofArray (utf8 ));
168- nameSegment .set (ValueLayout .JAVA_BYTE , len , (byte )0 );
171+ MemorySegment nameSegment = this .defaultBuffers .nameBuffer ().set (name );
169172
170173 MemorySegment desc = this .defaultBuffers .entityDescBuffer ().get ();
171174 ecs_entity_desc_t .name (desc , nameSegment );
@@ -295,14 +298,7 @@ public Query query(String expr) {
295298
296299 public long lookup (String name ) {
297300 this .checkClosed ();
298-
299- byte [] utf8 = name .getBytes (StandardCharsets .UTF_8 );
300- int len = utf8 .length ;
301-
302- MemorySegment segment = this .defaultBuffers .nameBuffer ().ensure (len + 1 );
303-
304- segment .asSlice (0 , len ).copyFrom (MemorySegment .ofArray (utf8 ));
305- segment .set (ValueLayout .JAVA_BYTE , len , (byte )0 );
301+ MemorySegment segment = this .defaultBuffers .nameBuffer ().set (name );
306302
307303 long entityId = flecs_h .ecs_lookup (this .nativeWorld , segment );
308304 return entityId == 0 ? -1 : entityId ;
0 commit comments