Skip to content

Commit 8342497

Browse files
riptlripatel-fd
authored andcommitted
capture: streaming base64 encode
Remove wksp and dynamic memory allocation (textstream) by using a streaming Base64 encoder.
1 parent 2385a4a commit 8342497

File tree

1 file changed

+7
-44
lines changed

1 file changed

+7
-44
lines changed

src/flamenco/capture/fd_solcap_yaml.c

Lines changed: 7 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
#include "../fd_flamenco.h"
21
#include "fd_solcap_proto.h"
32
#include "fd_solcap_reader.h"
43
#include "fd_solcap.pb.h"
54
#include "../runtime/fd_executor_err.h"
5+
#include "../../ballet/base64/fd_base64.h"
66
#include "../../ballet/nanopb/pb_decode.h"
7-
#include "../../util/textstream/fd_textstream.h"
87
#include <errno.h>
98
#include <stdio.h>
109

@@ -118,7 +117,6 @@ process_account( FILE * file,
118117
allows padding in the middle, but it's cleaner to only have
119118
padding at the end of the message. */
120119
# define PART_RAW_SZ (720UL)
121-
# define PART_BLK_SZ (4UL*(PART_RAW_SZ+2UL)/3UL) /* see fd_textstream_encode_base64 */
122120
ulong data_sz = meta->data_sz;
123121
while( data_sz>0UL ) {
124122
ulong n = fd_ulong_min( data_sz, PART_RAW_SZ );
@@ -129,25 +127,13 @@ process_account( FILE * file,
129127
FD_LOG_ERR(( "fread account data failed (%d-%s)", errno, strerror( errno ) ));
130128

131129
/* Encode chunk */
132-
fd_valloc_t valloc = fd_scratch_virtual();
133-
fd_scratch_push();
134-
135-
fd_textstream_t _data_out[1];
136-
fd_textstream_t * data_out = fd_textstream_new( _data_out, valloc, PART_BLK_SZ );
137-
fd_textstream_encode_base64( data_out, buf, n );
138-
139-
/* Get pointer to encoded chunk */
140-
FD_TEST( 1UL==fd_textstream_get_iov_count( data_out ) );
141-
struct fd_iovec iov[1];
142-
FD_TEST( 0 ==fd_textstream_get_iov( data_out, iov ) );
143-
130+
char chunk[ FD_BASE64_ENC_SZ( PART_RAW_SZ ) ];
131+
ulong chunk_sz = fd_base64_encode( chunk, buf, n );
144132
/* Print encoded chunk */
145-
FD_TEST( 1UL==fwrite( iov[0].iov_base, iov[0].iov_len, 1UL, stdout ) );
133+
FD_TEST( 1UL==fwrite( chunk, chunk_sz, 1UL, stdout ) );
146134

147135
/* Wind up for next iteration */
148136
data_sz -= n;
149-
fd_textstream_destroy( data_out ); /* technically noop */
150-
fd_scratch_pop();
151137
}
152138
# undef PART_RAW_SZ
153139
# undef PART_BLK_SZ
@@ -473,36 +459,16 @@ main( int argc,
473459
for( int i=1; i<argc; i++ )
474460
if( 0==strcmp( argv[i], "--help" ) ) return usage();
475461

476-
char const * _page_sz = fd_env_strip_cmdline_cstr ( &argc, &argv, "--page-sz", NULL, "gigantic" );
477-
ulong page_cnt = fd_env_strip_cmdline_ulong( &argc, &argv, "--page-cnt", NULL, 2UL );
478-
ulong scratch_mb = fd_env_strip_cmdline_ulong( &argc, &argv, "--scratch-mb", NULL, 1024UL );
479-
int verbose = fd_env_strip_cmdline_int ( &argc, &argv, "-v", NULL, 0 );
480-
ulong start_slot = fd_env_strip_cmdline_ulong( &argc, &argv, "--start-slot", NULL, 0 );
481-
ulong end_slot = fd_env_strip_cmdline_ulong( &argc, &argv, "--end-slot", NULL, ULONG_MAX );
482-
483-
ulong page_sz = fd_cstr_to_shmem_page_sz( _page_sz );
484-
if( FD_UNLIKELY( !page_sz ) ) FD_LOG_ERR(( "unsupported --page-sz" ));
462+
int verbose = fd_env_strip_cmdline_int ( &argc, &argv, "-v", NULL, 0 );
463+
ulong start_slot = fd_env_strip_cmdline_ulong( &argc, &argv, "--start-slot", NULL, 0 );
464+
ulong end_slot = fd_env_strip_cmdline_ulong( &argc, &argv, "--end-slot", NULL, ULONG_MAX );
485465

486466
if( argc!=2 ) {
487467
fprintf( stderr, "ERROR: expected 1 argument, got %d\n", argc-1 );
488468
usage();
489469
return 1;
490470
}
491471

492-
/* Create workspace and scratch allocator */
493-
494-
fd_wksp_t * wksp = fd_wksp_new_anonymous( page_sz, page_cnt, fd_log_cpu_id(), "wksp", 0UL );
495-
if( FD_UNLIKELY( !wksp ) ) FD_LOG_ERR(( "fd_wksp_new_anonymous() failed" ));
496-
497-
ulong smax = scratch_mb << 20;
498-
void * smem = fd_wksp_alloc_laddr( wksp, fd_scratch_smem_align(), smax, 1UL );
499-
if( FD_UNLIKELY( !smem ) ) FD_LOG_ERR(( "Failed to alloc scratch mem" ));
500-
ulong scratch_depth = 4UL;
501-
void * fmem = fd_wksp_alloc_laddr( wksp, fd_scratch_fmem_align(), fd_scratch_fmem_footprint( scratch_depth ), 2UL );
502-
if( FD_UNLIKELY( !fmem ) ) FD_LOG_ERR(( "Failed to alloc scratch frames" ));
503-
504-
fd_scratch_attach( smem, fmem, smax, scratch_depth );
505-
506472
/* Open file */
507473

508474
char const * path = argv[ 1 ];
@@ -558,9 +524,6 @@ main( int argc,
558524
/* Cleanup */
559525

560526
FD_LOG_NOTICE(( "Done" ));
561-
FD_TEST( fd_scratch_frame_used()==0UL );
562-
fd_wksp_free_laddr( fd_scratch_detach( NULL ) );
563-
fd_wksp_free_laddr( fmem );
564527
fclose( file );
565528
fd_halt();
566529
return 0;

0 commit comments

Comments
 (0)