Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bzero(256) compiles to byte 0x00000000000000000... in TEAL, using up a lot of space. #245

Open
nullun opened this issue Nov 23, 2023 · 3 comments
Labels
optimization optimization on opcode cost, but doesn't change core functionality

Comments

@nullun
Copy link
Contributor

nullun commented Nov 23, 2023

TEALScript

import { Contract } from '@algorandfoundation/tealscript';

class Demo extends Contract {
	@allow.create('NoOp')
	demo(): void {
		const data = bzero(32);
	}
}

TEAL

demo:
	proto 1 0

	// demo.algo.ts:6
	// data = bzero(32)
	byte 0x0000000000000000000000000000000000000000000000000000000000000000
	frame_bury -1 // data: bytes
	retsub

I'd have expected.

demo:
	proto 1 0

	// demo.algo.ts:6
	// data = bzero(32)
	int 32
	bzero
	frame_bury -1 // data: bytes
	retsub
@joe-p
Copy link
Contributor

joe-p commented Nov 23, 2023

Yeah I made this decision because I think generally people run into opcode budget before they hit program size limit, so if a literal is used as the argument it will just write out the byteslice. I could turn this into a command-line flag to let the user decide if it should optimize for size or opcode cost.

@nullun
Copy link
Contributor Author

nullun commented Nov 24, 2023

Ah OK. In this case it's just a single extra opcode cost on a somewhat infrequently used opcode, so I wouldn't expect it to be the optimisation that makes or breaks someone's ability to run within the budget constraints.

In general does TEALScript prioritise program size over opcode budget, or is it an opcode by opcode judgement? Since budget can be increased greatly, whereas program size is restricted to 3 extra pages max, it could be argued program size is more limited.

I do think a compilation flag would help here, or even an optional one in the code itself. But I'd recommend having fine control over what types of optimisations happen and where if possible, otherwise people may have to manually alter the TEAL after compiling which we would want to avoid.

@joe-p
Copy link
Contributor

joe-p commented Nov 24, 2023

In general, my philosophy for TEALScript has always been to optimize for the thing that is the least obvious when it becomes unoptimized. In this case, if someone has a program that is too big that will be immediately obvious and it won't even compile because TEALScript does a call to algod to get the PC mapping. If someone were to have bzero in a loop they might not properly test it with enough iterations and only later find out that they run out of opcode budget once the contract scales to a certain point, where every single opcode cost is important.

But yeah agreed that opinionated stuff like this should be configurable.

@joe-p joe-p added bug Something isn't working enhancement New feature or request optimization optimization on opcode cost, but doesn't change core functionality and removed bug Something isn't working enhancement New feature or request labels Dec 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
optimization optimization on opcode cost, but doesn't change core functionality
Projects
Status: TODO
Development

No branches or pull requests

2 participants